为Linux开启两步验证

今天又想折腾了,于是着手试了一遍为服务器开启两步验证增强安全性。话不多说,实战开始。

  • 安装依赖包
1
yum install pam-devel gcc-c++ make -y
  • 下载Google-authentication源码,并安装

墙内:百度盘

墙外:谷歌地址

解压

1
tar xjf libpam-google-authenticator-1.0-source.tar.bz2

进入源码目录并安装

1
2
cd libpam-google-authenticator-1.0
make && make install

 

  • 生成验证密钥

运行google-authenticator,你可以根据实际情况输入y或者n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
google-authenticator

Do you want authentication tokens to be time-based (y/n) y
#你是否要生成基于时间的认证口令?

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@AY140528171537Z%3Fsecret%3D2YA3XXXXXXXRCEIQ

Your new secret key is: 2YA3XXXXXXXRCEIQ
Your verification code is 17XXX4
Your emergency scratch codes are:
51XXXX25
93XXXX45
87XXXX39
98XXXX31
15XXXX83
#这五个是紧急状态使用的验证码,谨当无法获取验证码时使用,注意这些紧急验证码用一次就少一个,所以这几个紧急验证码一定要保存好

Do you want me to update your "/root/.google_authenticator" file (y/n) y
#你希望我更新你的“~/.google_authenticator”文件吗(y/n)?

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
#你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒,但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?y

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) y
#默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题,可以将窗口从默认大小即1分30秒加大到约4分。你希望这么做吗(y/n)?

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
#如果你登录的那台计算机没有加固,以防范暴力登录,可以对验证模块启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。你希望启用尝试次数限制吗(y/n)?

 

  • 配置ssh使用两步验证模块

1.编辑/etc/pam.d/sshd,将下面的内容添加进去

1
2
vim /etc/pam.d/sshd
auth required pam_google_authenticator.so

2.编辑/etc/ssh/sshd_config,将ChallengeResponseAuthentication no改为ChallengeResponseAuthentication yes,并重启sshd服务
 

  • 安装、配置谷歌身份验证器

Android:墙内:http://shouji.baidu.com/soft/item?docid=3825924 墙外:https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2

iOS:https://itunes.apple.com/gb/app/google-authenticator/id388497605

Windows Phone:http://www.windowsphone.com/en-gb/store/app/authenticator/e7994dbc-2336-4950-91ba-ca22d653759b

Chrome GAuth Authenticator插件:https://chrome.google.com/webstore/detail/gauth-authenticator/jcmgkikfgdbehjdajjdnebnnmmknfblm

Firefox GAuth Authenticator插件:https://marketplace.firefox.com/app/gauth-authenticator/

验证器的配置,这里以Android的APP为例:

打开身份验证器,点击开始设置。在此我们可以“输入提供的密钥”,其中名称可以随意定义,重要的密钥处需要输入上面我们运行google-authenticator时生成的secret key。若当初没有保存,可以查看根目录下的.google_authenticator文件。添加完成之后该软件就会每隔30秒刷新一次验证码。
 

  • 登陆服务器验证

打开putty,输入IP和端口,打开该会话。成功登陆会显示如下输入顺序

即先提示输入身份验证器APP的Verification code验证码,然后再输入Linux的密码。

若显示Using keyboard-interactive authentication却只有Password提示

说明SELinux为开启状态。因为SELinux会阻止sshd向用户根目录的~/.google_authenticator文件进行任何操作,偷懒的解决方式就是关闭SELinux。

 

注:SSH登陆时的验证步骤为密钥/公钥验证→验证码验证→密码验证,所以在本机有密钥时,会直接登陆服务器,不会触发验证码验证。