CentOS配置SSH登录二次认证(基于Google Authenticator的2FA)

在服务器上配置基于Google Authenticator的动态码认证是一个双因子认证方案,可实现登录服务器时,必须正确输入验证码和系统用户密码后才能正常登录,Google Authenticator是基于TOTP实现的。TOTP的全称是"基于时间的一次性密码"(Time-based One-time Password)。

1、安装epel

yum install epel-release

## 更换epel源可参考:https://mirrors.xinac.net/%40%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/#epel

2、安装 google-authenticator

yum install pam-devel qrencode-libs google-authenticator mercurial

3、google-authenticator配置

# google-authenticator

#是否启用基于时间的认证
Do you want authentication tokens to be time-based (y/n)  y

# 此处会在控制台生成二维码,可使用手机APP扫码或复制链接后在浏览器打开(打开链接需要科学上网)
# 手机APP可下载《华为云》或《阿里云》,都支持虚拟MFA

#将认证信息写入到这个文件内
Do you want me to update your "/root/.google_authenticator" file? (y/n)  y

#每次生成的验证码经过30s自动失效
your chances to notice or even prevent man-in-the-middle attacks (y/n)  y

#如果输入y,可以在4分钟查看8个有效的验证码,如果输入n,可以在一分半中查看三个有效验证码
Do you want to do so? (y/n)  n

#是否启用速率限制,主要是为了防止攻击
Do you want to enable rate-limiting? (y/n)  y

4、登录配置

# 1、设置权限
chmod 600 /root/.google_authenticator

# 2、配置 /etc/pam.d/sshd
## 以下命令将在 /etc/pam.d/sshd 的第一行添加pam_google_authenticator.so认证模块
## 以下配置在登录时将要求先输入验证码,再输入密码;如果想先输入密码,修改命令中的 1 -> 2
sed -i '1a\auth       required     pam_google_authenticator.so' /etc/pam.d/sshd

# 3、配置 /etc/ssh/sshd_config
## 如果文件中不存在配置项 ChallengeResponseAuthentication yes,可手动添加
sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config

# 4、重启ssh服务
systemctl restart sshd

5、配置SELINUX(必需)

# SELinux需要关闭或配置后才能登录,以下方案可选择一种

## 查看状态
getenforce
sestatus

## 关闭SELinux
### 临时关闭,立即生效,重启后失效
setenforce 0

### 永久关闭,需要重启后生效
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

## 更改 SELinux 策略
semanage fcontext -a -t ssh_home_t "/root/.google_authenticator"
restorecon -R /root/.google_authenticator

6、连接工具配置

如果用Xshell连接服务器,在会话属性的以下位置:文章源自新逸网络-https://www.xinac.net/9376.html

连接>用户身份认证>方法,勾选PasswordKeyboard Interactive文章源自新逸网络-https://www.xinac.net/9376.html

其他工具类似。在弹出输入密码时,选择Keyboard Interactive登录,按提示输入密码或验证码。文章源自新逸网络-https://www.xinac.net/9376.html

到此配置全部完成。文章源自新逸网络-https://www.xinac.net/9376.html 文章源自新逸网络-https://www.xinac.net/9376.html

weinxin
新逸IT技术
扫一扫关注微信公众号
Admin
  • 本文由 发表于 2023-07-25
  • 转载请注明:https://www.xinac.net/9376.html
CentOS系统openssh升级教程 运维笔记

CentOS系统openssh升级教程

注意:升级openssh版本有风险,升级过程中不能中断!否则服务器有失联的风险。使用VPN连接远程操作的,网络不稳定的请勿操作。 按本教程操作,网络稳定的情况下,99%的都会成功。 网上很多教程先安装...
评论  1  访客  1
    • zhao8378
      zhao8378 1

      拿到

    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定