环境介绍
两台CentOS服务器,主机名和IP分别是: server1 192.168.1.69 server2 192.168.1.70
准备
#两台都要安装
yum install -y openssh-clients
分别打开两台服务器的hosts文件,在文件末尾添加以下内容,让主机名和IP对应。(还有就是别忘了分别修改两台服务器的/etc/sysconfig/network文件里的HOSTNAME,改成对应的服务器的主机名即可。)
vim /etc/hosts
server1 192.168.1.69
server2 192.168.1.70
开始
准备工作做好了,现在就可以开工了,首先在server1运行ssh-keygen,直接一路回车
[root@server1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d8:0c:6c:f8:44:a3:61:c3:53:5c:09:93:2d:1d:d1:82 root@server1
The key's randomart image is:
+--[ RSA 2048]----+
| .+o*B+= |
| .oBEo= . |
| o.=. . |
| + = |
| o S |
| |
| |
| |
| |
+-----------------+
切换到server2,同上。
回到server1,运行ssh-copy-id -i .ssh/id_rsa.pub root@server2
[root@server1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@server2
The authenticity of host 'server2 (192.168.1.70)' can't be established.
RSA key fingerprint is 99:3b:ea:56:48:73:07:08:79:1f:69:b7:f2:a8:01:5c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server2,192.168.1.70' (RSA) to the list of known hosts.
root@server2's password: #输入对方服务器root密码
Now try logging into the machine, with "ssh 'root@server2'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
切换到server2,运行ssh-copy-id -i .ssh/id_rsa.pub root@server1
现在,你就可以不需要输入对方服务器的密码而直接登录了