Linux添加Google验证
PAM(Pluggable Authentication Modules)是Linux系统上用户对用户进行身份验证对身份验证基础结构。
Google Autherticator或其他Authy程序兼容Google设计的TOTP的PAM。
本文基于Debian 11本地会话进行配置,
如果您通过远程会话进行配置,请确保时刻有一个ssh连接,避免在配置后无法登录ssh服务器的情况
安装Google-PAM
Arch
Debian
1 | apt update |
Centos
为用户生成身份验证令牌
运行身份验证程序。
1 | google-authenticator |
1 | Do you want authentication tokens to be time-based (y/n) y |
配置OpenSSH
1 | vim /etc/pam.d/sshd |
在最后添加以下内容
1 | # Standard Un*x password updating. |
nullok的内容表明是OATH-TOTP令牌登录是可选的、没有该令牌用密码/密钥也能登录。我这边是强制使用。
1 | vim /etc/ssh/sshd_config |
找到 ChallengeResponseAuthentication 并设置为 yes
保存并关闭文件,重新启动 SSH 来重新加载配置文件。
1 | systemctl restart sshd.service |
配置两步验证
1 | vim /etc/ssh/sshd_config |
需要确保UsePAM
开启
添加以下内容
表明连接服务器需要有publickey+用户密码 或者是有publickey+OAUTH令牌
1 | AuthenticationMethods publickey,password publickey,keyboard-interactive |
这里当然也可以改成其他的。比如修改成只允许OAUTH令牌、要求publickey+OAUTH令牌等。
1 | vim /etc/pam.d/sshd |
注释掉@include common-auth
告诉服务器SSH不需要提示输入密码。如果需要用三种方式来验证就不需要注释该行
1 | #@include common-auth |
other
通过观察/etc/pam.d/其他文件,也可以发现有其他的配置文件。比如sudo,,
我们也可以为sudo添加OAUTH认证,只需要在/etc/pam.d/sudo
下添加
1 | auth required pam_google_authenticator.so |
同时 /etc/sudoers
文件的用户不能有NOPASSWD选项否则该认证就不会生效。
相应的,如果在su配置文件下添加
1 | auth required pam_google_authenticator |
那么在a用户切换b用户时,同样需要使用b用户的OAUTH一次性密码才能登录。
试了下passwd好像不能直接这样改,,有空得研究一下。。