目录git
徐亮伟, 江湖人称标杆徐。多年互联网运维工做经验,曾负责过大规模集群架构自动化运维管理工做。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工做。
我的博客"徐亮伟架构师之路"累计受益数万人。
笔者Q:552408925
架构师群:471443208github
一般咱们直接经过ssh输入密码链接服务器,但这样很容易出现暴力破解状况,因此咱们能够结合google的动态认证+ssh密码,这样可以大大的提高登录的安全。
简单来讲,就是当用户经过ssh登录系统时,先输入google的随机验证码,而后在输入服务器的ssh密码chrome
1.安装依赖包,环境属于centos7,centos6请自行查阅网上资料bootstrap
[root@bgx ~]# yum -y install wget gcc make pam-devel libpng-devel pam-devel
2.安装Google Authenticator PAM插件安装vim
[root@bgx ~]# wget https://github.com/google/google-authenticator-libpam/archive/1.04.tar.gz [root@bgx ~]# tar xf 1.04.tar.gz [root@bgx ~]# cd google-authenticator-libpam-1.04/ [root@bgx google-authenticator-libpam-1.04]# ./bootstrap.sh [root@bgx google-authenticator-libpam-1.04]# ./configure [root@bgx google-authenticator-libpam-1.04]# make && make install [root@bgx google-authenticator-libpam-1.04]# cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/
3.初始配置 Google Authenticatorcentos
[root@bgx google-authenticator-libpam-1.04]# google-authenticator
是否基于时间的认证,为了防止不一样跨时区的问题,这里选择n浏览器
Do you want authentication tokens to be time-based (y/n) n
而后会跳出一个google的二维码
红色框框是: 生成的密钥
绿色框框是: 生成的5个一次性紧急验证码,用于紧急状况下,使用过一次后该验证码即失效了。
安全
是否更新用户的 Google Authenticator 配置文件,选择 y 才能使上面操做对当前 root 用户生效,其实就是在对应用户的 Home 目录下生成了一个 .google_authenticator 文件,若是你想停用这个用户的 Google Authenticator 验证,只须要删除这个用户 Home 目录下的 .google_authenticator 文件就能够了。bash
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
每次生成的令牌30s生成一次,最高容许存在偏差4分钟。
By default, a new token is generated every 30 seconds by the mobile app. 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. This allows for a time skew of up to 30 seconds between authentication server and client. If you experience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the current code, the next code) to 17 permitted codes (the 8 previous codes, the current code, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server. Do you want to do so? (y/n) y
4.SSH调用及客户端配置,添加pam认证,在第一行添加
[root@bgx ~]# vim /etc/pam.d/sshd #添加以下行 auth required pam_google_authenticator.so
5.修改sshd配置,关联google认证
[root@bgx ~]# vim /etc/ssh/sshd_config ChallengeResponseAuthentication yes #修改成yes [root@bgx ~]# systemctl restart sshd #重启sshd服务
6.客户端经过ssh链接服务器测试
须要输入动态密码,动态密码经过手机获取以下图所示
7.查看服务端的安全日志文件,能够看到是先进程google动态密码认证
8.注意事项:
1.用password + google authenticator,若是使用公钥登陆的话,会跳过google authenticator验证直接登陆服务器的。
2.若是是内网测试使用,建议安装google authenticator 浏览器插件实践。若是是公网服务器建议安装手机版的Authenticator
9.安装Google authenticator
Andorid版: “自行百度” iOS版: 下载 “Authenticator” chrome浏览器有google authenticator的插件