SSH批量分发与管理
一、SSH服务介绍
SSH是Secure Shell Protocol的简写,由IETF网络工作小组制定;在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后再进行数据传输,确保了传递的数据安全。
SSH是专为远程登录会话和其他网络服务提供的安全性协议。利用SSH协议可以有效的防止远程管理过程中的信息泄露问题,在当前的生产环境当中,绝大多数企业普遍采用SSH协议服务来代替传统的不安全的远程联机服务软件。如telnet等。
SSH服务结构:
SSH服务是由服务端软件OpenSSH和客户端(常见的有SSH,SecureCRT,Xshell,Putty)组成,SSH服务默认使用22端口提供服务,它有两个不兼容的SSH协议版本,分别是1.x和2.x。
二、SSH服务认证类型
从SSH客户端来看,SSH服务主要有两种级别安全验证,具体级别如下:
1.基于口令的安全认证
2.基于密钥对的安全认证:基于密钥的安全认证也有windows客户端和linux客户端的区别。
三、SSH服务优化
修改sshd.conf
Port52113 #为了提高安全级别,建议改掉SSH服务默认连接端口PermitRootLoginno #root超级用户***都知道,建议禁止它(root)远程登陆PermitEmptyPasswordsno #禁止空密码登录UseDNSno #不使用DNSGSSAPIAuthentication no #加快SSH连接速度
四、SSH批量分发管理实战
批量分发数据或者文件
(1)添加系统账号,并修改密码
[root@A~]#useradd fenfa[root@A~]#id fenfauid=503(fenfa) gid=503(fenfa) groups=503(fenfa)[root@A~]#echo 123456|passwd --stdin fenfa
注意:所有被管理主机都要创建该用户
(2)创建密钥对
[fenfa@A ~]$ ssh-keygen -t dsaGenerating public/private dsa key pair.Enter file in which to save the key(/home/fenfa/.ssh/id_dsa):Created directory '/home/fenfa/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again: #此处回车Your identification has been saved in/home/fenfa/.ssh/id_dsa.Your public key has been saved in/home/fenfa/.ssh/id_dsa.pub.The key fingerprint is: #此处回车0e:99:ef:7f:2d:5c:36:88:79:09:7a:89:e0:d1:f7:fcfenfa@AThe key's randomart p_w_picpath is: #此处回车+--[ DSA 1024]----+| || || . || oo. o || .+oS+ B o || .+o = * + || o. o = . || . + E || .... . |+-----------------+
默认会在fenfa用户的家目录/home/fenfa/.ssh下生成两个文件:
id_dsa.pub #公钥,权限644,分发给需要管理的主机
id_dsa #私钥,权限600,保留在本地
(3)推送公钥到管理主机
实例为推送到192.168.0.111主机,192.168.0.112同样的方法推送
[fenfa@A~]$ ssh-copy-id -i.ssh/id_dsa.pub "-p 22 fenfa@192.168.0.111"The authenticity of host '192.168.0.111(192.168.0.111)' can't be established.RSA key fingerprint is85:83:52:21:20:dd:4a:7c:3c:df:ec:5a:de:a0:b4:82.Are you sure you want to continueconnecting (yes/no)? yesWarning: Permanently added '192.168.0.111'(RSA) to the list of known hosts.fenfa@192.168.0.111's password:Now try logging into the machine, with"ssh '-p 22 fenfa@192.168.0.111'", and check in: .ssh/authorized_keys #出现这个表示推送公钥成功to make sure we haven't added extra keysthat you weren't expecting.
注意:ssh-copy-id的特殊应用
如果SSH修改成了特殊端口,如52113,那么,用上面的ssh-copy-id命令就无法进行分发公钥匙了。如果仍要使用ssh-copy-id的话,那么可能的解决方法有两个:
1.命令为: ssh-copy-id -i .ssh/id_dsa.pub "-p 52113 fenfa@192.168.0.111"#特殊端口分发,要适当加引号
2.编辑vi /usr/bin/ssh-copy-id 在第41行做如下修改,见加粗部分
41 { eval "$GET_ID" ; } | ssh -p22 $1 "umask 077; test -d ~/.ssh ||
mkdir ~/.ssh ; cat >>~/.ssh/authorized_keys && (test -x /sbin/
restorecon && /sbin/restorecon~/.ssh ~/.ssh/authorized_keys >/d
ev/null 2>&1 || true)" ||exit 1 #在41行中的开头ssh后面和$1前面加入自定义的ssh端口
说明:ssh-copy-id的原理(ssh-copy-id -i .ssh/id_dsa.pub "-p52113 fenfa@192.168.0.111")
就是把.ssh/id_dsa.pub复制到192.168.0.111的fenfa用户家目录下面的.ssh目录(提前创建,权限为700)下,并做了更改名字的操作,名字改为authorized_keys,权限变为600.
(4)检查被管理主机上的公钥
[fenfa@B ~]$ ll .ssh/total 4-rw------- 1 fenfa fenfa 598 Jul 25 22:59authorized_keys [fenfa@C ~]$ ll .ssh/total 4-rw------- 1 fenfa fenfa 598 Jul 25 22:47authorized_keys
(5)批量分发测试
[fenfa@A ~]$ whoamifenfa[fenfa@A ~]$ echo 123 >a.txt[fenfa@A ~]$ lltotal 4-rw-rw-r-- 1 fenfa fenfa 4 Jul 26 00:00a.txt[fenfa@A ~]$ cat a.txt123[fenfa@A ~]$ scp -P22 a.txt fenfa@192.168.0.111:~a.txt 100% 4 0.0KB/s 00:00 [fenfa@A ~]$ scp -P22 a.txt fenfa@192.168.0.112:~a.txt 100% 4 0.0KB/s 00:00
批量分发脚本:
①建立被管理主机地址库
②创建批量分发脚本
实例:将分发主机(192.168.1.114)家目录的text.txt文件分发到被管理主机(192.168.1.113,192.168.1.115)
[fenfa@server_04 ~]$ cat ip.txt 192.168.1.113192.168.1.115[fenfa@server_04 ~]$ cat plfenfa.sh #!/bin/bash. /etc/profile. /etc/init.d/functionsFile_name=test.txtFile_dir=/home/fenfaFenfa_user=fenfafor ip in `cat /home/fenfa/ip.txt` do rsync -avz -e "ssh -p52113" ${File_dir}/$File_name ${Fenfa_user}@$ip:~ action "${Fenfa_user}@$ip ${File_dir}/$File_name copy" /bin/truedone[fenfa@server_04 ~]$ [fenfa@server_04 ~]$ sh plfenfa.sh sending incremental file listtest.txt sent 91 bytes received 37 bytes 256.00 bytes/sectotal size is 9 speedup is 0.07fenfa@192.168.1.113 /home/fenfa/test.txt copy [ OK ]sending incremental file listtest.txt sent 91 bytes received 31 bytes 244.00 bytes/sectotal size is 9 speedup is 0.07fenfa@192.168.1.115 /home/fenfa/test.txt copy [ OK ]
小结:
1)免密码登陆验证是单向的(管理主机--->被管理主机)
2)基于用户的,最好不要跨不同的用户
3)批量分发初始都需要输入一次密码,并且第一次连接要确认
(6)sudo对分发用户fenfa提权
root用户,visudo命令修改
在文件结尾加入一下内容:
fenfa ALL=(ALL) NOPASSWD:/bin/cp
说明:
fenfa | ALL | (ALL) | NOPASSWD | /bin/cp |
使用sudo的用户 | 允许使用sudo的主机 | 使用sudo免密码 | 使用sudo可以执行的命令,如果是允许所有命令填NOPASSWD:ALL |
root用户,vim /etc/sudoers直接修改sudoers文件,同理添加同上面的内容
五、利用expect脚本实现免交互ssh密钥分发
1)安装expect执行环境
2)expect批量分发,免交互脚本示例
主要有两部分:fenfa_sshkey.exp和fenfa.sh ,使用时直接执行fenfa.sh即可
[root@server_05 scripts]# cat fenfa_sshkey.exp #!/usr/bin/expectif { $argc != 2 } { send_user "usage: expect fenfa_sshkey.exp file host\n" exit}#define varset file [lindex $argv 0]set host [lindex $argv 1]set password "123456" ##分发帐号的密码set user "fenfa" ##分发帐号set port "52113" ##分发主机的ssh端口spawn ssh-copy-id -i $file "-p $port $user@$host"expect { "yes/no" {send "yes\r";exp_continue} "*password" {send "$password\r"}}expect eof
[root@server_05 scripts]# cat fenfa.sh #!/bin/bashIpaddr_head=192.168.1User=fenfaPort=52113Commond_dir=/usr/binif [ $UID -ne 0 ] then echo "Error:Please use root account to exec this script!" else for n in `seq 5` do ${Commond_dir}/ssh-copy-id -i "-p $Port ${User}@${Ipaddr_head}.$n" &2>/dev/null if [ $? -eq 0 ] then action "${Ipaddr_head}$n copy ssh_key..." /bin/ture else action "${Ipaddr_head}$n copy ssh_key..." /bin/false fi donefi