Linux上SSH登录远程服务器免密码

时间:2023-12-26 20:52:25

  在本地的客户端SSH到远程服务端时,每次都要输入用户名和密码,如果不想每次都输入密码则可以使用以下操作。

  首先在本地的客户端输入 ssh-keygen

[keysystem@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/keysystem/.ssh/id_rsa):
Created directory '/home/keysystem/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/keysystem/.ssh/id_rsa.
Your public key has been saved in /home/keysystem/.ssh/id_rsa.pub.
The key fingerprint is:
af:a4:d5:4d::3c:::ea:::0c::3b:ba:2b keysystem@localhost.localdomain
The key's randomart image is:
+--[ RSA ]----+
| . +o. |
| + o . |
| o . = |
| . + + . |
| S . X . |
| o = = |
| o o o |
| + .E . |
| . . .. |
+-----------------+
[keysystem@localhost ~]$

生成公私钥对,可以进入到~/.ssh目录下查看生成的公私钥对。

[keysystem@localhost ~]$ cd ~/.ssh/
[keysystem@localhost .ssh]$ ll -rlt
total
-rw-r--r--. keysystem keysystem Dec : id_rsa.pub
-rw-------. keysystem keysystem Dec : id_rsa
[keysystem@localhost .ssh]$

然后执行 ssh-copy-id dzy@172.20.16.42,其中dzy为远程主机的用户名,172.20.16.42为远程服务器的IP地址。

[keysystem@localhost ~]$ ssh-copy-id dzy@172.20.14.42