用 Google Authenticator 加强 VPS 及 WordPress 甚至桌面电(2)

首先需要切换到对应的用户,如果 VPS 上只有一个用户的话,自然是可以省略这一步的,但是多用户的 VPS 需要先切换到对应的用户,再运行google-authenticator 命令,结果类似这样:

用 Google Authenticator 加强 VPS 及 WordPress 甚至桌面电

这个 QR 码自然是给 Google Authenticator 应用程序来扫描的,也可以访问上面的那个链接,用 Google Chart API 生成的 QR 码来扫描。还可以照着 QR 码下面的文字密钥手工输入。当 Google Authenticator 识别了这个账号之后,验证器就配置好了。在文字密钥下面还提供了几个应急码,为手机丢了等情况下所用的,可以妥善保管。

这时 Google Authenticator 虽然运行了,但是相关设置还没有保存,程序会问你Do you want me to update your "~/.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。下一个问题是

Bydefault, tokens are good for30 seconds andin 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)

大意是问是否打开时间容错以防止客户端与服务器时间相差太大导致认证失败。这个可以根据实际情况来。我的 Android 设备时间很准(与网络同步的),所以答 n,如果一些 Android 平板电脑不怎么连网的,可以答 y 以防止时间错误导致认证失败。再一个问题是

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。

问题答完了,家目录中多出一个 .google_authenticator 文件(默认权限为 400),这时客户端与服务端已经配套起来了,以后不用再运行google-authenticator 命令了,否则会重新生成一组密码。

配置 SSH 验证

此时虽然 Google Authenticator 已经配置好了,但是并没有任何程序会去调用它。所以需要设置 SSH 登录的时候去通过它验证。

打开 /etc/pam.d/sshd 文件,添加

auth required pam_google_authenticator.so

这一行,保存。再打开 /etc/ssh/sshd_config 文件,找到

ChallengeResponseAuthenticationno

把它改成

ChallengeResponseAuthentication yes

并保存。最后,输入

sudo service ssh restart

来重启 SSH 服务以应用新的配置。

这时候再用 SSH 登录的话就会这样了:

wzyboy@vermilion:~$ ssh root@natatio

Password:[输入密码]

Verification code:[输入验证码]

Welcome to Ubuntu11.04(GNU/Linux2.6.38-8-generic x86_64)

于是就这样成功了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/18544.html