linux系统中ssh部署两台服务器远程免密登录
主机1:PC1,192.168.10.10
主机2:PC2,192.168.10.20
以下实现主要实现PC2远程免密登录PC1
1、测试PC2和PC1主机的连通性
[root@PC2 ~]# ifconfig | head -n 3 ## 查看PC2主机IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
[root@PC2 ~]# ping -c 3 192.168.10.10 ## 测试与 PC1连通性
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.234 ms
64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.226 ms
64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.191 ms
--- 192.168.10.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.191/0.217/0.234/0.018 ms
2、测试PC2直接远程登录PC1
[root@PC2 ~]# ssh root@192.168.10.10 ## 此时需要密码验证 root@192.168.10.10's password:
3、在PC2端生成秘钥和公钥对
[root@PC2 ~]# ssh-keygen ## 一路回车即可
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 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:
72:b5:42:a2:c4:f5:34:54:b8:fd:8c:2b:76:7f:5b:61 root@PC2
The key's randomart image is:
+--[ RSA 2048]----+
| ..+o. |
| . . o.. |
| o . oo. |
| . . o.... |
| . . S .+ E |
| o .. o . .|
| . .|
| o o .. |
| . o ..... |
+-----------------+
[root@PC2 ~]# ls -a /root/.ssh/ ## 生成的公钥和秘钥在这里
. .. id_rsa id_rsa.pub known_hosts
4、在PC2端将公钥传输至PC1
[root@PC2 ~]# ssh-copy-id 192.168.10.10
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.10's password: ## 此处输入PC1主机的root密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.10.10'"
and check to make sure that only the key(s) you wanted were added.
5、PC2端测试远程免密登录
[root@PC2 ~]# ssh root@192.168.10.10 ## 此时无需密码可直接登录
Last login: Wed Dec 2 16:39:39 2020
[root@PC1 ~]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe66:37f7 prefixlen 64 scopeid 0x20<link>
[root@PC1 ~]# exit ## 退出
logout
Connection to 192.168.10.10 closed.
以上实验实现了PC2主机远程免密登录PC1主机。