利用谷歌安全令牌(google authenticator)为服务器ssh添加二次验证功能

准备

重要的事情:一定要改主机名!!!

因为你在第一次扫二维码的时候,手机上会自动识别你的当前主机名,如果命名不方便你自己区分各个主机,那么后续你需要手动在手机上更改这些信息,机器多的时候会非!常!麻!烦!

开始

1、Google Authenticator依赖时间与客户端进行校验,因此首先得把服务器时间更新至最新

# ntpdate time.nist.gov

2、安装或更新repo(ubuntu自己改用apt,下同)

# yum install epel-release -y
# sudo yum install –y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm (红帽系统)

3、安装Google Authenticator

# yum install google-authenticator -y

4、为当前用户开启两步验证

注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)

# google-authenticator

这个是命令。

在这里插入图片描述

5、初始化,交互选项的翻译

Do you want authentication tokens to be time-based (y/n) y
Do you want me to update your “/root/.google_authenticator” file (y/n) y #提示是否要更新验证文件,选择y ,以上5个验证码保存在此文件中

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 #默认动态验证码在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进行限制

6、关于google authenticator 本身的配置在上一步就告一段落,接下来要对操作系统本身的ssh进行配置,让ssh对google authenticator 支持

# vim /etc/pam.d/sshd  (增加)

auth required pam_google_authenticator.so

在这里插入图片描述


# vim /etc/ssh/sshd_config (修改为yes)

ChallengeResponseAuthentication yes
如果有需求需要密钥+google auth的话,则做如下修改
PubkeyAuthentication yes
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
PasswordAuthentication yes #这一项是决定是否启用密码验证,根据自身需求改

在这里插入图片描述

改完之后重启sshd

#systemctl restart sshd

后记:

基于谷歌验证器的这种二次验证方案,真正应用到生产环境的时候,往往不需要每台服务器创建不同的验证密钥。
设想一下,如果一个项目,用7-8台服务器,那么就需要在手机上生成7-8个秘钥。
如果需要连接服务器的账户有3个(两个普通管理员,一个root)那么秘钥就更多了。
其实这么多秘钥并不是很有必要。


所以我们可以整个项目用一个秘钥,关键操作就是在用户家目录下找到.google_authenticator,将这个文件拷贝到目标机器,对应的用户的家目录下。记得:
一、文件的权限不能高于600,
二、不要再多此一举运行google_authenticator命令,因为这样会刷新覆盖掉刚刚拷贝来的文件。


有一天,客户嫌登录麻烦,要我把谷歌验证器卸掉

于是乎:

yum remove google_authenticator

或者

apt remove google_authenticator

接着需要改的配置文件有两个,就是和安装配置的时候一样的两个

vim /etc/ssh/sshd_config
---
vim /etc/pam.d/sshd
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值