两台CentOS7:

    youxi1  192.168.2.103

    youxi2  192.168.2.104


[root@localhost ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):  

]Created directory '/root/.ssh'.

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:

SHA256:s3UfKpok75/reD0KNwOi/M2nonkALSVbXr1foBG7rB0 root@localhost.localdomain

The key's randomart image is:

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

|       ...       |

|  . o . o..      |

|   B .  .+ .     |

|  + o  .o.  .    |

|   o  . E.... .  |

|   ... + *.. o . |

|    o.o = =.. .  |

|     oo* *o=o    |

|    oo.oX**o .   |

+----[SHA256]-----+

[root@localhost ~]# ls /root/.ssh/

id_rsa  id_rsa.pub

接着使用命令ssh-copy-id命令将公钥发到192.168.2.104服务器上

​[root@localhost ~]#  ssh-copy-id -i .ssh/id_rsa.pub  root@192.168.2.104

​/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"


/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.2.104's password:  


Number of key(s) added: 1


Now try logging into the machine, with:   "ssh 'root@192.168.2.104'"

and check to make sure that only the key(s) you wanted were added.​

​[root@localhost ~]# ssh root@192.168.2.104

Last login: Mon Oct  4 01:20:33 2021 from 192.168.2.103

[root@localhost ~]# ip addr

​1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

      valid_lft forever preferred_lft forever

   inet6 ::1/128 scope host  

      valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

   link/ether 00:0c:29:55:b1:20 brd ff:ff:ff:ff:ff:ff

   inet 192.168.2.104/24 brd 192.168.2.255 scope global noprefixroute dynamic ens33

      valid_lft 6313sec preferred_lft 6313sec

   inet6 fe80::b92a:baa7:4873:a720/64 scope link tentative noprefixroute dadfailed  

      valid_lft forever preferred_lft forever

   inet6 fe80::90dc:b1a9:5062:45c1/64 scope link tentative noprefixroute dadfailed  

      valid_lft forever preferred_lft forever

   inet6 fe80::e3cb:dc5:77b8:7e6a/64 scope link noprefixroute  

      valid_lft forever preferred_lft forever

[root@localhost ~]# exit

登出

Connection to 192.168.2.104 closed.

[root@localhost ~]#