快速建立ssh互信(转)

转自:魏巍的Linux酒吧 - 51CTO技术博客(http://weiweilinux.blog.51cto.com/3349074/1048212

快速建立ssh互信

    因为工作中经常需要配置服务器之间的ssh互信.在网上找到好多关于ssh互信的配置方法,大多很麻烦,而且配置起来经常不成功.

    下面为介绍一种可以快速建立ssh互信的方法:

实验背景:

使用的系统为CentOS 6.0

两台服务器:

服务器A :地址192.168.26.134 新建用户redhat 密码: redhat ;

服务器B :地址192.168.26.135 新建用户centos 密码: centos ;

(出于安全考虑一般不配置root用户的ssh互信)

配置过程

----------------------------------------------------------------------------

一.配置服务器A:

1.用redhat用户登录系统.或者su 到redhat用户.

2.生成私钥和公钥文件

  1. ssh-keygen -t rsa 

(注:这里不需要事先在家目录下建立 .ssh 文件夹)

回车后会显示如下内容:  (之后直接回车就可以了)

  1. Generating public/private rsa key pair. 
  2. Enter file in which to save the key (/home/redhat/.ssh/id_rsa):(回车) 
  3. Created directory '/home/redhat/.ssh'. //自动建立 .ssh文件夹 
  4. Enter passphrase (empty for no passphrase): (回车) 
  5. Enter same passphrase again: (回车) 

显示类似如下内容则表明私,公钥文件生成成功

 

3.将公钥拷贝到服务器B上

  1. ssh-copy-id -i .ssh/id_rsa.pub centos@192.168.26.135 

出现如下内容:

  1. The authenticity of host '192.168.26.135 (192.168.26.135)' can't be established. 
  2. RSA key fingerprint is 50:8b:93:c4:98:a9:ca:3d:44:68:9d:71:73:64:53:3e. 
  3. Are you sure you want to continue connecting (yes/no)? (这里输入yes) 

之后会让你输入服务器B上的用户的密码

  1. Warning: Permanently added '192.168.26.135' (RSA) to the list of known hosts. 
  2. centos@192.168.26.135's password: (输入centos用户的密码centos) 

最后显示如下内容表示拷贝成功:

 

 

4.验证ssh时是否还需要输入密码:

  1. ssh centos@192.168.26.135 -- 'whoami' 

  1. ssh-copy-id -i .ssh/id_rsa.pub redhat@192.168.26.134 

一切正常的话会直接显示centos用户的用户名

--------------------------------------------------------------------------- 

二.配置服务器B:

配置过程跟配置服务器A相同,这里简化描述,只描述步骤与命令.

1.登陆服务器B 

2.生成密钥文件

  1. ssh-keygen -t rsa   

3.拷贝公钥文件服务器A

  1. ssh-copy-id -i .ssh/id_rsa.pub redhat@192.168.26.134 

4.验证

  1. ssh redhat@192.168.26.134 -- 'whoami' 

--------------------------------------------------------------------------

总结.到此服务器之间的ssh就建立好了.总的来说建立互信只需要执行两条命令

  1. ssh-keygen -t rsa   
  2. ssh-copy-id -i .ssh/(公钥文件,以.pub结尾的文件) USER@(服务器地址) 

而不需要建立目录,修改权限,修改文件.

原文地址:https://www.cnblogs.com/Aiapple/p/6475238.html