SSH免密码登录

 

ssh-keygen -t rsa

scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys   //直接覆盖了

 

追加KEY

$ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //将文件拷贝至远程服务器
$ cat ~/pub_key >>~/.ssh/authorized_keys //将内容追加到authorized_keys文件中, 不过要登录远程服务器来执行这条命令

 

 

一个shell脚本实现linux集群所有机器免密钥登录

 

1,先安装expect
yum install expect

2.生成密钥
ssh-keygen(注,一路回车,不用管)

3.修改host文件 /etc/hosts

4.编写shell脚本

#!/bin/bash
SERVERS="flux01 flux02 flux03 flux04 flux05  flux06  flux07  flux08  flux09"
PASSWORD=root
auto_ssh_copy_id() {
expect -c "set timeout -1;
spawn ssh-copy-id $1;
expect {
*(yes/no)* {send -- yes\r;exp_continue;}
*assword:* {send -- $2\r;exp_continue;}
eof        {exit 0;}
}";
}

ssh_copy_id_to_all() {
for SERVER in $SERVERS
do
auto_ssh_copy_id $SERVER $PASSWORD
done
}

ssh_copy_id_to_all

 

 

 

普通用户注意权限

  • 注意目录和文件的权限/home/zhangsan/.ssh 的 owner 应该为 zhangsan:group

chown​ -R  zhangsan:dev /home/zhangsan/.ssh

chmod 700 /home/zhangsan/.ssh

​chmod 600 /home/zhangsan/.ssh/authorized_keys​

登录测试ssh -i /home/zhangsan/.ssh/id_rsa zhangsan@127.0.0.1

 

 

详细参考:http://www.jb51.net/article/94599.htm

 

 

 

您可以选择一种方式赞助本站