Linux系统之间实现ssh无需密码连接

简介:

 

有时候我们需要在Linux系统之间互相传输文件,正常登录系统是需要输入密码,但是在自动化执行的时候如果需要手动输入密码就比较麻烦了,如何实现无需密码,但是又保证系统的安全呢?这就使用到了密钥登录。

 

环境说明:

两台LinuxCentOS 6.7)系统:

balichos   192.168.171.50

balichvm  192.168.171.51

 

1)、分别在balichosbalichvm都安装openssh-clients,可以使用yuminstall openssh-clients.

 

2)、分别在balichosbalichvm上修改/etc/hosts的文件,加入balichosbalichvm对应的ip如下:

 192.168.171.50  balichos

 192.168.171.51  balichvm

 

3)、使用ssh-keygen生成公钥和私钥,这一步的操作分别在balichosbalichvm两台主机上操作。

 

[root@balichOS .ssh]# ssh-keygen -t rsa          #-t参数是指定加密的类型为rsa

Generating public/private rsa key pair.

Enter file in which to save the key(/root/.ssh/id_rsa):                             #密钥存放的路径

Enter passphrase (empty for no passphrase):                                            #不需要密码登录不用输入,直接enter

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:

96:09:8b:c7:ec:6a:f5:40:52:2f:d3:e3:e8:05:ed:21root@balichOS.org

The key's randomart image is:

+--[ RSA 2048]----+

|                 |

|     .          |

|    ..+         |

|   .+Eo*o       |

|   .o=OSo       |

|    o+.+        |

|    o.+         |

|   ... .        |

|  ..            |

+-----------------+

[root@balichOS .ssh]#

 

刚刚的操作也同样在balichvm上操作。

 

4)、把需要在登录的系统上加入自己的公钥,用自己的私钥作为连接。操作如下:

balichos上:

[root@balichOS~]# cd .ssh    

[root@balichOS.ssh]# cp id_rsa.pub authorized_keys

[root@balichOS.ssh]#scp authorized_keys root@balichvm:/root/.ssh/

 

 

balichvm上:

[root@balichvm~]# cd .ssh    

[root@balichvm.ssh]# cp id_rsa.pub authorized_keys

[root@balichvm.ssh]#scp authorized_keys root@balichos:/root/.ssh/

 

5)、分别在balichosbalichvm修改.ssh 700)和authorized_keys600)的权限

 

[root@balichOS.ssh]# chmod 600 authorized_keys   ##balichos上修改权限

 

[root@balichvm.ssh]# chmod 600 authorized_keys   ##balichvm上修改权限

 

以上步骤就可以实现两台主机之间访问不需要密码,如果想在客户端使用访问服务器, 而必须要使用密钥访问,可以执行如下的操作。



6)、分别在balichosbalichvm修改/etc/ssh/sshd_config配置文件修改如下:

 

PasswordAuthentication no        ##关闭密码登录

 

以下三行是默认有#注释的,将#号删除。

RSAAuthenticationyes            ##开启rsa认证

PubkeyAuthenticationyes

AuthorizedKeysFile      .ssh/authorized_keys             ##指定认证的公钥文件

 

7)、分别在balichosbalichvm重启sshd服务。

   service sshd restart

 

8)分别在balichosbalichvm测试连接。

[root@balichOS ~]#

[root@balichOS ~]# ssh balichvm     #登录到balichvm

Last login: Tue Oct 27 21:14:47 2015 frombalichos

 

[root@balichvm ~]# ssh balichos                  ##登录到balichos

Last login: Wed Oct 28 05:53:07 2015 frombalichvm

[root@balichOS ~]#

 

 

需要注意的问题:

在用户root/.ssh目录下,要保留私钥id_rsa,如果没有这个文件,是不能登录到对方的机器的;

 

假如需要使用诸如puttyxshell登录系统,需要把私钥id_rsa拷贝出来,在用户验证身份指定私钥id_rsa的文件;如有多对密码,authorized_keys里面的公钥的内容追加进去而不是覆盖。

 

基本的思路就是,公钥存放在需要被连接的机器上,私钥存放在连接的客户端上。私钥是用来解密的。




本文转自 巴利奇 51CTO博客,原文链接:http://blog.51cto.com/balich/1706981
相关文章
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 关闭PPP(Point-to-Point Protocol)连接 ppp-off命令 使用指南
【Shell 命令集合 网络通讯 】Linux 关闭PPP(Point-to-Point Protocol)连接 ppp-off命令 使用指南
40 1
|
27天前
|
存储 安全 Shell
【Shell 命令集合 系统设置 】⭐⭐⭐Linux 更改用户密码 passwd命令 使用指南
【Shell 命令集合 系统设置 】⭐⭐⭐Linux 更改用户密码 passwd命令 使用指南
35 0
|
28天前
|
安全 Shell Linux
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
34 0
|
9天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
19 1
|
9天前
|
关系型数据库 MySQL Linux
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
17 0
|
27天前
|
监控 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
54 1
|
27天前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 启动和管理SLIP 连接 sliplogin命令 使用指南
【Shell 命令集合 系统管理 】Linux 启动和管理SLIP 连接 sliplogin命令 使用指南
30 0
|
27天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 同步密码 pwconv命令 使用指南
【Shell 命令集合 系统设置 】Linux 同步密码 pwconv命令 使用指南
28 0
|
27天前
|
监控 Shell Linux
【Shell 命令集合 网络通讯 】Linux 拨号连接 dip命令 使用指南
【Shell 命令集合 网络通讯 】Linux 拨号连接 dip命令 使用指南
35 0
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 建立串行连接 cu命令 使用指南
【Shell 命令集合 网络通讯 】Linux 建立串行连接 cu命令 使用指南
28 0