
RHEL-Like
1 前言
一个问题,一篇文章,一出故事。
笔者想开启2FA以便增强SSH服务的安全性,于是便整理此文。
2 最佳实践
2.1 环境信息
Host Name = sftp.cmdschool.org
OS = RHEL 8.x x86_64
IP Address = any
2.2 安装认证插件
dnf install -y epel-release dnf install -y google-authenticator
2.3 配置认证插件
google-authenticator
配置向导如下,
Do you want authentication tokens to be time-based (y/n) y Warning: pasting the following URL into your browser exposes the OTP secret to Google: https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@sftp.cmdschool.org%3Fsecret%3D33XS73PDPRIAVUHKOTVQ52HDS2%26issuer%3Dsftp.cmdschool.org 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: 33XS73PDPRIAVUHKOTVQ52HDS2 Enter code from app (-1 to skip): 766718 Code confirmed Your emergency scratch codes are: 51232342 40682323 17384676 72587667 88623425 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 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
需要注意的是,根据提示我们需要按照如下方法操作,
– Step1,手机中安装“Google Authenticator”APP(鸿蒙手机需要先安装Gspace)
– Step2,把上面加产生的URL粘贴到浏览器,会看到浏览器返回的二维码
– Step3,手机“Google Authenticator”APP点击【已验证ID】->【扫描QR码】->【Authenticator】->【sftp】即可查看一次性密码代码
– Step4,在以上的向导中输入“一次性密码代码”,范例中为“766718”,然后一路选择“y”即可完成配置
2.4 修改PAM的SSHD配置
vim /etc/pam.d/sshd
加入如下配置,
auth substack password-auth auth required pam_google_authenticator.so nullok
2.5 修改SSHD配置
vim /etc/ssh/sshd_config
加入如下配置,
#ChallengeResponseAuthentication no ChallengeResponseAuthentication yes
重启服务使配置生效,
systemctl restart sshd.service
2.6 验证2FA配置
ssh root@sftp.cmdschool.org
测试向导如下,
(root@sftp.cmdschool.org) Password: ****** (rroot@sftp.cmdschool.org) Verification code: ****** Activate the web console with: systemctl enable --now cockpit.socket Last login: Mon Feb 26 11:58:11 2024 from 10.168.0.8
2.7 配置例外的非2FA用户
2.7.1 配置用户仅键盘交互即可登录
vim /etc/ssh/sshd_config
加入如下配置,
Match user will AuthenticationMethods keyboard-interactive
重启服务使配置生效,
systemctl restart sshd.service
2.7.2 配置用户仅仅公钥认证后即可登录
vim /etc/ssh/sshd_config
加入如下配置,
Match user will AuthenticationMethods publickey
重启服务使配置生效,
systemctl restart sshd.service
2.7.3 配置用户公钥认证后和键盘交互后才能登录
vim /etc/ssh/sshd_config
加入如下配置,
Match user will AuthenticationMethods publickey,keyboard-interactive
重启服务使配置生效,
systemctl restart sshd.service
参阅文档
=======================
https://www.redhat.com/sysadmin/mfa-linux
没有评论