ssh 多台服务器之间连接(linux)
* ssh 多台服务器之间连接
例如有4台机器,需要相互之间不使用密码通信。
|----------+-------------|
| host | IP |
|----------+-------------|
| server01 | 192.168.0.1 |
| server02 | 192.168.0.2 |
| server03 | 192.168.0.3 |
| server04 | 192.168.0.4 |
|----------+-------------|
** 第一步:生成各自的文件夹
server01:# mkdir -p /root/.ssh; chmod 700 /root/.ssh
server02:# mkdir -p /root/.ssh; chmod 700 /root/.ssh
server03:# mkdir -p /root/.ssh; chmod 700 /root/.ssh
server04:# mkdir -p /root/.ssh; chmod 700 /root/.ssh
** 第二步: 生成各自的key
1. 分别进入/root/.ssh/目录,执行命令:
server01:# ssh-keygen -t rsa
server02:# ssh-keygen -t rsa
server03:# ssh-keygen -t rsa
server04:# ssh-keygen -t rsa
一路回车即可生成对应的key(private key: id_rsa ) ( public key: id_rsa.pub )
server01:# cp id_rsa.pub authorized_keys
server02:# cp id_rsa.pub authorized_keys
server03:# cp id_rsa.pub authorized_keys
server04:# cp id_rsa.pub authorized_keys
2. 四台服务器上分别执行下面买的命令
server01:# ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N ""
server01:# ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_rsa_key -N ""
server01:# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ""
server01:# /etc/init.d/ssh restart
** 第三步: 添加不同服务器的public key(id_rsa.pub)到自己的authorized_keys文件中
(注意:下面的scp命令中还需要手动输入密码才能登陆)
server01:# scp 192.168.0.2:/root/.ssh/id_rsa.pub /root/
server01:# cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
server01:# scp 192.168.0.3:/root/.ssh/id_rsa.pub /root/
server01:# cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
server01:# scp 192.168.0.4:/root/.ssh/id_rsa.pub /root/
server01:# cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
现在server01上的/root/.ssh/authorized_keys已经包含了四台服务器的public key。
server01:# scp /root/.ssh/authorized_keys scp 192.168.0.2:/root/.ssh/authorized_keys
server01:# scp /root/.ssh/authorized_keys scp 192.168.0.3:/root/.ssh/authorized_keys
server01:# scp /root/.ssh/authorized_keys scp 192.168.0.4:/root/.ssh/authorized_keys
至此,应该可以相互之间无密码连接了。
engoy it!