ssh动态验证码登录机器
Google Authenticator是一个动态验证码程序,兼容各种智能手机平板设备,可以用来做各种帐号的二次验证,增加帐号的安全性。SSH是Linux系统的最重要防线之一,为了防止密码泄露或者被爆破,可以使用Google Authenticator来做二次验证,使用方法也很简单
谷歌身份验证器生成的是动态验证码,默认30秒更新。修改配置,SSH登录必须在输入密码之前输入动态验证码。即使账号和密码泄露,验证码输入错误,仍然无法登录。苹果或者安卓手机端可以安装身份验证器App读取验证码。
操作系统环境
[[email protected] ~]# cat /etc/issue \S Kernel \r on an \m
操作步骤如下
1.关闭selinux
[[email protected] ~]# sed -i ‘[email protected][email protected][email protected]‘ /etc/selinux/config
注:需要重启生效
[[email protected] ~]# setenforce 0
注:临时关闭生效提示警告不影响使用
查看selinux是否关闭
[[email protected] ~]# getenforce Disabled
2.安装软件依赖组件
[[email protected] ~]# yum -y install gcc make pam-devel libpng-devel libtool wget git
3.添加阿里云epel源
[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
4.qrencode安装,谷歌身份验证器要调用那程序生成并显示二维码
[[email protected] ~]# yum -y install qrencode
5.安装谷歌身份验证器
[[email protected] ~]# mkdir -p /app [[email protected] ~]# cd /app [[email protected] app]# git clone https://github.com/google/google-authenticator.git Cloning into ‘google-authenticator‘... remote: Counting objects: 1461, done. remote: Total 1461 (delta 0), reused 0 (delta 0), pack-reused 1460 Receiving objects: 100% (1461/1461), 2.33 MiB | 25.00 KiB/s, done. Resolving deltas: 100% (776/776), done. [[email protected] app]# cd google-authenticator/libpam/ [[email protected] libpam]# ./bootstrap.sh [[email protected] libpam]# ./configure [[email protected] libpam]# make [[email protected] libpam]# make install [[email protected] libpam]# cp .libs/pam_google_authenticator.so /lib64/security/
6.配置PAM,添加谷歌身份验证器PAM模块配置
[[email protected] ~]# head -n 2 /etc/pam.d/sshd #%PAM-1.0 auth required pam_google_authenticator.so
7.修改ssh服务配置文件sshd_config
[[email protected] libpam]# sed -i ‘s#^ChallengeResponseAuthentication no#ChallengeResponseAuthentication yes#‘ /etc/ssh/sshd_config
8.重启ssh服务
[[email protected] libpam]# systemctl restart sshd.service
9.生成二次验证码的系统账号后运行验证器程序google-authenticator
,或者切换到相应用户需要需要进行二次验证码的用户,这里我是直接使用root
用户验证
[[email protected] ~]# google-authenticator Do you want authentication tokens to be time-based (y/n) y #提示是否要基于时间生成令牌,选择y https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/[email protected]%3Fsecret%3D44FOMDL4D5NC7RFJD4CJDDGXFM%26issuer%3DiZ25nmodinrZ
Your new secret key is: 44FOMDL4D5NC7RFJD4CJDDGXFM Your verification code is 770892 Your emergency scratch codes are: 23028713 12647530 28715420 82540673 13054818 #上面的网址为生成的二维码图形地址(需要翻墙才能打开),还会生成密钥,以及5个紧急验证码(当无法获取动态验证码时使用,注意:这5个验证码用一个就会少一个!请保存好!) Do you want me to update your "/root/.google_authenticator" file? (y/n) y #提示是否要更新验证文件,选择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. 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 +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens). Do you want to do so? (y/n) y #默认动态验证码在30秒内有效,由于客户端和服务器可能会存在时间差,可将时间增加到最长4分钟,是否要这么做:这里选择是n,继续默认30秒 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进行限制
10.手机安装谷歌身份验证器,苹果手机打开app store
搜索应用Google Authenticator
,安卓系统打开相应的应用进行安装即可,由于我使用的是安卓系统安装的是安卓版的
11.打开APP后点击“开始设置”,扫描终端的二维码,需要安装专用的条码扫描器,其他软件都无法扫描二维码无效,由于我的手机无法番墙,所以只能使用秘钥的方式了
配置好后的身份验证器
12.由于终端使用的是crt,需要进行设置
使用SecureCRT客户端的话,默认情况下会不支持这种验证方式,只要在会话选项中修改SSH—-Authentication,设置为Keyboard Interactive即可,其他的选项包括Password都要去掉
13.使用crt登录
注:使用Publickey免密码登录方式来登录ssh的话,不支持使用Google Authenticator验证,必须是密码验证的登录方式才支持。
时间: 2024-10-10 01:21:50