













Google Authenticator(谷歌身份验证器)是一款免费的双因素认证(2FA, Two-Factor Authentication)工具,核心作用是为账户登录、系统访问等场景增加一层安全防护,避免仅依赖密码(单因素认证)带来的被盗风险。
基于 TOTP(基于时间的一次性密码算法) 实现,核心逻辑:
| 核心优势 | 典型适用场景 |
|---|---|
| 离线可用(无需网络交互生成验证码) | Linux 服务器 SSH 登录保护 |
| 验证码 30 秒自动刷新,时效性强 | Zabbix、Nginx 等 Web 应用后台登录 |
| 配置简单,支持跨平台(Android/iOS) | 自建数据库、运维工具的账户认证 |
| 防暴力破解、防密码泄露(即使密码泄露,无验证码也无法登录) | 生产环境核心服务器的权限管控 |
Rocky Linux 9 默认软件源包含 Google Authenticator 相关包,无需额外配置第三方源,直接通过 yum 安装即可。
# 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
重启主机
# 安装核心包(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
配置核心分为「生成认证密钥(绑定手机)」和「配置 PAM 模块(启用 2FA 认证)」两步,以下以 root 用户为例(普通用户配置流程一致)。
注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)
Google Authenticator 依赖 Linux 的 PAM(可插拔认证模块)实现认证拦截,需修改 SSH 相关配置,启用 2FA 认证。
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.so 在 auth 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
使用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 次尝试,防止暴力破解验证码) |
执行完成后,终端会输出以下核心信息,需妥善记录:
Your new secret key is: 唯一密钥串(手机端手动配置时需输入,二维码失效时可用);Your verification code is: 当前实时验证码(用于测试);Your emergency scratch codes are: 8 组紧急备用码(手机丢失时,可用于登录,建议复制到安全位置保存,使用后失效);qrencode 生成图片文件)。# 生成二维码图片(保存到当前用户家目录,文件名为 google_auth_qr.png)
qrencode -o ~/google_auth_qr.png "otpauth://totp/root@$(hostname)?secret=你的密钥串"
scp 将二维码文件下载到本地电脑,用手机端 Google Authenticator 应用(国内用户推荐使用阿里云手机端)扫描即可完成绑定;完整命令步骤流程
生成二维码进行绑定,终端界面不显示扫描用的二维码,不过可以通过输入相同效果的密钥进行操作
[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 ~]#
ssh root@你的服务器IP
root 用户的密码(第一因素);Verification code:,输入手机端 Google Authenticator 应用显示的 6 位验证码(第二因素);若手机丢失,在提示 Verification code: 时,输入配置时保存的紧急备用码,即可正常登录,登录后该备用码失效。
yum install -y chrony && systemctl start chronyd && systemctl enable chronyd);② 手机端开启自动时间同步;③ 重新获取手机端最新验证码尝试。ChallengeResponseAuthentication 未改为 yes,或 SSH 服务未重启;/etc/ssh/sshd_config 配置,重启 sshd 服务(systemctl restart sshd)。/etc/pam.d/sshd 末尾的配置项,确保无拼写错误。google-authenticator 生成个人配置文件;su - 用户名),重新执行步骤 1 的初始化配置,生成个人密钥并绑定手机。yum install -y google-authenticator qrencode 完成安装;google-authenticator 生成密钥(绑定手机)→ 配置 PAM 模块 → 修改 SSH 配置并重启服务;在Authentication这儿只勾选上:Keyboard Interactive,并调整为第一
如此一来,在登录的时候,会让先输入用户名,再输入密码,最后输入验证码,即可登录到主机上。
若是默认已添加上用户名了,登录的时候就是输入密码,再输入验证码




Xshell也是同样的配置

修改配置后无需重启系统,新登录会话将立即生效。若配置错误可能导致无法登录,建议保留一个不受限的终端会话或使用救援模式修复。
# 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
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。