title: ssh 批量分发
tags: sshkey,批量,分发
grammar_cjkRuby: true
sshkey批量分发,管理方案
1. 批量发简述
linux运维工程师,每天都需要维护大量的主机,特别是互联网公司的运维工程师,需要看大量的日志,硬件状态信息,服务状态信息,有时候甚至要导入大量文件或者配置文件,这样的话,如果你还一台一台登录操的话,效率低下不用说,每天的工作往往是满满的负荷工作,而且是机械式的一台一台看。但是如果你了解熟悉ssh就不一样了,ssh可以通过主机间的互信,来免密码登录,还可以利用脚本完成一些重要的工作。比如给各个主机分发hosts文件等等。
2. ssh 连接原理
ssh 建立连接方式有密码登录验证和 密钥登录验证,我们现在用的是密钥登录来让主机之间添加互信关系。其中密钥登录的原理图如上图。
- 客户端和服务端建立通道连接
- 客户端向服务端发送公钥(其中公钥和私钥都可以用ssh-keygen生成,默认生成一次就是一对,分别是公钥和私钥,私钥是保存在客户端,公钥是保存在服务端,其中生成公钥私钥可以理解为去超市买一把锁,默认都是有锁有钥匙,其中锁给服务端,钥匙自己留着,用来去服务端开锁)
- 客户端发送过来公钥,服务端会检查自有的known_hosts文件进行验证,如果服务端发现这次访问时第一次,它会提示客户端,让客户端敲yes后,服务端会把客户端的指纹信息写在known_hosts,然后让客户端输入服务端的密码,如果客户端输正确了,服务端会把该公钥放在自己的.ssh目录中
- 客户端带着私钥过来要求连接服务端
- 服务端用公钥质询加密发给客户端
- 客户端拿配套的私钥进行解密后的质询发送回服务端
- 验证通过,双方建立连接,客户端终于连接上了服务端
3. scp工具解析
- scp命令和cp命令很像,cp是本地拷贝,scp是加密的远程拷贝,参数的区别是,-p 是保持属性,-P是端口号。
- scp可以把数据从一台机器推送到另一台机器,也可以从其他服务器把数据拉回本地。
- scp和rsync的区别是,scp不支持增量拷贝,且没有daemon模式,每一次都是全量拷贝,效率低,适合第一用,如果是增量备份还是推荐使用rsync。举例如下:
推 [[email protected] ~]# scp -r -p -P22 GNU-Linux-x86/ [email protected]:/tmp [email protected]‘s password: confxml.xml 100% 2214 2.2KB/s 00:00 拉 [[email protected] ~]# scp -p -P22 [email protected]:/tmp/rsync_fail_log.sh ~/ [email protected]‘s password: rsync_fail_log.sh 100% 0 0.0KB/s 00:00 [[email protected] ~]# ls |grep rsync_fail_log.sh rsync_fail_log.sh
4. 密钥生成与分发
密钥生成就是一对,分为公钥和私钥,公钥为锁,私钥为钥匙,所以一定要清楚,是钥匙开锁,而不是锁开钥匙。因此私钥自己留着,公钥是发给对方,然后我们可以拿着私钥去登录对方(打开对方的门)。锁的定义很形象,生成密钥可以想象中是去超市买锁,因此只要有ssh-keygen这个工具都是可以生成密钥,密钥是不限主机的,只要分清,私钥自己拿着,公钥给别人。我们一般使用dsa的密钥对,生成密钥的步骤如下:
[[email protected] .ssh]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
b8:42:03:c2:9d:ee:bf:10:83:6c:44:a5:1f:c8:39:80 [email protected]
The key‘s randomart image is:
+--[ DSA 1024]----+
|o... |
|E = . |
|.O.+ |
|o.=.. . |
| + =o . S |
|. ..o. . |
| o. . |
| o. |
| o. |
+-----------------+
[[email protected] .ssh]# ls -a
. .. id_dsa id_dsa.pub
[[email protected] .ssh]# ls -l
total 8
-rw-------. 1 root root 668 May 23 01:30 id_dsa
生成密钥我们用ssh-keygen,分配公钥给对方我们使用ssh-copy-id ,
[[email protected] .ssh]# ssh-copy-id -i 192.168.50.4
/usr/bin/ssh-copy-id: ERROR: No identities found
[[email protected] .ssh]# ssh-copy-id -i id_dsa.pub 192.168.50.4
The authenticity of host ‘192.168.50.4 (192.168.50.4)‘ can‘t be established.
RSA key fingerprint is 41:8a:f8:19:54:e2:e1:fa:eb:9e:3a:22:d2:1f:08:00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.50.4‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘192.168.50.4‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
[[email protected] .ssh]#
如果对方的ssh端口号改了,不是默认端口则我们也要改
[[email protected] .ssh]# ssh-copy-id -i id_dsa.pub "-P22 [email protected]"
The authenticity of host ‘192.168.50.3 (192.168.50.3)‘ can‘t be established.
RSA key fingerprint is 41:8a:f8:19:54:e2:e1:fa:eb:9e:3a:22:d2:1f:08:00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.50.3‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘-P22 [email protected]‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting
5. 文件分发实例讲解
利用ssh互信,我们可以让主机之间免密码登录,发送文件给对方,这样我们就可以通过脚本做很多事情了如下列实例:
如图:存储C作为核心中的分发机,因为图中架构所有设备都处于内网内,为了提高各个主机的互访能力,因此/etc/hosts文件都对各个服务器的主机名和ip有解析。因此各个主机的hosts文件是保持一致的,现在要求以分发机的hosts文件为准,分发机C将最新的hosts文件定时分发到各主机,覆盖原来的hosts文件,这样让所有主机都可以及时更新到最新的hosts文件,这样如果hosts文件有更新的话就只要改一下分发机的hosts文件即可。分析解答如下:
- 我们可以通过脚本利用ssh互信免密码,将主机的hosts文件通过scp推送到各个主机,但是存在一个问题,/etc目录必须要root权限才能放进去,因此必须选择root互信才能完成需求。这样可以直接推送到/etc/下完成覆盖。
[[email protected] .ssh]# ssh-keygen -t dsa Generating public/private dsa key pair. [[email protected] .ssh]# ls id_dsa id_dsa.pub [[email protected] .ssh]# ssh-copy-id -i id_dsa.pub "-P22 192.168.50.2" The authenticity of host ‘192.168.50.2 (192.168.50.2)‘ can‘t be established. [[email protected] .ssh]# ssh-copy-id -i id_dsa.pub "-P22 192.168.50.3" The authenticity of host ‘192.168.50.3 (192.168.50.3)‘ can‘t be established. [[email protected] .ssh]# ssh-copy-id -i id_dsa.pub "-P22 192.168.50.4" The authenticity of host ‘192.168.50.4 (192.168.50.4)‘ can‘t be established 确认是不是互信成功 [[email protected] .ssh]# vim /service/scripts/check-ssh.sh #!/usr/bin/env bash ############################################################## # File Name: /service/scripts/check-ssh.sh # Version: V1.0 # Author: OuYoung # Blog: http://blog.51cto.com/ouyangtao # Created Time : 2018-05-23 12:49:19 # Description: ############################################################## for n in 2 3 4 do echo "This is 192.168.50.$n" ssh [email protected]$n hostname done [[email protected] .ssh]# sh /service/scripts/check-ssh.sh This is 192.168.50.2 MYSQL-B This is 192.168.50.3 LAMP-A1 This is 192.168.50.4 backup-D 写个简单的批发脚本,执行成功后写入crontab里,默认每30分钟执行一次分发。 [[email protected] .ssh]# vim /service/scripts/fengfa1.sh #!/usr/bin/env bash ############################################################## # File Name: /service/scripts/fengfa1.sh # Version: V1.0 # Author: OuYoung # Blog: http://blog.51cto.com/ouyangtao # Created Time : 2018-05-23 13:18:25 # Description: ############################################################## . /etc/init.d/functions FILE=$1 REMOUTEDIR=$2 if [ $# -ne 2 ] then echo "WARNINT: $0: LOCAL FILE AND REMOUTE DIR " exit 1; fi for n in 2 3 4 do cp -r $FILE /fengfa/ &&\ scp /fengfa/$(basename $FILE) [email protected]$n:$REMOUTEDIR >/dev/null if [ $? -eq 0 ] then action "192.168.50.$n fengfa $1 is OK" true else action "192.168.50.$n fengfa $1 is fail" false fi done 执行测试脚本,先在分发机上/etc/hosts文件上改动,然后执行脚本,再看每台被分发机上是否有覆盖。 [[email protected] .ssh]# echo "# test fengfa is ok" >>/etc/hosts [[email protected] .ssh]# sh /service/scripts/fengfa1.sh /etc/hosts /etc 192.168.50.2 fengfa /etc/hosts is OK [ OK ] 192.168.50.3 fengfa /etc/hosts is OK [ OK ] 192.168.50.4 fengfa /etc/hosts is OK [ OK ] [[email protected] ~]# tail -1 /etc/hosts # test fengfa is ok [[email protected] .ssh]# tail -1 /etc/hosts # test fengfa is ok [[email protected] ~]# tail -1 /etc/hosts # test fengfa is ok 在分发机上的cron文件写入,每三十分钟执行一次 [[email protected] ~]# echo "*/30 * * * * sh /service/scripts/fengfa1.sh /etc/hosts /etc " >>/var/spool/cron/root [[email protected] ~]# crontab -l |grep "/etc/hosts" */30 * * * * sh /service/scripts/fengfa1.sh /etc/hosts /etc
- 方法1中存在安全隐患问题,如果分发机和各个主机都是root互信的话,那么分发机就拥有所有主机的root权限,那么分发机可以随时百分百的控制各个主机,这样稍有不慎就会酿成大错,非常的不安全。我们可以在选择用普通用户做互信,先将hosts文件发送到该用户的家目录,然后给rsync添加SUID权限,然后让该用户用rsync将hosts文件在推送到本地的/etc
[[email protected] ~]# chmod u+s /usr/bin/rsync [[email protected] ~]# ls -l `which rsync` -rwsr-xr-t. 1 root root 410536 Apr 30 2014 /usr/bin/rsync [[email protected] ~]# chmod u+s /usr/bin/rsync [[email protected] ~]# ls -l `which rsync` -rwsr-xr-x. 1 root root 410536 Apr 30 2014 /usr/bin/rsync [[email protected] ~]# chmod u+s /usr/bin/rsync [[email protected] ~]# ls -l `which rsync` -rwsr-xr-x. 1 root root 410536 Apr 30 2014 /usr/bin/rsync 我们把分发脚本稍微改一下,重新命名fengfa2.sh [[email protected] ~]$ vim /service/scripts/fengfa2.sh #!/usr/bin/env bash ############################################################## # File Name: /service/scripts/fengfa1.sh # Version: V1.0 # Author: OuYoung # Blog: http://blog.51cto.com/ouyangtao # Created Time : 2018-05-23 13:18:25 # Description: ############################################################## . /etc/init.d/functions FILE=$1 REMOUTEDIR=$2 if [ $# -ne 2 ] then echo "WARNINT: $0: LOCAL FILE AND REMOUTE DIR " exit 1; fi for n in 2 3 4 do cp -r $FILE /fengfa/ &&scp /fengfa/$(basename $FILE) [email protected]$n:~/ &>/dev/null &&ssh [email protected]$n "rsync ~/$(basename $FILE) $REMOUTEDIR" &>/dev/null if [ $? -eq 0 ] then action "192.168.50.$n fengfa $1 is OK" true else action "192.168.50.$n fengfa $1 is fail" false fi done 再次进行测试发现: [[email protected] ~]$ sh /service/scripts/fengfa2.sh /etc/hosts /etc 192.168.50.2 fengfa /etc/hosts is OK [ OK ] 192.168.50.3 fengfa /etc/hosts is OK [ OK ] 192.168.50.4 fengfa /etc/hosts is OK [ OK ]
- 当然方法二也不安全,把rsync的权限设置为SUID的话,那么任何用户执行rsync都可以像root执行rsync一样,那安全隐患也很大,我们继续限制让只有test用户才可以执行rsync,其他的普通用户依然不能执行rsync,这样安全性减少很多。因此我们利用sudo,在每个客户端上将rsync设置为test用户可以执行,如:
[[email protected] ~]# chmod u-s `which rsync` [[email protected] ~]# echo "test ALL=(ALL) NOPASSWD: /usr/bin/rsync" >>/etc/sudoers [[email protected] ~]# tail -1 /etc/sudoers test ALL=(ALL) NOPASSWD: /usr/bin/rsync [[email protected] ~]# visudo -c /etc/sudoers: parsed OK [[email protected] ~]# chmod u-s `which rsync` [[email protected] ~]# echo "test ALL=(ALL) NOPASSWD: /usr/bin/rsync" >>/etc/sudoers [[email protected] ~]# tail -1 /etc/sudoers test ALL=(ALL) NOPASSWD: /usr/bin/rsync [[email protected] ~]# visudo -c /etc/sudoers: parsed OK [[email protected] ~]# chmod u-s `which rsync` [[email protected] ~]# echo "test ALL=(ALL) NOPASSWD: /usr/bin/rsync" >>/etc/sudoers [[email protected] ~]# chmod u-s `which rsync` [[email protected] ~]# tail -1 /etc/sudoers test ALL=(ALL) NOPASSWD: /usr/bin/rsync [[email protected] ~]# visudo -c /etc/sudoers: parsed OK 分发脚本稍微改一下,重新命名fengfa23.sh [[email protected] ~]$ vim /service/scripts/fengfa3.sh #!/usr/bin/env bash ############################################################## # File Name: /service/scripts/fengfa1.sh # Version: V1.0 # Author: OuYoung # Blog: http://blog.51cto.com/ouyangtao # Created Time : 2018-05-23 13:18:25 # Description: ############################################################## . /etc/init.d/functions FILE=$1 REMOUTEDIR=$2 if [ $# -ne 2 ] then echo "WARNINT: $0: LOCAL FILE AND REMOUTE DIR " exit 1; fi for n in 2 3 4 do cp -r $FILE /fengfa/ &&scp /fengfa/$(basename $FILE) [email protected]$n:~/ &>/dev/null &&ssh -t [email protected]$n "sudo rsync ~/$(basename $FILE) /etc/" &>/dev/null if [ $? -eq 0 ] then action "192.168.50.$n fengfa $1 is OK" true else action "192.168.50.$n fengfa $1 is fail" false fi done [[email protected] ~]$ sh /service/scripts/fengfa3.sh /etc/hosts /etc 192.168.50.2 fengfa /etc/hosts is OK [ OK ] 192.168.50.3 fengfa /etc/hosts is OK [ OK ] 192.168.50.4 fengfa /etc/hosts is OK [ OK ]
6. 批量分发小结
其实现在很多人批量分发都看不起sshkey,他们大都采用puppet和saltstack,诚然puppet和saltstack必然是更加专业而且更加强大,但是配置起来比起sshkey的方案难太多了,根据linux的基本原则,如果简单易用的方法可以解决需求,那请尽量选择简单易用的方案,如果不满足需求才考虑其他更优的方案,因为我们的运维法则是简单,易用,高效,毕竟ssh key才是中小企业最基本实用的批量分发,管理方案。
原文地址:http://blog.51cto.com/ouyangtao/2123158