随笔- 188  评论- 13  文章- 0 

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

最近老是在群里看到许多同学问,linux之间无密码互访怎么弄啊,百度的看得我头晕之类的,所以我就写写怎么样在两台linux服务器之间实现无密码互访,也就是让它们互相信任的意思,废话不多说,直接上菜。

环境介绍

两台CentOS服务器,主机名和IP分别是: server1 192.168.1.69 server2 192.168.1.70

准备

  1. #两台都要安装
  2. yum install -y openssh-clients

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

  1. vim /etc/hosts
  2. server1 192.168.1.69
  3. server2 192.168.1.70

开始

准备工作做好了,现在就可以开工了,首先在server1运行ssh-keygen,直接一路回车

  1. [root@server1 ~]# 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. +-----------------+

切换到server2,同上。

回到server1,运行ssh-copy-id -i .ssh/id_rsa.pub root@server2

  1. [root@server1 ~]# 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.

切换到server2,运行ssh-copy-id -i .ssh/id_rsa.pub root@server1

现在,你就可以不需要输入对方服务器的密码而直接登录了,试试看吧 O(∩_∩)O

posted on 2016-08-19 13:56 用代码书写人生 阅读(...) 评论(...) 编辑 收藏