[OLD] 为 Debian 9 的 SSH 登录启用 Google Authenticator 两步验证

注意

本文的内容已经合并,失效,或已不具有参考价值,请查看给出的重定向链接。

15 秒后自动重定向至: Ubuntu (Debian) 服务器的初始化配置


本文介绍如何在 Debian 9 上启用对于 SSH 登录的两步验证,2FA 使用 Google Authenticator 。示例命令以 root 用户身份运行。


安装 Google Authenticator

运行命令:

1
2
apt update
apt install libpam-google-authenticator -y

为当前用户配置 Google Authenticator

运行:

1
google-authenticator

将出现一些问题:

1
Do you want authentication tokens to be time-based (y/n)

y

回答完此问题后,会出现二维码、Key,还有备用码,用手机的 Google Authenticator 扫描二维码或手动键入 Key,即可添加二步验证码,另外保存好备用码到一个安全的地方,以防手机丢失。

1
Do you want me to update your "~/.google_authenticator" file (y/n)

y

1
Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) 

y

1
By default, tokens are good for 30 seconds and in order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. If you experience problems with poor time synchronization, you can increase the window from its default size of 1:30min to about 4min. Do you want to do so (y/n)

n

1
If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n)

y

配置 OpenSSH

编辑 sshd 文件:

1
vim /etc/pam.d/sshd

注释掉 @include common-auth 一行,结果如下所示:

/etc/pam.d/sshd
1
2
# Standard Un*x authentication.
#@include common-auth

在此文件末尾添加一行:

/etc/pam.d/sshd
1
auth required pam_google_authenticator.so

保存并退出。

编辑 sshd_config 文件:

1
vim /etc/ssh/sshd_config

找到 ChallengeResponseAuthentication 将其值改为 yes ,结果如下所示:

/etc/ssh/sshd_config
1
2
3
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes

在此文件末尾添加一行:

/etc/ssh/sshd_config
1
AuthenticationMethods publickey,password publickey,keyboard-interactive

保存并退出。

重启 sshd 服务:

1
systemctl restart sshd

尝试登录

1
ssh user@serverip

显示:

1
Verification code:

此后输入二步验证码,回车,会再次提示输入密码(password),或直接登录进入(publickey)。

至此,已在 Debian 9 上成功启用 SSH 的二步验证。如有问题欢迎留言交流。

Mastodon