Linux 实现 Google Authenticator 动态密码 + SSH 密码双重认证

Google Authenticator1.png

说明:一般我们考虑到 VPS 的安全问题的时候,都是更改 SSH 端口和密码,然后更安全的也就是禁用密码使用密匙登录。方法很久前就水过了,这里再分享一个方法,可以在 VPS 上安装一个 Google Authenticator(谷歌身份验证器),这样我们登录 VPS 的时候,不仅需要密码正确,而且还要你输入正确的动态验证码才能登录进去,这样安全性就高了不少,这里就说下 CentOSDebianUbuntu 的使用。

提示:教程需要配合 Google 身份验证器一起使用,手机没有安装该 APP 的需要安装一下,方便获取动态验证码。

安装

1、软件包安装

 
  • #CentOS 6系统

  • rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

  • yum install google-authenticator -y

  •  
  • #CentOS 7系统

  • rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

  • yum install google-authenticator -y

  •  
  • #Debian/Ubuntu系统

  • apt update

  • apt install libpam-google-authenticator -y

2、编译安装
安装依赖:

 
  • #CentOS系统

  • yum install gcc make pam-devel libpng-devel libtool wget git autoconf automake qrencode -y

  •  
  • #Debian/Ubuntu系统

  • apt update

  • apt install -y gcc make autoconf automake libtool libpam0g-dev libqrencode3 git

安装验证器:

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

  • cd google-authenticator-libpam

  • ./bootstrap.sh

  • ./configure

  • make && make install

配置

1、配置验证器

  • google-authenticator

输出如下:

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

  • #验证二维码,在浏览器打开使用谷歌验证器APP扫描添加即可。

  • https://www.google.com/chart?chs=200x200xxx

  • Your new secret key is: WKDPJH7KR2P3DOWL

  • Your verification code is 187102

  • #临时验证码,手机不在身边可以使用,不过一个码只能用一次

  • Your emergency scratch codes are:

  • 77676926

  • 14725443

  • 83654478

  • 55663982

  • 23962253

  •  
  • #下面可以直接照着填,或者自己使用谷歌翻译,然后自行选择

  • Do you want me to update your "/root/.google_authenticator" file (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) 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) 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) y

2、配置 PAM 文件
修改 PAM 配置文件:

 
  • nano /etc/pam.d/sshd

在相应的位置添加 auth required pam_google_authenticator.so 代码,大概如下:

 
  • #CentOS 6在#%PAM-1.0下面一行添加

  • #CentOS 7在auth substack password-auth下面一行添加

  • #Debian和Ubuntu在末尾添加

然后使用 Ctrl+xy 保存退出。

或者直接使用命令添加:

 
  • #CentOS 6系统

  • sed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd

  • #CentOS 7系统

  • sed -i "/auth[ ]*substack[ ]*pass*/a\auth required pam_google_authenticator.so" /etc/pam.d/sshd

  • #Debian/Ubuntu系统

  • echo 'auth required pam_google_authenticator.so' >>/etc/pam.d/sshd

如果是编译安装的,还需要做一下软链接:

 
  • #CentOS系统

  • ln -fs /usr/local/lib/security/pam_google_authenticator.so /lib64/security/

  • #Debian/Ubuntu系统

  • ln -fs /usr/local/lib/security/pam_google_authenticator.so /lib/x86_64-linux-gnu/security/

3、修改 SSH 文件
这里可以直接使用命令:

 
  • sed -i -r 's#(ChallengeResponseAuthentication) no#\1 yes#g' /etc/ssh/sshd_config

然后同步下时间:

 
  • #查看下服务器时间

  • date

  • #如果时区不一样,再使用命令修改为本地时间

  • ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

对于 CentOS 系统,还需要关闭 SELINUX,不过并不是所有系统都是开启状态,使用命令:

 
  • #使用命令查看状态

  • getenforce

  • #如果输出disabled则为关闭,反之开启,然后使用命令关闭

  • sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

最后重启 SSH

 
  • #CentOS系统

  • service sshd restart

  • #Debian/Ubuntu系统

  • service ssh restart

配置好了,再登录 SSH 的时候,这里以 Xshell 为例,类型选择 Keyboard Interactive 方式,然后会要你输入动态验证码了。

Google_Authenticator.png

基本上以后我们每次登录 VPS 的时候,不仅会要你输入密码,还会要你输入谷歌验证的动态码才能进入 VPS,安全增加了不少。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Google Authenticator(谷歌验证)的密码输入,实际上并没有一个固定的密码Google Authenticator通过基于时间的一次性密码(TOTP)生成密码来进行账户验证。这种密码是根据事先与服务器约定好的密钥和当前的时间戳来生成的。当用户登录时,他们需要在Google Authenticator应用程序中输入由此算法生成的一次性密码,并将其发送到服务器进行验证。如果生成的一次性密码与服务器计算的密码一致,登录将成功。这种基于时间的算法确保了每个一次性密码只有在短时间内有效,提高了账户的安全性。所以实际上,你需要根据服务器提供的密钥和当前的时间戳,在Google Authenticator应用程序中生成对应的一次性密码,并输入该密码进行验证。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [谷歌验证Google Authenticator工作原理](https://blog.csdn.net/weixin_39732991/article/details/110784762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [谷歌Google authenticator 整合到JAVA项目](https://blog.csdn.net/baidu_38990811/article/details/106002098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值