*、在一坨机器中,若总是让你输入密码,我想你也会疯的,所以这个时候免密功能将能为我们带来便利,但是注意私钥的安全,注意私钥的安全,注意私钥的安全。哈哈。
另外:若在目标机器上需要执行的指令过多,建议写成脚本,然后发送到目标机器,调用执行即可
为一个集群环境配置免密登录,需要以下源码指令
#!/bin/bash #多个IP使用空格分割即可 SERVERS="IP1 IP2 IP3" PASSWORD=ppp auto_ssh_copy_id() { expect -c "set timeout -1; #若遇到ssh-copy-id不存在时,采用变通的方式:实际就是要把id_rsa.pub公钥放到目标~/.ssh/authorized_keys spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$1; #spawn scp /root/.ssh/authorized_keys root@$1:/root/.ssh/; expect { *(yes/no)* {send -- yes\r;exp_continue;} *assword:* {send -- $2\r;exp_continue;} eof {exit 0;} }"; } ssh_copy_id_to_all() { #ssh-keygen -t rsa后续优化:若已存在让使用者判断是否重建秘钥 #cat /root/.ssh/id_rsa.pub > authorized_keys后续优化:先将该文件生成并赋值 for SERVER in $SERVERS do auto_ssh_copy_id $SERVER $PASSWORD done } ssh_copy_id_to_all #del ssh-pub-key删除已有公钥 #for i in ip1 ip2; do ssh $i "rm -f ~/.ssh/auth*" done
在已生成公钥私钥的基础上,配置上目标IP,空格分割,执行即可