为Linux开启两步验证
今天又想折腾了,于是着手试了一遍为服务器开启两步验证增强安全性。话不多说,实战开始。
- 安装依赖包
1 | yum install pam-devel gcc-c++ make -y |
- 下载Google-authentication源码,并安装
墙内:百度盘
墙外:谷歌地址
解压
1 | tar xjf libpam-google-authenticator-1.0-source.tar.bz2 |
进入源码目录并安装
1 | cd libpam-google-authenticator-1.0 |
- 生成验证密钥
运行google-authenticator,你可以根据实际情况输入y或者n
1 | google-authenticator |
- 配置ssh使用两步验证模块
1.编辑/etc/pam.d/sshd,将下面的内容添加进去
1 | vim /etc/pam.d/sshd |
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登陆时的验证步骤为密钥/公钥验证→验证码验证→密码验证,所以在本机有密钥时,会直接登陆服务器,不会触发验证码验证。