惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
D
DataBreaches.Net
B
Blog RSS Feed
D
Docker
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Y
Y Combinator Blog
A
About on SuperTechFans
V
V2EX
罗磊的独立博客
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
月光博客
月光博客
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志

博客园 - 哈喽哈喽111111

AI 不仅没有减少工作,反而增加了工作 你需要知道的 AI 内存知识 firewall-cmd使用教程 minio从旧集群导出桶再上传到新集群导入桶 RockyLinux SSH 跳板转发 3389(Windows 远程桌面)完整方案 MySQL使用自带的logrotate配置日志轮转 yum方式安装redis7 nacos新加用户操作 网络运营商禁止端口信息 Linux开机启动rc.local不生效的一般解决方案 Nginx流量拷贝ngx_http_mirror_module模块使用方法详解 Adobe 修改 hosts 文件 axios 投毒与好莱坞式骗术 Linux系统在使用systemctl启动服务的失败,报错如下:Error No space left on device git submodule 的增、查、改、删 Rocky Linux 安装 Google Chrome 浏览器 “头号玩家”—— 美国技术霸权下的全球虚拟货币资产收割行动深层解析 你是第几级 AI 编程 MySQL中通过关联update将一张表的一个字段更新到另外一张表中 Linux 的 Port Knocking 端口碰撞(端口敲门) MySQL解除死锁 jar文件解压缩操作 设置Windows服务器远程桌面能使用多个桌面 20251024- 使用shell脚本分库定时备份MySQL数据 禁用sentinel 在 Linux 中安装和配置 NTP 服务器和 NTP 客户端 springboot配置文件关系及加载顺序 用自带的Nginx为gitlab做白名单 Rocky9和Ubuntu使用pip安装python的库mysqlclient失败解决方式 在Spring Boot Admin中根据Nacos的命名空间来区分和管理不同的环境
Rocky 9.6安装配置Google Authenticator
哈喽哈喽111111 · 2026-08-25 · via 博客园 - 哈喽哈喽111111

一、Google Authenticator 核心概念

Google Authenticator(谷歌身份验证器)是一款免费的双因素认证(2FA, Two-Factor Authentication)工具,核心作用是为账户登录、系统访问等场景增加一层安全防护,避免仅依赖密码(单因素认证)带来的被盗风险。

1. 核心工作原理

基于 TOTP(基于时间的一次性密码算法) 实现,核心逻辑:

  1. 配置时,服务器与手机端(Google Authenticator 应用)共享一个唯一的「密钥串」(无需网络同步,离线即可生成);
  2. 两端基于当前系统时间(误差允许 30-60 秒),通过相同算法生成 6 位数字一次性验证码;
  3. 登录时,用户除了输入账户密码(第一因素),还需输入手机端实时刷新的 6 位验证码(第二因素),两者均验证通过方可登录。

2. 核心优势与适用场景

核心优势 典型适用场景
离线可用(无需网络交互生成验证码) Linux 服务器 SSH 登录保护
验证码 30 秒自动刷新,时效性强 Zabbix、Nginx 等 Web 应用后台登录
配置简单,支持跨平台(Android/iOS) 自建数据库、运维工具的账户认证
防暴力破解、防密码泄露(即使密码泄露,无验证码也无法登录) 生产环境核心服务器的权限管控

3. 注意事项

  • 需在手机端安装「Google Authenticator」应用(Android/iOS 应用商店可下载);
  • 配置时生成的「紧急备用码」需妥善保存(手机丢失/损坏时,可通过备用码登录,避免账户锁定);
  • 服务器与手机端的时间需同步(建议开启自动时间同步),否则验证码会不匹配。

二、RockyLinux 9 环境安装 Google Authenticator

Rocky Linux 9 默认软件源包含 Google Authenticator 相关包,无需额外配置第三方源,直接通过 yum 安装即可。

步骤 1:系统环境准备

# 1. 启用 EPEL 源(Rocky Linux 9 部分工具依赖 EPEL,必做)
yum install -y epel-release

# 2. 清理并刷新 yum 缓存
yum clean all && yum makecache

# 3.关闭selinux
临时关闭: setenforce 0
永久关闭:
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

重启主机

步骤 2:安装 Google Authenticator 及辅助工具

