在 Fedora 上为 SSH 设置双因子验证
创始人
2024-03-02 02:07:28
0

每天似乎都有一个安全漏洞的新闻报道,说我们的数据会因此而存在风险。尽管 SSH 是一种远程连接系统的安全方式,但你仍然可以使它更安全。本文将向你展示如何做到这一点。

此时 双因子验证 two-factor authentication (2FA)就有用武之地了。即使你禁用密码并只允许使用公钥和私钥进行 SSH 连接,但如果未经授权的用户偷窃了你的密钥,他仍然可以借此访问系统。

使用双因子验证,你不能仅仅使用 SSH 密钥连接到服务器,你还需要提供手机上的验证器应用程序随机生成的数字。

本文展示的方法是 基于时间的一次性密码 Time-based One-time Password (TOTP)算法。Google Authenticator 用作服务器应用程序。默认情况下,Google Authenticator 在 Fedora 中是可用的。

至于手机,你可以使用与 TOTP 兼容的任何可以双路验证的应用程序。Andorid 或 iOS 有许多可以与 TOTP 和 Google Authenticator 配合使用的免费应用程序。本文与 FreeOTP 为例。

安装并设置 Google Authenticator

首先,在你的服务器上安装 Google Authenticator。 $ 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

这个应用程序为你提供一个密钥、验证码和恢复码。把它们放在安全的地方。如果你丢失了手机,恢复码是访问服务器的唯一方式。

设置手机验证

在你的手机上安装验证器应用程序(FreeOTP)。如果你有一台安卓手机,那么你可以在 Google Play 中找到它,也可以在苹果 iPhone 的 iTunes 商店中找到它。

Google Authenticator 会在屏幕上显示一个二维码。打开手机上的 FreeOTP 应用程序,选择添加新账户,在应用程序顶部选择二维码形状工具,然后扫描二维码即可。设置完成后,在每次远程连接服务器时,你必须提供验证器应用程序生成的随机数。

完成配置

应用程序会向你询问更多的问题。下面示例展示了如何设置合理的安全配置。

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 连接,因为我们将禁用密码连接。如果出现问题或错误,一个已经建立的连接将允许你修复问题。

在你的服务器上,使用 sudo 编辑 /etc/pam.d/sshd 文件。

$ sudo vi /etc/pam.d/ssh

注释掉 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:

验证码由你手机上的验证器应用程序随机生成。由于这个数字每隔几秒就会发生变化,因此你需要在它变化之前输入它。

如果你不输入验证码,你将无法访问系统,你会收到一个权限被拒绝的错误:

[user@client ~]$ ssh user@example.com
Verification code:
Verification code:
Verification code:
Permission denied (keyboard-interactive).
[user@client ~]$

结论

通过添加这种简单的双路验证,现在未经授权的用户访问你的服务器将变得更加困难。


via: https://fedoramagazine.org/two-factor-authentication-ssh-fedora/

作者:Curt Warfield 选题:lujun9972 译者:MjSeven 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关内容

ssh 登录超时 expe...
ssh -p 22 root@192.168.1.1 -v...
2025-05-29 15:17:15
添加已有的PublicKe...
mkdir -p ~/.ssh/ To overwrite aut...
2025-05-29 06:01:59
不知道如何通过telegr...
连续循环时,通常使用while loop或for loop,但在T...
2025-01-12 06:30:56
不需要SSH连接远程Mon...
要不需要SSH连接远程MongoDB服务器,可以使用以下方法之一:...
2025-01-10 15:00:48
不同操作系统的客户端与SS...
不同操作系统的客户端与SSH服务器的行为不同:分段错误可能是由于操...
2025-01-08 09:00:54
不使用添加的SSH密钥在n...
在nushell中使用Git时,如果你没有使用添加SSH密钥的权限...
2024-12-29 11:30:55

热门资讯

“Linux 中国” 开源社区... 这是一个艰难的决定 —— 这不是调侃,而是当你真正做出改变了你十几年生活轨迹的决定时,你就会明白这个...
硬核观察 #1263 德国铁路... #1 德国铁路公司招聘 Windows for Workgroups 3.11 管理员该职位要求了解...
人工智能教程(七):Sciki... 在本系列的 上一篇文章 中,我们用 TensorFlow 构建了第一个神经网络,然后还通过 Kera...
关闭 WSL 中正在运行的 L... 以下是使用 WSL 关闭在 Windows 内运行的 Linux 系统的不同方法。你使用 WSL 在...
MX Linux 23.2 “... 最新的 MX Linux 23.2 小版本更新带来了基本的改进和新增功能。MX Linux 推出了 ...
硬核观察 #1262 网络上最... #1 网络上最古老的软件存档库之一即将关闭美国新墨西哥州立大学(NMSU)最近宣布,即将于 2024...
如何在 RHEL 上通过绑定配... 在 Linux 中,你可以根据自己的需要创建高级网络设置,如 网络绑定 Network Bondin...
Linux Matrix 消息... Matrix 用户可以升级 Fractal 6 了!Fractal 是目前 最好的 Matrix 客...
如何通过 VLC 使用字幕 使用 VLC 媒体播放器播放和管理字幕的新手指南。我是一个超级动漫迷,如果你想观看最新的剧集,那么你...
硬核观察 #1261 X 计划... #1 X 计划在奥斯汀建立内容审核总部据报道,该部门将招聘 100 名全职员工,将主要关注 CSE ...