天天看点

用Google Authenticator加强SSH登录安全性

环境:

CentOS 6.3 x86_64

基础组件安装:

1

<code>yum -y </code><code>install</code> <code>wget gcc </code><code>make</code> <code>pam-devel libpng-devel</code>

一、安装qrencode

在Linux上,有一个名为 QrenCode 的命令行工具可以很容易帮我们生成二维码,google authenticator命令行生成二维码就是调用它。

2

3

4

<code>wget http:</code><code>//fukuchi</code><code>.org</code><code>/works/qrencode/qrencode-3</code><code>.3.1.</code><code>tar</code><code>.gz</code>

<code>tar</code> <code>zxf qrencode-3.3.1.</code><code>tar</code><code>.gz</code>

<code>cd</code> <code>qrencode-3.3.1</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr</code> <code>&amp;&amp; </code><code>make</code> <code>&amp;&amp; </code><code>make</code> <code>install</code>

二、安装google authenticator PAM插件

<code>wget http:</code><code>//google-authenticator</code><code>.googlecode.com</code><code>/files/libpam-google-authenticator-1</code><code>.0-</code><code>source</code><code>.</code><code>tar</code><code>.bz2</code>

<code>tar</code> <code>jxf libpam-google-authenticator-1.0-</code><code>source</code><code>.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>libpam-google-authenticator-1.0</code>

<code>make</code> <code>&amp;&amp; </code><code>make</code> <code>install</code>

三、配置google authenticator

Google Authenticator 其实是一套开源的解决方案,所以不仅在 Google 的网站上能用,在其他地方也能用的。然而,在 Google 的网站上,会直接给你一个 QR 码让你扫的,而自己配置的 Google Authenticator 则要自己生成了。

首先需要切换到对应的用户,如果 VPS 上只有一个用户的话,自然是可以省略这一步的,但是多用户的 VPS 需要先切换到对应的用户,再运行 google-authenticator 命令,程序会问你Do you want authentication tokens to be time-based (y/n),大意是基于时间生成验证码(及TOTP),这里选择y。结果类似这样:

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

这时 Google Authenticator 虽然运行了,但是相关设置还没有保存,程序会问你 Do you want me to update your “/root/.google_authenticator” file (y/n) (是否将配置写入家目录的配置文件),当然是回答 y 了。又会问

大意是说是否禁止一个口令多用,自然也是答 y。下一个问题是

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

选择是否打开尝试次数限制(防止暴力攻击),自然答 y。

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

四、配置SSH验证

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

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

auth required pam_google_authenticator.so

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

ChallengeResponseAuthentication no

把它改成

ChallengeResponseAuthentication yes

并保存。最后,输入

service ssh restart

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

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

这样就成功了。

     本文转自ljl_19880709 51CTO博客,原文链接:http://blog.51cto.com/luojianlong/1381725,如需转载请自行联系原作者

继续阅读