Linux主机之间ssh免密登录配置

原创
2017/08/13 14:14
阅读数 1.3W

修改ssh配置

每台linux主机修改配置文件/etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

启用这三项配置,即去掉这三项的注释

 

配置spiro1主机到spiro2主机的免密登录

hosts配置

前置条件,主机名为spiro1和spiro2

先配置两台主机的/etc/hosts

192.168.231.101 spiro1
192.168.231.102 spiro2

生成公私钥对

在spiro1机器上执行

[root@spiro1 .ssh]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
1c:04:2f:b3:dd:32:ef:80:12:9c:e3:ee:22:35:bc:c1 root@spiro1
The key's randomart image is:
+--[ RSA 2048]----+
|      ...        |
|       o         |
|      o o        |
|   . . * o       |
| o  = . S .      |
|  E. o . +       |
| . +o . . .      |
|. o. .   o       |
| . oo     .      |
+-----------------+

在/root/.ssh目录下生成

[root@spiro1 .ssh]# ll
total 8
-rw-------. 1 root root 1675 Aug 12 22:35 id_rsa
-rw-r--r--. 1 root root  393 Aug 12 22:35 id_rsa.pub

id_rsa: 私钥

id_rsa.pub: 公钥

安装公钥到登录目标主机

[root@spiro1 .ssh]# ssh-copy-id -i id_rsa.pub root@spiro2
root@spiro2's password:
Now try logging into the machine, with "ssh 'root@spiro2'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

到spiro2主机上检查

[root@spiro2 .ssh]# pwd
/root/.ssh
[root@spiro2 .ssh]# cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1+gaNkgPTQ9dDgz3bj9Z9QfLrEt/6Qp3zwVhOtJi5blIfKXct0H0e+0FPttG6LeeWKZ00u/KkUfU9a7q3xwBHoujq4nHa9c+xbzIVeAnst7HP9Kl7tgfyUjaC1mg4CPViqt65aiE9dVy8f1pPeIQ9kJhtJFRbDsj8vgMX6oou8DGrj2AsnPWAVOxY06HNxWCma/2+GmnfwVOfWj6d89FBcvnMST84ldc1to/VXyhmftKTHJ62nr0KA+VzTKs4xTrrSANE5UZ/vR3DEZglehNjedcoaaWb/2zJ6l0D+SS5Z/3iI+6DvJSRHgVGEuwluW6djcifSZeCIYmBEj2M19BVQ== root@spiro1

测试

[root@spiro1 .ssh]# ssh spiro2
Last login: Sat Aug 12 23:09:09 2017 from spiro1
[root@spiro2 ~]#

免密登录成功。

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