Linux上用ssh-keygen和ssh-copy-id实现SSH免密码登录

一、生成公钥私钥对ssh-keygen

[woods@localhost ~]$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/woods/.ssh/id_rsa): 
Created directory '/home/woods/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/woods/.ssh/id_rsa.
Your public key has been saved in /home/woods/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:y66WJQDJNVsCwiwo5ccVkcopSEfLU7b3HnpLLOAkFWA [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|=o+E=+=+         |
|+**o+*+          |
|+.o*+= .         |
|. ..B . .        |
|   o +  So       |
|    + o.=..      |
|     . *o=       |
|      o.+ .      |
|     .....       |
+----[SHA256]-----+
[woods@localhost ~]$ 

二、用ssh-copy-id user@id将公钥复制到远程机器中

[woods@localhost ~]$ ssh-copy-id [email protected]
The authenticity of host '192.168.50.127 (192.168.50.127)' can't be established.
ECDSA key fingerprint is SHA256:M4bxlFwA62g6nL8jrEQH8FG40Mhd+N0EKoNxMEB9l0c.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[woods@localhost ~]$ 

三、用ssh user@id登录验证

[woods@localhost ~]$ ssh [email protected]
Activate the web console with: systemctl enable --now cockpit.socket

[root@localhost ~]# ip a
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:98:c9:c0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.50.127/24 brd 192.168.50.255 scope global dynamic noprefixroute ens33
       valid_lft 84134sec preferred_lft 84134sec
    inet6 fe80::71b5:62cc:374f:1625/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:60:9f:c4 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic:  mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:60:9f:c4 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# 

你可能感兴趣的:(Linux上用ssh-keygen和ssh-copy-id实现SSH免密码登录)