实现linux服务器之间无密码互访

原创
2015/04/08 14:04
阅读数 40

一、环境介绍

两台CentOS服务器,主机名和IP分别是:
hmoserver 192.168.10.89
hmoserver 192.168.10.41

二、准备

两台服务器上分别安装openssh-clients

yum install -y openssh-clients

三、修改hosts文件

分别打开两台服务器的/etc/hosts文件,在文件末尾添加以下内容,让主机名和IP对应(别忘了修改/etc/sysconfig/network文件里面的HOSTNAME,改成对应的服务器主机名即可)。

vim /etc/hosts

hmoserver 192.168.10.89

hmoserver 192.168.10.41

四、开始

首先在192.168.10.89服务器上运行ssh-keygen,然后一路回车

  1. [root@hmoserver ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. d8:0c:6c:f8:44:a3:61:c3:53:5c:09:93:2d:1d:d1:82 root@server1
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | .+o*B+= |
  14. | .oBEo= . |
  15. | o.=. . |
  16. | + = |
  17. | o S |
  18. | |
  19. | |
  20. | |
  21. | |
  22. +-----------------+
切换到192.168.10.41同上。

回到192.168.10.89上运行 :ssh-copy-id -i .ssh/id_rsa.pub root@192.168.10.41

  1. [root@hmoserver ~]# ssh-copy-id -i .ssh/id_rsa.pub root@server2
  2. The authenticity of host 'server2 (192.168.1.70)' can't be established.
  3. RSA key fingerprint is 99:3b:ea:56:48:73:07:08:79:1f:69:b7:f2:a8:01:5c.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added 'server2,192.168.1.70' (RSA) to the list of known hosts.
  6. root@server2's password: #输入对方服务器root密码
  7. Now try logging into the machine, with "ssh 'root@server2'", and check in:
  8.  
  9. .ssh/authorized_keys
  10.  
  11. to make sure we haven't added extra keys that you weren't expecting.
切换到 192.168.10.41 ,运行 ssh-copy-id -i .ssh/id_rsa.pub root@192.168.10.89

现在你就可以不需要输入对方服务器密码的登录了!!





展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部