云网牛站
所在位置:首页 > Linux云服务器 > 启用Ubuntu Server的SSH多重身份验证

启用Ubuntu Server的SSH多重身份验证

2017-10-19 22:16:07作者:Linux编辑稿源:系统极客

身份验证对于Linux服务器管理来说是非常重要的步骤,通过验证过程可以证明你的权力和权限,例如:你是谁?你能做什么?通常情况下,大家都采用账户和密码的方式来登录 SSH,谨慎一点的 Linux 管理员会采用密钥对的方式来验证 SSH 用户身份。

大家都知道,SSH 默认是采用密码方式来进行身份验证的,即便你使用了 SSH 密钥来代替密码。由于还是单一因素的身份验证方式,一旦密钥泄漏,同样还是会危及到服务器安全。

为了解决这一问题,本文中我们将介绍如何启用 Ubuntu Server 的 SSH 多重身份验证(MFA,Multi-factor authentication)。启用之后,在登录 SSH 时需要验证你绑定的计算机或手机等不同的验证因素才能登录成功。当然,多重身份验证的因素可以包括:

密码或安全问题

验证程序或安全令牌

指纹或语音

常见的验证方式就是使用 OATH-TOTP 应用,例如 Google Authenticator(Microsoft Account 的两步验证功能就是用的它)。OATH-TOTP(基于时间的一次性开放密码)是一种开放协议,它用于生成不同的一次性使用密码,通常情况下为每 30 秒新生成一个 6 位验证码。

下面我们将介绍如何使用 OATH-TOTP 应用完成 SSH 登录认证,从而取代掉系统原本的 SSH 密码或密钥验证方式,使得服务器管理更安全。

 

安装libpam-google-authenticator

在本步骤中我们将安装和配置 Google PAM。

熟知 Linux 身份验证体系的朋友都知道,PAM 是 Linux 系统中验证用户身份的基础架构和认证模块。Google 开发的 OATH-TOTP 应用也安全兼容 PAM,所以我们可以使用 Google Authenticator 来完成 SSH 多重身份验证。

1.使用如下命令更新 Ubuntu 软件仓库缓存:

sudo apt-get update

2.安装 Google PAM:

sudo apt-get install libpam-google-authenticator

libpam-google-authenticator 安装好之后,我们便需要为每个要进行重身份验证的用户生成 TOTP key。这个 Key 是在用户基础上生成的,而不是系统全局。也就是说,要使用 TOTP 认证程序登录 SSH 的用户必需单独获取和维护自己的密钥。

google-authenticator

执行上述命令后,程序会提出问题,第一个问题是询问是否生成基于时间的认证令牌。Google PAM 支持基于时间或基于序列的访问令牌。使用基于序列的令牌时,每次访问需要将令牌代码递增;基于时间的访问令牌会在一定时间内随机变化,使用起来更像是 Google Authenticator,所以我们这里选择yes。

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

启用Ubuntu Server的SSH多重身份验证

在回答第一个问题之后,便会立即输出大量信息。包括:QR 码、新的安全密码、验证码及 5 个 8 位的紧急码。这些信息请务必妥善保存,相当重要。

此时请用 Google Authenticator 扫描输出的二维码将其添加到 Google 的 OATH-TOTP 应用当中,一旦添加成功,将会出现一个新的条目并每 30 秒刷新一次 6 位数令牌。

启用Ubuntu Server的SSH多重身份验证

剩下的就是告诉 Google PAM 如何工作,我们一步一步来看。

Do you want me to update your “~/.google_authenticator” file (y/n) y

表示将 key 和配置选项写入 .google_authenticator 文件,我们选择yes。

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

是否将使用过的密码配置为立即过期,这里通常选择yes,以防止有人截获。

By default, tokens are good for 30 seconds and 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 1:30min to about 4min. Do you want to do so (y/n) n

默认令牌有效时间为 30 秒并会自动补偿服务器与客户端之间的时间差,该选项表示是否将时间容差改为 4 分钟,我们选择No。

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

是否限制 30 秒内只能尝试 3 次登录,我们选择yes以防止人有暴力破解。

完成以上步骤后,Google PAM 的所有配置工作就已经完成了。

 

配置OpenSSH支持MFA

接下来,我们要配置 SSH 来使用 TOTP key,这需要告诉 SSH 使用 Google PAM 进行身份验证。

1.在/etc/pam.d/sshd文件最后添加如下内容:

auth required pam_google_authenticator.so nullok

启用Ubuntu Server的SSH多重身份验证

最后的nullok是告诉 PAM 此种身份验证方式为可选,此时用户仍然可同时使用 SSH 密码和密钥的方式进行登录。当我们测试使用 OATH-TOTP key 能够正确无误登录时,便可将nullo删除以指明强制使用 MFA。

2.接下来我们需要配置 SSH 支持此种方式的身份验证。编辑/etc/ssh/sshd_config配置文件,将其中的ChallengeResponseAuthentication配置为yes,再重启 SSH 服务:

sudo service ssh restart

至此,我们的 SSH 已经可以使用 Google Authenticator 进行验证了,是不是非常不错。最后,为了保证服务器安全,建议大家开启 SSH 密钥 + MFA 双重验证的方式,放弃对 SSH 密码验证方式的使用。

编辑/etc/ssh/sshd_config配置文件:

PasswordAuthentication no

UsePAM yes #下面增加

AuthenticationMethods publickey,keyboard-interactive

重要:在配置过程中大家务必在测试好使用 Google Authenticator 可以登录 SSH 之后再停用 SSH 密码或密钥验证方式,否则导致无法连接就 Over 了。

 

相关主题

Kali Linux安装SSH Server

精选文章
热门文章