Google Authenticator (简体中文)
Google Authenticator 使用一次性密码(One-time Passcodes)进行两步验证。iOS、Android 甚至 Blackberry 上都提供了 OTP 生成器应用。与 S/KEY Authentication 类似,两步验证将集成在Linux的 PAM 系统中。此指南显示了此两步验证的安装与配置。
Contents
安装
安装 libpam-google-authenticator 软件包或开发者版本 google-authenticator-libpam-gitAUR。
设置插入式验证模块(Pluggable Authentication Modules)
通常远程登录才需要设置两步验证。PAM的配置是在文件/etc/pam.d/sshd
内。如果想全局使用谷歌两步身份验证,请小心的修改/etc/pam.d/sshd
,以免锁定自己从而不能登录。在本指南中,我们将在本地会话中安全的编辑文件/etc/pam.d/sshd
。
要同时输入 Unix 密码与两步验证码登录,请在 /etc/pam.d/sshd
文件的system-remote-login行之上添加pam_google_authenticator.so
:
auth required pam_google_authenticator.so auth include system-remote-login account include system-remote-login password include system-remote-login session include system-remote-login
这样将会首先询问两步验证码,验证成功后才会询问 Unix 密码。改变上述添加于/etc/pam.d/sshd
文件的配置顺序则会改变验证顺序。
要允许使用 Unix 密码或两步验证码登录,请修改:
auth sufficient pam_google_authenticator.so
在文件/etc/ssh/sshd_config
内开启质疑-应答认证(challenge-response authentication):
ChallengeResponseAuthentication yes
最后 重新加载 sshd
服务
生成密钥文件
每一个想要使用两步验证的用户需要在其用户目录生成一个密钥文件,使用命令google-authenticator来完成:
$ google-authenticator Do you want authentication tokens to be time-based (y/n) y <这里是自动生成的二维码> Your new secret key is: ZVZG5UZU4D7MY4DH (密钥) Your verification code is 269371 (验证码) Your emergency scratch codes are: (备用令牌码) 70058954 97277505 99684896 56514332 82717798 Do you want me to update your "/home/username/.google_authenticator" file (y/n) y 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 (是否拒绝多次使用使用相同的令牌?这将限制你每30s仅能登录一次,但会提醒/阻止中间人攻击。) 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分30秒增加到约4分钟?这将缓解时间同步问题。) If the computer that you are logging into is not 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 (是否启用此模块的登录频率限制,登录者将会被限制为最多在30秒内登录3次。)
建议您将备用令牌码保存在安全的地方,因为当丢失手机或其他原因不能使用两步验证器时,只能使用备用令牌码登录。它们同时也被保存在~/.google_authenticator
。
设置两步验证软件
从谷歌市场安装两步验证器Android market(如FreeOTP),或者安装F-Droid。在软件中创建一个新验证,输入密钥(如例子中的'ZVZG5UZU4D7MY4DH')或扫描二维码来导入密钥,并输入验证码。
软件现在应该会显示一个每30秒更新的验证码。
测试
连接SSH服务器:
$ ssh hostname login as: <username> Verification code: <令牌码/备用令牌码> Password: <password> $
存储位置
如果想要改变密钥存储位置,请使用--secret
参数:
$ google-authenticator --secret="/PATH_FOLDER/USERNAME"
然后更改/etc/pam.d/sshd
内的路径配置:
/etc/pam.d/sshd
auth required pam_google_authenticator.so user=root secret=/PATH_FOLDER/${USER}
user=root
用于强制PAM使用root用户权限来搜索文件。
另外请注意,密钥文件的所有者是root,生成文件的用户只能读取文件(chmod: 400
)。
$ chown root.root /PATH_FILE/SECRET_KEY_FILES chmod 400 /PATH_FILE/SECRET_KEY_FILES
用于桌面登陆
谷歌两步认证插件可以同时用于控制台与 GNOME 桌面登录。只需要在文件 /etc/pam.d/login
或 /etc/pam.d/gdm-password
内加入
auth required pam_google_authenticator.so