Centos服务器安全配置SSH使用Google Authenticator二次验证

CentOS安装所需组件:

yum -y install mercurial pam-devel

安装Google Authenticator:

#http://www.haiyun.me
wget --no-check-certificate https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
tar jxvf libpam-google-authenticator-1.0-source.tar.bz2
cd libpam-google-authenticator-1.0
make
make install

SSH登录时调用google-authenticator模块,编辑:安装Google Authenticator:

/etc/pam.d/sshd

第一行添加:

auth       required     pam_google_authenticator.so

修改SSH配置文件:第一行添加:

vim /etc/ssh/sshd_config

添加或修改以下内容:

ChallengeResponseAuthentication yes
UsePAM yes

重启SSH:添加或修改以下内容:

/etc/init.d/sshd restart

生成google-authenticator配置,运行:google-authenticator

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/AD2C2SY4Y2BK7EIJ%3Fsecret%3D7RX6O7Q7AXYPJNHO
Your new secret key is: 7RX6O7Q7AXYPJNHO
Your verification code is 093599
Your emergency scratch codes are:
 97632952
 24402162
 85433022
 49344079
 65373589
#上面几行数字为应急码
Do you want me to update your "/root/.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
#禁止一个口令多用
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
#客户端与服务器时间误差
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
#次数限制

Android安装google-authenticator客户端,扫描添加上方网址的二维码,以后在登录服务器时输入账号密码的同时还需输入google-authenticator即时生成的验证码才能登录。

ssh 192.168.1.1
Verification code:
Password:

多台机器使用相同的code验证可以先部署一台机器后把生成配置文件拷贝到另一台并且更改权限为400重启sshd就可以使用相同的code码验证了

-r--------   1 root   root      192 Oct 28 10:41 .google_authenticator
-r--------   1 root   root      183 Oct 28 10:22 .google_authenticator-bak
/etc/init.d/sshd restart

blob.png

转载请注明出处 枫叶落 » Centos服务器安全配置SSH使用Google Authenticator二次验证