linux上配置google authenticator pam模块本质是ssh登录时叠加totp校验,需正确安装模块、严格设置用户密钥文件权限(600)、在/etc/pam.d/sshd顶部添加auth [success=ok default=die] pam_google_authenticator.so nullok secret=/home/${user}/.google_authenticator,并启用sshd的challengeresponseauthentication与usepam。

Linux 上配置 Google Authenticator 的 PAM 模块,本质是让 SSH 登录流程中插入一次 TOTP 动态码校验。它不替代密码或密钥,而是叠加在第一因素之后——必须装对模块、配对路径、写准规则,三者缺一不可。
确认 PAM 模块已正确安装
模块文件必须存在且路径匹配系统架构:
- Debian/Ubuntu:检查 /lib/security/pam_google_authenticator.so 是否存在
- RHEL/CentOS/Rocky 8+:检查 /usr/lib64/security/pam_google_authenticator.so
- CentOS 7:路径同 RHEL 8+,但需先启用 EPEL 源
若缺失,补装命令如下:
PyCharm 2026.2.0.1 Linux版提供 JetBrains 官方 2026.2.0.1 版本安装包,适合需要指定 PyCharm 版本进行 Python 项目开发、运行和调试的用户。
- Ubuntu/Debian:sudo apt install libpam-google-authenticator
- RHEL/CentOS 8+:sudo dnf install google-authenticator
- CentOS 7:sudo yum install epel-release google-authenticator
用户级密钥文件权限必须严格
PAM 默认拒绝读取权限宽松的 ~/.google_authenticator,静默失败无日志提示:
- 运行 google-authenticator 必须以目标用户身份执行(su - username 或 sudo -u username bash)
- 生成后立即执行:chmod 600 ~/.google_authenticator
- 同时收紧家目录和 ~/.ssh:chmod 700 ~ ~/.ssh
- SELinux 启用时(如 CentOS 7),需运行:setsebool -P authlogin_nis_enabled 1
PAM 规则要加在正确位置和格式
编辑 /etc/pam.d/sshd,在所有 auth include 行之前添加(顺序决定是否触发):
- 推荐写法(兼顾安全与容错):
auth [success=ok default=die] pam_google_authenticator.so nullok secret=/home/${USER}/.google_authenticator - nullok 允许未初始化用户跳过,上线后可删掉,改为强制验证
- 切勿用 required 或 requisite —— 模块异常会导致全部 SSH 登录被拒
- 避免写在 auth include password-auth 之后,否则密码验证成功即退出,TOTP 不会执行
SSH 服务必须启用交互式认证
PAM 规则只是“验证逻辑”,sshd 需主动触发键盘输入:
- 编辑 /etc/ssh/sshd_config,确保以下三项为 yes:
ChallengeResponseAuthentication yes
UsePAM yes
PasswordAuthentication yes(若仅用密钥登录,则设为 no,但需保留 PubkeyAuthentication yes) - 修改后重启服务:sudo systemctl restart sshd
- 测试前建议开两个终端:一个留作应急,另一个用 ssh -o PubkeyAuthentication=no user@localhost 验证流程










