Linux服务器如何禁用密码登录,使用秘钥文件登录增加服务器安全性?

生成秘钥公钥
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
29
30
31
32
33
34
35
36
37
38
39
40
41
$  ssh-keygen       //生成秘钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
The key's randomart image is:
+---[RSA 2048]----+
| .o=B@*. |
| . = =o*.*. |
|. . + @ + o o |
|oo o * + |
|o +.. + S |
|.oo. . |
| =. |
|..=oE |
| *+ o. |
+----[SHA256]-----+



$ cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys //把公钥放进授权文件中去

$ cd /root/.ssh

$ chmod 600 authorized_keys

$ chmod 700 ~/.ssh

$ vim /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
PermitRootLogin yes //另外,请留意 root 用户能否通过 SSH 登录,默认为yes:

当我们完成全部设置并以密钥方式登录成功后,可以禁用密码登录。这里我们先不禁用,先允许密码登陆
PasswordAuthentication yes

$ service sshd restart //重启服务
客户端使用秘钥登录
1
2
3
1. /root/.ssh/id_rsa 到Windwos桌面 更改为.pem文件 
2.使用SecurtCRT SSH远程连接工具进行登录
3.登录成功后 请把/etc/ssh/sshd_config 文件中的PasswordAuthentication改为No 禁止密码进行远程登录 防止服务器被恶意SSH暴力破解攻击
服务器被攻击如图

相关截图