我遵循了digitalocean的这本不错的指南,在Ubuntu 16.04服务器上为SSH设置了Multi-Factor Authentication(无UI(,
在此之后,当通过ssh系统登录时,系统上的每个用户都希望获得额外的身份验证,如果设置了2fa,一切都会正常工作,但如果没有设置,它就会失败(文章确实提到,如果我将nullok
留在sshd中,如果没有设置的话,它将继续运行,而不会期望得到2fa,但事实并非如此。(
基本上,我想为系统上的每个交互式用户设置2fa,但我不希望在root
用户上启用它。
顺便说一句,我确实试过https://askubuntu.com/a/1051973/867525,它确实适用于普通用户,但不适用于root用户。
一旦你在Ubuntu上为SSH设置了Google Two-Factor,所有使用";"密码认证";,但是你可以让基于密钥的身份验证优先(发生在密码身份验证之前。你可以为那些你不想在你的情况下使用2因素的用户设置
您可以继续,只需在sshd_config文件中指定对所有用户禁用基于密钥的身份验证:
PubkeyAuthentication no
并且仅通过root为特定用户启用,在您的情况下为
Match User root
PubkeyAuthentication yes
作为一个额外的安全提示,您可以只允许特定用户使用
AllowUsers <username> <username2>
基于Dieskim的上述回答,这就是我所做的,不将PAM/2fa应用于root用户。
在/etc/ssh/sshd_config中,我有条件地添加了AuthenticationMethods publickey,password publickey,keyboard-interactive
,
UsePAM yes
# Apply following rule to everyone (*) but root user (!root)
Match user "!root,*"
AuthenticationMethods publickey,password publickey,keyboard-interactive
Match all
我还想提及以下内容:
最初,即使在/etc/pam.d/sshd
中有auth required pam_google_authenticator.so nullok
根登录不起作用,原因是sshd_config中的PermitRootLogin
被设置为prohibit-password
,OpenSSH需要允许根登录使用PAM验证的密码来使用任何类型的密码,包括OTP
由于我想要PermitRootLogin
到no
,上面的Match user
解决方案运行良好。
关于auth required pam_google_authenticator.so nullok
的另一件事,这里认为nullok
意味着如果没有为用户配置2fa,用户仍然可以登录,但最新版本的libpam-google-authenticator
并非如此
# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so nullok
编辑:以下语句不正确(已从上面的答案中删除(,当您可选地启用AuthenticationMethods时,请不要在sshd文件中添加auth required pam_permit.so
。
将
auth required pam_permit.so
添加为/etc/pam.d/sshd
的最后一行很重要
还测试您的任何用户是否无法使用任何凭据登录。