0%

ssh二次验证配置指南

本文是为了将sshd服务公开在公网时,为加强安全性设置google二次验证的配置方式记录;这里对免密登陆前做一次验证码登陆;依靠google的安全性验证防止攻击.

  1. 安装PAM模块

    1
    2
    $ yum install -y epel-release
    $ yum install -y google-authenticator
  2. 生成二次验证代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 生成验证码
    # 哪个账号需要动态验证码,请切换到该账号下操作

    # -t: 使用 TOTP 验证
    # -f: 将配置保存到 ~/.google_authenticator 文件里面
    # -d: 不允许重复使用以前使用的令牌
    # -w 3: 使用令牌进行身份验证以进行时钟偏移
    # -e 10: 生成 10 个紧急备用代码
    # -r 3 -R 30: 限速 - 每 30 秒允许 3 次登录
    $ google-authenticator -t -f -d -w 3 -e 10 -r 3 -R 30

    注意:默认情况下,命令 google-authenticator 生成的文件路径在 ~/.google_authenticator,为了能够让 PAM 读取含有所需 key 的文件 ~/.google_authenticator,可先将其复制到 ~/.ssh/ 目录中(同时更改文件owner),再通过设置指令 secret=${HOME}/.ssh/.google_authenticator。这样即便已经启用了 SELinux,PAM 仍可以读取所需的文件内容,而不会被 SELinux 拦截。
    完整指令common-auth文件内容:

    1
    2
    3
    auth required pam_google_authenticator.so secret=${HOME}/.ssh/.google_authenticator
    # or
    auth required pam_google_authenticator.so secret=${HOME}/.ssh/.google_authenticator [authtok_prompt=Verification code: ]
  3. SSH服务器配置二次验证
    /etc/pam.d/sshd的文件修改

    1
    2
    3
    #在/etc/pam.d/中增加common-auth文件
    # 首先进行google验证器的验证
    auth include common-auth
  4. sshd_config文件修改

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 密钥登录
    PubkeyAuthentication yes
    # 先进行验证码验证
    AuthenticationMethods keyboard-interactive,publickey
    # 禁止密码登录
    PasswordAuthentication no
    # Change to no to disable s/key passwords
    # 开启二次验证
    ChallengeResponseAuthentication yes