0%

ssh客户端无需输入密码登录服务器

原来通过ssh client登录服务器时:

1
2
$ ssh root@192.168.1.100
root@192.168.1.100's password:

是需要登录用户输入密码的,如果密码强度设置得比较高的话,还是不太容易记忆的。每次登录前都得把密码记录翻出来看看,工作效率不是很高。

为了提高效率,我们可以在本地(client端)生成一个私钥,一个公钥。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ cd ~/.ssh

$ ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/simon/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/simon/.ssh/id_rsa.
Your public key has been saved in /Users/simon/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:aleqVoG5ecZ*****************qWkWaO0BTRG8 simon@Simon.local
The key's randomart image is:
+---[RSA 2048]----+
| ++. |
| B o.+ |
| * @ BEo |
| = O O.= o |
| . + XSB.o |
| o +.@o. |
| o o*oo |
| . .oo |
| .. |
+----[SHA256]-----+

$ ls
config id_rsa.pub
id_rsa known_hosts

然后登录到远程服务器,在~/.ssh目录下新建authorized_keys文件。

1
$ touch ~/.ssh/authorized_keys

把公钥(client端)的~/.ssh/id_rsa.pub文件的内容复制到服务器的.ssh/authorized_keys里面保存着。

这时,当你通过ssh root@192.168.1.100登录服务器时,就已经不需要输入密码了。