提高 Linux 系统的 SSH 安全性

更改 sshd 默认端口

编辑 /etc/ssh/sshd_config,修改 Port 为高位、不常用端口号。

1
2
- #Port 22
+ Port 11451

重启 sshd

1
sudo systemctl restart sshd

禁用 root 用户,使用普通用户

新建新用户

1
sudo adduser lixx
sudo adduser lixx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
root@lixx:~# sudo adduser lixx
Adding user `lixx' ...
Adding new group `lixx' (1000) ...
Adding new user `lixx' (1000) with group `lixx (1000)' ...
Creating home directory `/home/lixx' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for lixx
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
Adding new user `lixx' to supplemental / extra groups `users' ...
Adding user `lixx' to group `users' ...
root@lixx:~#

为新用户授予 sudo 权限

1
sudo usermod -aG sudo lixx

禁止 root 通过 ssh 登录

编辑 /etc/ssh/sshd_config,修改 PermitRootLogin 值为 no

PermitRootLogin no

重启 sshd

1
sudo systemctl restart sshd

使用密钥对登录。禁止使用密码登录

生成 ssh 密钥对

1
2
3
4
5
ssh-keygen -t ed25519 -f ~/.ssh/lixx_ed25519 -C "lixx_ed25519-20240810"

# -t 密钥类型
# -f 输出的密钥文件
# -C 自定义的标注

生成的密钥文件应当妥善保管。

上传公钥到服务器

将生成的公钥(~/.ssh/lixx_ed25519.pub)的文本内容复制粘贴到服务器的 ~/.ssh/authorized_keys 文件中。

禁止使用密码登录

编辑 /etc/ssh/sshd_config,修改 PasswordAuthentication 值为 no,禁用密码认证

PasswordAuthentication no
PermitEmptyPasswords no

重启 sshd

1
sudo systemctl restart sshd
Built with Hugo
Theme Stack designed by Jimmy