Linux增加GOOGLE两步验证登陆

2018-10-18 11:00 By "Powerless" 3053 0 0

一、安装所需的组件

yum groupinstall "Development Tools" -y
yum install pam-devel -y
yum install chrony -y


二、编译安装谷歌两步验证

    服务器上有Git的同学可以这样

git clone https://github.com/google/google-authenticator-libpam.git     
cd google-authenticator-libpam/

    如果没有Git也可以直接下载

wget https://github.com/google/google-authenticator-libpam/archive/1.05.tar.gz
tar -zxvf 1.05.tar.gz

完成后我们进行编译安装

 ./bootstrap.sh
 ./configure
 make && make install

三、完成后进入重要步骤

    将谷歌两步验证加入到ssh服务中:

vim /etc/pam.d/sshd

在添加引入.so文件

#%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       required     pam_google_authenticator.so
auth       include      postlogin

注意:如果你需要在密码验证通过后再验证动态码那么pam_google_authenticator.so文件一定要在password-auth下。

配置ssh服务,开启两步验证:

vim /etc/ssh/sshd_config

找到ChallengeResponseAuthentication no 将no修改为yes


【重点】编译目录下./libs文件夹中有个pam_google_authenticator.so文件

cp pam_google_authenticator.so /lib64/security/

完成后重启ssh

systemctl restart sshd

现在开始配置谷歌两步验证,启动脚本

./google-authenticator

下面程序需要你回答一些问题

Do you want authentication tokens to be time-based (y/n)

#你是否要生成基于时间的认证口令?选择y

这时谷歌会生成一个二维码你用手机扫一下就好了,如果不想扫码当然也可以输入

Your new secret key is:之后的内容

另外会给你五个是紧急状态使用的验证码,谨当无法获取验证码时使用,注意这些紧急验证码用一次就少一个,所以这几个紧急验证码一定要保存好

Do you want me to update your "/home/jss/.google_authenticator" file (y/n)

#你希望我更新你的“~/.google_authenticator”文件吗(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)

#你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒,但是这加大了发现或甚至防止中间人攻击的可能性(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)

#默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题,可以将窗口从默认大小即1分30秒加大到约4分。你希望这么做吗(y/n)?选择y

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)

#如果你登录的那台计算机没有加固,以防范暴力登录,可以对验证模块启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。你希望启用尝试次数限制吗(y/n)?选择y


四、完成

现在就可以通过谷歌验证码登录你的服务器了。不过建议保险起见,可以先给服务器成一个公钥到本机上。确保可以用公钥连接到自己的服务器。这样。即使无法使用谷歌验证器登录任然可以使用公钥登录服务器。

评 论

View in WeChat

Others Discussion

  • 分布式服务限流
    Posted on 2020-02-07 18:57
  • 有状态服务VS无状态服务
    Posted on 2020-02-07 18:18
  • 企业级PAAS云平台几个关键问题和挑战
    Posted on 2019-06-12 18:33
  • MySQL 单库后期分库策略
    Posted on 2019-08-19 14:31
  • Redis七大经典问题
    Posted on 2021-05-27 11:14
  • 关于HTTPS的五大误区
    Posted on 2020-02-02 01:10
  • PHP实现精确发布时间
    Posted on 2018-12-06 21:00
  • 为什么要测量尾部延迟
    Posted on 2020-09-18 10:34