# 安装核心包(pam_google_authenticator 是 PAM 认证模块,核心依赖)
yum install -y google-authenticator qrencode

# 验证安装是否成功
google-authenticator --version
  • google-authenticator:核心配置工具,用于生成密钥、备用码等;
  • qrencode:辅助工具,用于生成二维码(方便手机端扫描快速配置,无需手动输入密钥),这个可以不用安装。

源码安装方式:

yum -y install autoconf automake libtool wget gcc make pam-devel libpng-devel
git clone https://github.com/google/google-authenticator-libpam.git
cd ./google-authenticator-libpam/
./bootstrap.sh && ./configure && make && make install

[root@localhost google-authenticator-libpam]# ./bootstrap.sh 
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'build'.
libtoolize: copying file 'build/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'build'.
libtoolize: copying file 'build/libtool.m4'
libtoolize: copying file 'build/ltoptions.m4'
libtoolize: copying file 'build/ltsugar.m4'
libtoolize: copying file 'build/ltversion.m4'
libtoolize: copying file 'build/lt~obsolete.m4'
configure.ac:12: installing 'build/compile'
configure.ac:16: installing 'build/config.guess'
configure.ac:16: installing 'build/config.sub'
configure.ac:13: installing 'build/install-sh'
configure.ac:13: installing 'build/missing'
Makefile.am: installing 'build/depcomp'
parallel-tests: installing 'build/test-driver'

[root@localhost google-authenticator-libpam]# ./configure 

configure: creating ./config.status
config.status: creating Makefile
config.status: creating contrib/rpm.spec
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands

  google-authenticator version 1.11
  Prefix.........: /usr/local
  Debug Build....: 
  C Compiler.....: gcc -g -O2 -Wall 
  Linker.........: /usr/bin/ld -m elf_x86_64  
  SELinux CFlags.: 
  SELinux libs...: 


[root@localhost google-authenticator-libpam]# make
make  all-am
make[1]: Entering directory '/root/google-authenticator-libpam'
  CC       src/google-authenticator.o
  CC       src/util.o
  CC       src/base32.o
  CC       src/hmac.o
  CC       src/sha1.o
  CCLD     google-authenticator
  CC       src/base32_prog.o
  CCLD     base32
  CC       src/pam_google_authenticator_la-pam_google_authenticator.lo
  CC       src/pam_google_authenticator_la-util.lo
  CC       src/pam_google_authenticator_la-base32.lo
  CC       src/pam_google_authenticator_la-hmac.lo
  CC       src/pam_google_authenticator_la-sha1.lo
  CCLD     pam_google_authenticator.la
make[1]: Leaving directory '/root/google-authenticator-libpam'

[root@localhost google-authenticator-libpam]# make install

Libraries have been installed in:
   /usr/local/lib/security  # 注意这个路径

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[1]: Leaving directory '/root/google-authenticator-libpam'

[root@localhost google-authenticator-libpam]# ll /usr/local/lib/security
total 136
-rwxr-xr-x 1 root root   1015 Aug 25 10:16 pam_google_authenticator.la
-rwxr-xr-x 1 root root 132512 Aug 25 10:16 pam_google_authenticator.so

三、Rocky Linux 9环境配置Google Authenticator(以 SSH 登录保护为例)

配置核心分为「生成认证密钥(绑定手机)」和「配置 PAM 模块(启用 2FA 认证)」两步,以下以 root 用户为例(普通用户配置流程一致)。

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

步骤 1:配置 PAM 模块,启用 SSH 双因素认证

Google Authenticator 依赖 Linux 的 PAM(可插拔认证模块)实现认证拦截,需修改 SSH 相关配置,启用 2FA 认证。

  1. 编辑 SSH 的 PAM 配置文件:
vim /etc/pam.d/sshd

# 在原有文件的第一行下添加以下配置(启用 Google Authenticator 认证模块)
auth substack   password-auth # 文件原来的第一行
auth required   /usr/local/lib/security/pam_google_authenticator.so # 在第二行的位置处新增这一行
  • 允许未配置 2FA 的用户正常登录,再加上 nullok 即可(auth required /usr/local/lib/security/pam_google_authenticator.so nullok);

  • auth required /usr/local/lib/security/pam_google_authenticator.soauth substack password-auth 之后表示先输密码、再输动态码,反之则需要先输动态码、再输密码。一般使用前者。这样可以将密码记录在 xshell 等登录工具中,每次登录只需要输入一次动态码即可,否则每次都需要手动输动态码和密码,登录流程相对较复杂。

