云网牛站
所在位置:首页 > Linux安全 > 在Fedora系统上为SSH设置双因素身份验证的方法

在Fedora系统上为SSH设置双因素身份验证的方法

2019-03-13 11:40:59作者:戴进稿源:云网牛站

尽管SSH是一种远程连接到系统的安全方式,但你仍然可以使其更加安全,本文介绍在Fedora系统上为SSH设置双因素身份验证(双向身份验证)的方法。

在Fedora系统上为SSH设置双因素身份验证的方法

 

背景

即使你禁用密码并且只允许使用公钥和私钥进行SSH连接,未经授权的用户仍可以在窃取你的密钥时访问你的系统,这就是双因素身份验证(2FA)的用武之地。

使用双因素身份验证,你无法仅使用SSH密钥连接到服务器,你还需要在移动电话上提供由验证器应用程序显示的随机生成的数字。

基于时间的一次性密码算法(TOTP)是本文中显示的方法,Google身份验证器用作服务器应用程序,默认情况下,Fedora中提供Google身份验证器。

对于你的手机,可以使用与TOTP兼容的任何双向身份验证应用程序,Android或IOS有许多可与TOTP和Google身份验证器配合使用的免费应用程序,本文使用FreeOTP作为示例。

 

安装并设置Google身份验证器

首先,在你的服务器上安装Google身份验证器包:

$ sudo dnf install -y google-authenticator

运行该应用程序:

$ google-authenticator

该应用程序为你提供了一系列问题,下面的代码段显示了如何回答相当安全的设置:

Do you want authentication tokens to be time-based (y/n) y

Do you want me to update your "/home/user/.google_authenticator" file (y/n)? y

该应用程序为你提供密钥,验证码和恢复代码,将它们放在安全的地方,如果丢失手机,恢复代码是访问服务器的唯一方法。可多参考在Linux系统下更改或更新SSH密钥密码的方法一文。

 

设置手机身份验证

在你的手机上安装身份验证器应用程序(FreeOTP),如果有Android手机,可以在Google Play中找到它,也可以在Apple iPhone的iTunes商店中找到它。

QR码显示在屏幕上,打开手机上的FreeOTP应用程序,要添加新帐户,请在应用程序顶部选择QR码形状工具,然后扫描QR码,设置完成后,每次远程连接服务器时,都必须提供验证器应用程序生成的随机数。

 

完成配置

该申请提出了进一步的问题,以下示例显示了如何应对设置合理安全的配置:

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, tokens are good for 30 seconds. 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. If you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens). 

Do you want to do so? (y/n) n

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

现在,必须设置SSH以利用新的双向身份验证。

 

配置SSH

在完成此步骤之前,请确保已使用公共SSH密钥建立了有效的SSH连接,因为我们将禁用密码连接,如果出现问题或错误,建立连接将允许你解决问题。

在服务器上,使用sudo编辑/etc/pam.d/sshd文件(参考:配置Fedora系统以使用sudo的方法https://ywnz.com/linuxjc/4501.html):

$ sudo vi /etc/pam.d/sshd

注释掉auth substack password-auth行:

#auth substack password-auth

将以下行添加到文件的底部:

auth sufficient pam_google_authenticator.so

保存并关闭文件,接下来,编辑/etc/ssh/sshd_config文件:

$ sudo vi /etc/ssh/sshd_config

查找ChallengeResponseAuthentication行并将其更改为yes:

ChallengeResponseAuthentication yes

查找PasswordAuthentication行并将其更改为no:

PasswordAuthentication no

将以下行添加到文件的底部:

AuthenticationMethods publickey,password publickey,keyboard-interactive

保存并关闭该文件,然后重新启动SSH:

$ sudo systemctl restart sshd

 

测试双因素身份验证

当尝试连接到服务器时,系统会提示你输入验证码:

[user@client ~]$ ssh user@example.com

Verification code:

验证码由你的移动电话上的验证器应用程序随机生成,由于此数字每隔几秒就会更改一次,因此你需要在更改之前输入该数字:

在Fedora系统上为SSH设置双因素身份验证的方法

如果未输入验证码,将无法访问系统,并且将收到权限被拒绝错误,如下:

[user@client ~]$ ssh user@example.com

Verification code:

Verification code:

Verification code:

Permission denied (keyboard-interactive).

[user@client ~]$

 

结语

通过添加这种简单的双向身份验证,你现在已经使未经授权的用户更难以访问到服务器了。

 

相关主题

在Linux系统中允许或拒绝SSH访问特定用户或组的方法

精选文章
热门文章