环境:
主机名 | IP |
jftest-221 | 192.168.31.221 |
jftest-222 | 192.168.31.222 |
jftest-223 | 192.168.31.223 |
以jftest-222为例:
[root@jftest-222 ~]# ssh-keygen
密钥对存放位置 ~/.ssh/
其中 id_rsa 为私钥; id_rsa.pub为公钥
使用ssh-C++opy-id 命令,这里使用shell 脚本直接copy至包含本机的3台(本机也可以免密连接)
[root@jftest-222 .ssh]# export HostList="192.168.31.221
192.168.31.222
192.168.31.223"
[root@jftest-222 .ssh]# for ip in $HostList; do ssh-copy-id $ip; done
如果是单台直接 执行 ssh-copy-id $ip 。
[root@jftest-222 .ssh]# sed -i '$a\192.168.31.221 jftest-221\n192.168.31.222 jftest-222\n192.168.31.223 jftest-223' /etc/hosts
使用 ip 以及 hostname 都可以免密连接。
还可以手动copy公钥内容至远端服务器$HOME/.ssh/authorized_keys下,
如不存在该文件,可手动创建,修改该文件权限为600
以jftest-223为例:
[root@jftest-223 .ssh]# ssh-keygen
[root@jftest-223 ~]# cat ~/.ssh/id_rsa.pub
把公钥内容复制到jftest-222的$HOME/.ssh/authorized_keys;
测试连接正常