如何在CentOS/RHEL 8/7上设置两个因素(2FA)身份验证

时间:2020-02-23 14:39:01  来源:igfitidea点击:

我们将在CentOS/Rhel 8/7上使用Google Authenticator进行两位因子身份验证,如何保护SSH。
双因素认证是一个撰写两个阶段的过程,以验证网络中的实体访问服务的身份。
它将第二层安全性添加到标准用户名和密码身份验证。

SSH是一种广泛使用的协议,用于访问远程Linux/UNIX服务器,并在服务器之间推送文件。
如果没有适当的安全策略管理在SSH上进行访问,则成功的蛮力攻击可能会导致损失。
本教程将讨论CentOS/RHEL 8/7上SSH的两个因素(2FA)身份验证如何配置。

我们将用于本练习的第二层安全性是Google验证者。
我们将使用可用于Android(在播放商店)和iOS(INTUNES中)的Google身份验证器应用程序来生成身份验证代码。

需要什么?

服务器运行RHEL/CentOS 8/7A手机运行Android或者IOSGoogle身份验证器ApplicationA配置SSH ConnectionYour可用性以设置两个因子身份验证

第1步:安装EPEL存储库

我们需要安装epel存储库并工作,以便可以安装依赖项。
使用下面的教程将EPEL存储库添加到RHEL/CentOS 8系统中。

如何在RHEL/CentOS 7/8上安装EPEL存储库

第2步:安装和配置所需的包

我们需要从epel存储库安装Google Authenticator PAM模块。
首先,确认包是否可用。

$sudo yum search google-authenticator
Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 0:01:53 ago on Sat 29 Dec 2016 10:09:22 AM EAT.
========================================================== Name Matched: oogle-authenticator ==========================================================
google-authenticator.x86_64 : One-time pass-code support using open standards

确认后,继续安装它 qrencode使用DNF或者YUM包管理器。

sudo dnf -y install google-authenticator qrencode

Google Authenticator软件包包含一个插件身份验证模块(PAM),允许使用符合开放标准的一次性通行码登录,该计划由开放式身份验证(OATH)(与OAuth不相关)。

有关已安装包的更多详细信息,可以使用:

$rpm -qi google-authenticator
 Name        : google-authenticator
 Version     : 1.04
 Release     : 1.el7
 Architecture: x86_64
 Install Date: Sat 29 Dec 2016 10:12:50 AM EAT
 Group       : Unspecified
 Size        : 98922
 License     : ASL 2.0
 Signature   : RSA/SHA256, Thu 17 Aug 2016 03:27:00 AM EAT, Key ID 6a2faea2352c64e5
 Source RPM  : google-authenticator-1.04-1.el7.src.rpm
 Build Date  : Thu 17 Aug 2016 03:13:19 AM EAT
 Build Host  : buildhw-06.phx2.fedoraproject.org
 Relocations : (not relocatable)
 Packager    : Fedora Project
 Vendor      : Fedora Project
 URL         : https://github.com/google/google-authenticator-libpam/
 Summary     : One-time pass-code support using open standards
 ......

第3步:配置SSH服务器

安装后,我们需要使SSH使用Google Authenticator PAM模块。
为此,请打开文件 /etc/pam.d/sshd并在最后添加以下行。

$sudo vi /etc/pam.d/sshd
# Add to end
auth required pam_google_authenticator.so

然后修改 /etc/ssh/sshd_config

$sudo vi /etc/ssh/sshd_config
ChallengeResponseAuthentication yes

改变 ChallengeResponseAuthenticationnoyes启用挑战 - 响应密码。

第4步:配置身份验证

使用Google Authenticator,配置双因素身份验证是在公园散步。
这需要为每个用户帐户完成,以便能够登录。

在终端中,运行Google-Authenticator命令。

$google-authenticator

这将询问我们一系列问题,这是一个推荐的配置:使用"基于时间的"时间基于时间的标记:YesUpdate .google_authenticator文件:YesDisAllow同一身份验证令牌的多个用途:Yesincrease原始生成时间限制:可噪声速率限制:是的

如果我们无法访问手机,我们将被赋予秘密密钥,验证码和紧急临时代码。
将它们写在纸或者记事本上,并保持安全。

Your new secret key is: DKM6MJWQVGZHLTWJ4G45XXXXXX
Your verification code is 869XXX
Your emergency scratch codes are:
 2746XXXX
 2665XXXX
 3671XXXX
 2271XXXX
 8000XXXX

安装和配置Google Authenticator

Google Authenticator应用程序可用于Android(在Play Store)和IOS(IN ITUNES中)以生成身份验证代码。

下载并安装它。

按照提示完成设置,然后选择扫描条形码或者输入私钥。

在安装过程中扫描在屏幕中打印的条形码或者添加密钥以添加SSH帐户。

我们应该看到添加到Google Authenticator的帐户和

测试SSH两因素身份验证

尝试向服务器启动新SSH。

$ssh rhel8 
Password: <Enter SSH Password>
Verification code: <Enter Verificarion code on Google Authenticator>
Activate the web console with: systemctl enable --now cockpit.socket
Last failed login: Sat Dec 29 11:51:46 EAT 2016 from 192.168.122.1 on ssh:notty
Last login: Sat Dec 29 11:48:31 2016
[Hyman@theitroad ~]$