Administrator
发布于 2025-05-10 / 10 阅读
0
0

【新教程】Linux服务器ssh启用两步验证

1 背景

服务器被恶意破解的事件层出不穷,一旦被破解就比较麻烦。不如提前通过简单的措施——增加两步验证,来大大增强服务器的安全性。本教程在Debian 12.5、Ubuntu 24.04等系统上测试通过。

2 详细过程

1、安装 libpam-google-authenticator

sudo apt update
sudo apt install libpam-google-authenticator -y

2、为每个用户生成 TOTP 密钥

google-authenticator

3、回答显示的问题,默认y就可以。
4、使用微软或者谷歌验证器扫码。
5、拍照记录应急验证码。
6、配置 PAM 模块启用 TOTP

sudo vim /etc/pam.d/sshd

在文件最上方添加以下内容:

auth required pam_google_authenticator.so nullok

如果必须使用两步验证,则:

auth required pam_google_authenticator.so

上述区别在于,如果没有配置验证器,还能否登录。前者如果没有配置验证器还可以登录,后者就无法登陆了,必须配置验证器。
7、编辑ssh配置。

sudo vim /etc/ssh/sshd_config

修改以下内容:

ChallengeResponseAuthentication yes
UsePAM yes
PasswordAuthentication yes  # 如使用公钥登录可为 no
KbdInteractiveAuthentication yes  # 这一行必不可少,否则不能使用xshell,termius等

8、重启ssh服务,使上述配置生效。

sudo systemctl restart ssh

3 说明

在配置完成后,之后每次登录都需要先输入验证码,然后再输入密码。
如果按照上述步骤不能正常登录,请先检查使用的ssh软件,使用xshell或者termius甚至使用系统自带的终端,看能否顺利登录


评论