ssh本身是一个非常安全的认证连接方式。不过由于人过等方面的原因,难免会造成密码的泄露。针对这种问题我们不妨给ssh再加一把锁。当然,增加这层锁的方式有很多种。例如:knockd、S/KEY、OPIE/OPTW、Two-factor authentication等(以上列举的几种方式中可能互相包含,有兴趣的可以去维基百科上查下,进而细分下)。而今天讲到的Google Authenticator就属于Two-factor authentication的代表。

一、安装

1、安装相关包

1yum -y install wget gcc make pam-devel libpng-devel

2、安装相关包

1wget http://fukuchi.org/works/qrencode/qrencode-3.4.1.tar.gz
2tar zxf qrencode-3.4.1.tar.gz
3cd qrencode-3.4.1
4./configure --prefix=/usr && make && make install

该软件为非必须包,其主要用于在终端下生成二维码用。

3、google authenticator PAM插件安装

该插件有两种安装方式,一种是通过下载tar.gz包安装,一种是通过git下载安装。

tar.gz包安装:

1wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
2tar jxf libpam-google-authenticator-1.0-source.tar.bz2
3cd libpam-google-authenticator-1.0
4make && make install

git下载安装:

1git clone https://code.google.com/p/google-authenticator/
2cd google-authenticator/libpam/
3make install

二、配置google-authenticator

安装完成后会在/lib64/security/目录生成pam_google_authenticator.so文件,如果是32位系统会在/lib/security/目录生成。除此之外,系统还会多在/usr/local/bin目录生成一个google-authenticator可执行文件,运行该命令。

 1[root@localhost /]# google-authenticator
 2Do you want authentication tokens to be time-based (y/n) y
 3https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@localhost.localdomain%3Fsecret%3DIO4SZL7YYHOTFNQJ
 4Your new secret key is: IO4SZL7YYHOTFNQJ
 5Your verification code is 236731
 6Your emergency scratch codes are:
 7  98144901
 8  30891929
 9  79811882
10  84230327
11  53670228
12Do you want me to update your "/root/.google_authenticator" file (y/n) y
13Do you want to disallow multiple uses of the same authentication
14token? This restricts you to one login about every 30s, but it increases
15your chances to notice or even prevent man-in-the-middle attacks (y/n) y
16By default, tokens are good for 30 seconds and in order to compensate for
17possible time-skew between the client and the server, we allow an extra
18token before and after the current time. If you experience problems with poor
19time synchronization, you can increase the window from its default
20size of 1:30min to about 4min. Do you want to do so (y/n) n
21If the computer that you are logging into isn't hardened against brute-force
22login attempts, you can enable rate-limiting for the authentication module.
23By default, this limits attackers to no more than 3 login attempts every 30s.
24Do you want to enable rate-limiting (y/n) y

注:

1、如果安装了qrencode包,会在出现google的链接后出一 个二维码图标。方便手持设备扫描。当然,没装该包,可以打开https开头的url,打开也是一个二维码图片,同qrencode在终端下生成的是一样的。 2、”Your emergency scratch codes are:”下面的五个码用于在取不到或错的验证码有错时,用于应急用的。不过每个只能用一次,不能重复使用。 3、第四处选择项是由于选择是否开启时间容错。如果客户 端设备具有很好的同时间服务器同步功能,建议此处选择n。如果客户端设备(手机、ipad等 )不具有很好的同ntp时间同步的功能,可以设为y —— 即开启时间容错。

三、ssh调用及客户端配置

1、sshd服务配置

按以上方式配置完成以后,是没有程序去调用的。想要ssh调用,还需要在pam.d认证上进行 配置。打开 /etc/pam.d/sshd 文件在第一行添加

1auth required pam_google_authenticator.so

再打开 /etc/ssh/sshd_config 文件

1ChallengeResponseAuthentication no
2把上面配置改成
3ChallengeResponseAuthentication yes

service ssh restart重启ssh服务生效。

2、客户端配置

获取30秒一次的动态码的客户端是浏览器(仅支持chrome、firefox)、Android设备、苹果IOS设备、Blackberry、WP手持设备。各自程序的下载地址为:

chrome google-authenticator插件

firefox google-authenticator插件

手机应用下载地址 (该手机链接提示支持symbian系统,不过经本人手机测试,发现应用安装完以后账户add没用反应)

以下以chrome下的插件为例,新增一个配置:

google-authenticator

帐户名可以随意添写,安全key即为刚刚运行google-authenticator命令后生成的,add增加 完成后。就会每过30秒新生成一个验证码。此时可以通过另外一台主机连接的方式如下:

1[root@localhost ~]# ssh root@192.168.10.14
2Verification code:
3Password:
4Last login: Fri Jan  4 16:27:57 2013 from 192.168.10.11
5[root@localhost ~]#

注:

1、如果命使用SecureCRT客户端登录时,需要在 连接——属 性——连接——ssh2——”鉴权/authenicaion” 中将 “键盘交互/keyboard Interactive” 调到第一 位。不然连接会出错。 2、以上安全配置对于ssh密钥认证方式进行的登录无效——即在pam配置中增加上面的配置后,通过密钥认证时不会要求输入验证码 。