You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 18.04系统SSH服务拒绝Google Authenticator验证码问题求助

Ubuntu 18 SSH 拒绝 Google Authenticator 验证码排查方案

1. 校验服务端系统时间与时区

  • 执行命令查看当前系统时间:date
  • TOTP 验证码依赖时间一致性,服务端时间和标准时间偏差超过30秒就会触发校验失败,Ubuntu 18 默认使用 systemd-timesyncd 同步时间,执行命令强制开启时间同步:sudo timedatectl set-ntp true
  • 确认服务端时区和移动端一致,如需修改为国内时区可执行:sudo timedatectl set-timezone Asia/Shanghai

2. 校验 PAM 配置正确性

  • 查看 SSH 对应的 PAM 配置文件:sudo cat /etc/pam.d/sshd
  • 确认存在以下配置且未被注释:

auth required pam_google_authenticator.so nullok
若需要强制所有用户启用 MFA,可去掉 nullok 参数

  • 注意该配置必须放在 @include common-auth 之前,否则会出现验证逻辑顺序错误,导致合法验证码被拦截

3. 校验 sshd 服务配置

  • 查看 sshd 主配置文件:sudo cat /etc/ssh/sshd_config
  • 确认以下参数配置正确:

ChallengeResponseAuthentication yes
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive
若使用密码+MFA的验证逻辑,将最后一行改为 AuthenticationMethods password,keyboard-interactive

  • 修改配置后重启 sshd 生效:sudo systemctl restart sshd

4. 校验用户侧 Google Authenticator 配置

  • 切换到登录用户,检查配置文件权限:ls -l ~/.google_authenticator
  • 该文件权限必须为 600,所有者必须为当前登录用户,权限错误可执行修复:chmod 600 ~/.google_authenticator && chown $USER:$USER ~/.google_authenticator
  • 如密钥绑定异常可重新生成配置:google-authenticator -t -d -f -r 3 -R 30 -w 3

参数说明:-t 使用 TOTP 协议、-d 禁止密码重用、-f 强制写入配置、-r 3 -R 30 限制30秒内最多尝试3次、-w 3 允许前后1个时间偏移的验证码(总共3个有效验证码,兼容小范围时间偏差)

  • 重新生成后需重新在移动端扫码绑定,不要手动输入密钥,避免字符大小写或特殊字符输入错误

5. 日志定位具体错误

  • 实时查看 SSH 认证日志:tail -f /var/log/auth.log
  • 主动触发 SSH 连接验证失败,根据日志报错排查:
    • 出现 Invalid verification code 优先检查时间偏差、密钥一致性
    • 出现 Module is unknown 说明 PAM 模块未安装,执行 sudo apt install libpam-google-authenticator 即可

内容的提问来源于stack exchange,提问作者JJR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 04:15:04