首先明确,从userA通过ssh免密码登录到userB
1、在userA终端输入命令:ssh-keygen -t rsa
一路回车后出现下列信息:
wrf@wrf-virtual-machine:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/wrf/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/wrf/.ssh/id_rsa.
Your public key has been saved in /home/wrf/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:uOWi24pciEx7zFrnQFaiZqcmNZe9dzkVNCJUOa9mplQ wrf@wrf-virtual-machine
The key's randomart image is:
+---[RSA 2048]----+
| .o.o.o |
| .oo . |
| . . o. |
| . oo . E .. |
| =o+o o S. .. |
|=oO+ =. =o |
|o=.*..+.o=+ |
|o.+o+o o.. . |
| .o ++. |
+----[SHA256]-----+
此时进入.ssh目录会看到id_rsa.pub文件,说明密钥对已经生成
wrf@wrf-virtual-machine:~/.ssh$ ll
total 20
drwx------ 2 wrf wrf 4096 1月 21 09:58 ./
drwxr-xr-x 30 wrf wrf 4096 1月 19 11:49 ../
-rw------- 1 wrf wrf 1679 1月 21 09:58 id_rsa
-rw-r--r-- 1 wrf wrf 405 1月 21 09:58 id_rsa.pub
-rw-r--r-- 1 wrf wrf 666 1月 13 13:43 known_hosts
2、在userA终端输入命令:ssh-copy-id userB@192.168.1.112
注意:其中userB为有要登录的远端服务器@后面是远端服务器的ip地址
wrf@wrf-virtual-machine:~$ ssh-copy-id nvidia@192.168.1.112
/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
nvidia@192.168.1.112's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'nvidia@192.168.1.112'"
and check to make sure that only the key(s) you wanted were added.
3、使用ssh登录userB,发现已经不用密码登录,进入.ssh文件夹可以看到id_rsa.pub文件已经被copy到userB的.ssh目录下
nvidia@tegra-ubuntu:~$ ls -ll .ssh/
total 16
-rw------- 1 nvidia nvidia 2009 1月 17 12:34 authorized_keys
-rw------- 1 nvidia nvidia 1679 1月 17 12:19 id_rsa
-rw-r--r-- 1 nvidia nvidia 401 1月 17 12:19 id_rsa.pub
-rw-r--r-- 1 nvidia nvidia 444 1月 17 12:13 known_hosts