2.编辑 SSH 服务配置文件,启用密码+2FA 双重认证:

# 编辑/etc/ssh/sshd_config文件,若该文件内容中有引用的其他配置文件,
比如:Include /etc/ssh/sshd_config.d/*.conf,则需要在这个目录下找对应的配置文件进行编辑,比如是:/etc/ssh/sshd_config.d/50-redhat.conf

# vim /etc/ssh/sshd_config 或者是 # vim /etc/ssh/sshd_config.d/50-redhat.conf

ChallengeResponseAuthentication yes # 允许使用提示应答式认证(2FA 依赖该选项)
UsePAM yes
PubkeyAuthentication no #禁止秘钥登录

保存配置并重启 SSH 服务(使配置生效):

# 重启 sshd 服务
systemctl restart sshd

# 验证 sshd 服务状态(无 failed 即为正常)
systemctl status sshd

步骤 2:生成认证密钥与绑定手机

使用google authenticator PAM插件为ssh登录账号生成动态验证码

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

1.执行初始化配置命令,进入交互模式:

google-authenticator

2.交互选项配置(建议全部选 y,保障安全性,每一步均有提示):

交互选项 选择 核心说明
Make tokens time-based (y/n) y 启用基于时间的一次性验证码(TOTP 算法,默认推荐)
Update your .google_authenticator file (y/n) y 将配置信息写入当前用户家目录的 .google_authenticator 文件(核心配置文件,不可删除)
Disallow multiple uses of the same authentication token (y/n) y 禁止同一验证码重复使用(防止重放攻击,提升安全性)
Increase the window of acceptable authentication tokens (y/n) y 扩大验证码容错窗口(允许前后各 1 个时间窗口,解决轻微时间同步偏差问题)
Enable rate-limiting (y/n) y 启用速率限制(默认每 30 秒允许 3 次尝试,防止暴力破解验证码)
  1. 保存核心输出信息(关键!切勿遗漏):

执行完成后,终端会输出以下核心信息,需妥善记录:

    • Your new secret key is: 唯一密钥串(手机端手动配置时需输入,二维码失效时可用);
    • Your verification code is: 当前实时验证码(用于测试);
    • Your emergency scratch codes are: 8 组紧急备用码(手机丢失时,可用于登录,建议复制到安全位置保存,使用后失效);
    • 二维码 URL(终端会输出可视化二维码,若服务器有图形界面,可直接扫描;无图形界面可通过 qrencode 生成图片文件)。
  1. 生成二维码图片(方便手机扫描绑定,可选但推荐):
# 生成二维码图片(保存到当前用户家目录,文件名为 google_auth_qr.png)
qrencode -o ~/google_auth_qr.png "otpauth://totp/root@$(hostname)?secret=你的密钥串"
    • 可通过 scp 将二维码文件下载到本地电脑,用手机端 Google Authenticator 应用(国内用户推荐使用阿里云手机端)扫描即可完成绑定;
    • 绑定成功后,手机端应用会实时显示 6 位验证码,30 秒自动刷新。

完整命令步骤流程

生成二维码进行绑定,终端界面不显示扫描用的二维码,不过可以通过输入相同效果的密钥进行操作

[root@localhost ~]# google-authenticator 

Do you want authentication tokens to be time-based (y/n) y  # 确认:基于时间的认证token)
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: X3AQ2DEZXEBXRDTJQVWMARM3J3RORJOV  # MFA软件中输入这个密钥
Enter code from app (-1 to skip): 969037  # 输入MFA软件中显示的二次验证码
Code confirmed
Your emergency scratch codes are:
  63751366
  58415612
  67380698
  45557260
  97988530

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, 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 # token有效期是1.5min,选y就是4min

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 # 30s内只允许尝试三次
[root@localhost ~]# 

步骤 3:验证 SSH 双因素认证是否生效

  1. 打开新的终端窗口,尝试通过 SSH 登录目标服务器:
ssh root@你的服务器IP
  1. 登录流程验证(正常应出现两步验证):
    1. 首先输入 root 用户的密码(第一因素);
    2. 随后提示 Verification code:,输入手机端 Google Authenticator 应用显示的 6 位验证码(第二因素);
    3. 两者均输入正确后,即可成功登录服务器。
  2. 紧急备用码测试(可选,确保备用码可用):

若手机丢失,在提示 Verification code: 时,输入配置时保存的紧急备用码,即可正常登录,登录后该备用码失效。

四、常见问题与避坑

1. 验证码输入正确但提示认证失败

  • 原因:服务器与手机端时间不同步,或验证码已过期(超过 30 秒);
  • 修复:① 服务器开启时间同步(yum install -y chrony && systemctl start chronyd && systemctl enable chronyd);② 手机端开启自动时间同步;③ 重新获取手机端最新验证码尝试。

2. SSH 登录时无验证码提示,直接通过密码登录

  • 原因:ChallengeResponseAuthentication 未改为 yes,或 SSH 服务未重启;
  • 修复:重新核对 /etc/ssh/sshd_config 配置,重启 sshd 服务(systemctl restart sshd)。

3. 配置后无法登录服务器,提示认证失败

  • 原因:配置错误(如 PAM 配置写错),或备用码未保存;
  • 修复:① 若有物理控制台/云服务器 VNC 登录权限,直接登录服务器修改配置;② 核对 /etc/pam.d/sshd 末尾的配置项,确保无拼写错误。

4. 普通用户无法使用 2FA 认证

  • 原因:普通用户未执行 google-authenticator 生成个人配置文件;
  • 修复:切换到普通用户(su - 用户名),重新执行步骤 1 的初始化配置,生成个人密钥并绑定手机。

五、核心总结

  1. Google Authenticator 是基于 TOTP 算法的双因素认证工具,核心用于提升账户/系统登录安全性,离线可用;
  2. Rocky Linux 9 需先启用 EPEL 源,再通过 yum install -y google-authenticator qrencode 完成安装;
  3. 核心配置流程:google-authenticator 生成密钥(绑定手机)→ 配置 PAM 模块 → 修改 SSH 配置并重启服务;
  4. 关键注意点:妥善保存紧急备用码,确保服务器与手机端时间同步,配置后先通过新终端测试登录,避免锁定。

六、使用SecureCRT 登录配置

在Authentication这儿只勾选上:Keyboard Interactive,并调整为第一

如此一来,在登录的时候,会让先输入用户名,再输入密码,最后输入验证码,即可登录到主机上。

若是默认已添加上用户名了,登录的时候就是输入密码,再输入验证码

image-20260825120034707

image-20260825120219029

image-20260825120103252

image-20260825120126331

Xshell也是同样的配置

image-20260825120309437

七、白名单配置

修改配置后无需重启系统,新登录会话将立即生效。若配置错误可能导致无法登录,建议保留一个不受限的终端会话或使用救援模式修复。

# vim /etc/pam.d/sshd
# 在指定位置添加一行内容,效果如下所示
auth [success=1 default=ignore] pam_access.so accessfile=/etc/google-auth.conf # 添加这行内容
auth  required /usr/local/lib/security/pam_google_authenticator.so

新建 /etc/google-auth.conf 文件,按如下格式分3种情况启用IP或用户白名单。

$ cat /etc/google-auth.conf

# +表示允许,-表示拒绝
# 中间表示的是登录用户
# 最后表示的是登录来源IP地址

# 所有用户从内网其它机器ssh过来不启用二次验证
+ : ALL : 192.168.1.0/24
+ : ALL : LOCAL

# 用户 user1 user2 从任何地方登录都不启用二次验证
+ : user1 user2 : ALL

# 用户 user1 user2 从192.168.2.0/24这个网段登录不启用二次验证
+ : user1 user2 : 192.168.2.0/24

# 允许特定组从内网登录
+ : @ops_group : 192.168.1.0/24

# 禁止 root 从所有远程地址登录(仅允许本地)
- : root : ALL EXCEPT LOCAL

# 拒绝某用户从除指定 IP 外的所有地方登
- : admin_user : ALL EXCEPT 10.0.0.5