如何为SSH登录建立双因子验证机制(谷歌身份验证器)?

译文
安全 数据安全
默认情况下,SSH已经在远程机器之间使用安全的数据通信;但是如果你想为自己的SSH连接添加另外某种安全层,可以添加谷歌身份验证器(Google Authenticator)双因子验证模块,该模块让你可以在连接到SSH服务器时,输入一个随机的一次性密码(TOTP)验证码

默认情况下,SSH已经在远程机器之间使用安全的数据通信;但是如果你想为自己的SSH连接添加另外某种安全层,可以添加谷歌身份验证器(Google Authenticator)双因子验证模块,该模块让你可以在连接到SSH服务器时,输入一个随机的一次性密码(TOTP)验证码。你在连接时,就得从智能手机或个人电脑输入这个验证码。

谷歌身份验证器是一种开源模块,实施了由谷歌开发的一次性通行码(TOTP)验证令牌。除了PAM(可插入验证模块)外,它还支持几种移动平台。这些一次性通行码是使用OATH(开放验证组织)制定的开放标准生成的。

SSH双因子验证

SSH双因子验证

我在本文中将为大家介绍如何在红帽、CentOS、Fedora、Ubuntu、Linux Mint和Debian等系统环境下,建立和配置SSH,以实现双因子验证机制。

安装谷歌身份验证器模块

打开你想建立双因子验证机制的那个机器,安装下列PAM库以及PAM模块与谷歌身份验证器模块顺利协同运行所需要的开发库。

在红帽、CentOS和Fedora等系统上,安装‘pam-devel’安装包。

# yum install pam-devel make gcc-c++ wget

在Ubuntu、Linux Mint和Debian等系统上,安装‘libpam0g-dev’安装包。

# apt-get install libpam0g-dev make gcc-c++ wget

下载谷歌身份验证器模块,并解压缩到Home目录下(假设你已经登录到根目录的home目录)。

# cd /root

# wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2

# tar -xvf libpam-google-authenticator-1.0-source.tar.bz2

输入下列命令,在系统上编译和安装谷歌身份验证器模块。

# cd libpam-google-authenticator-1.0

# make

# make install

# google-authenticator

一旦你运行了‘google-authenticator’命令,它会向你提示一系列问题。在大多数情况下,只要输入‘y’(yes)作为答案。要是出了什么问题,你可以再次输入‘google-authenticator’命令,以便重置设置。

◦你希望验证令牌基于时间吗(y/n)?y

在这个问题之后,你会获得你的“密钥”和“应急验证码”。把这些详细信息记在某个地方,以后我们在安装谷歌身份验证器应用程序时要用到“密钥”。

[root@tecmint libpam-google-authenticator-1.0]# google-authenticator

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

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@tecmint.com%3Fsecret%3DXEKITDTYCBA2TLPL

Your new secret key is: XEKITDTYCBA2TLPL

Your verification code is 461618

Your emergency scratch codes are:

65083399

10733609

47588351

71111643

92017550

#p#

下一步,按安装向导的提示进行操作。在大多数情况下,只要输入‘y’(yes)作为答案,如下所示。

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

你希望我更新你的“/root/.google_authenticator”文件吗(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

你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒,

但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?y

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) y

默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,

我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题,

可以将窗口从默认大小即1分30秒加大到约4分。你希望这么做吗(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

如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块

启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。

你希望启用尝试次数限制吗(y/n)?y

配置SSH,以便使用谷歌身份验证器模块

打开PAM配置文件‘/etc/pam.d/sshd’,把下面这行添加到该文件的顶部。

auth required pam_google_authenticator.so

下一步,打开SSH配置文件‘/etc/ssh/sshd_config’,精细滚动,找到显示下面内容的这一行:

ChallengeResponseAuthentication no

把它更改成“yes”。所以,它现在变成了这样子:

ChallengeResponseAuthentication yes

最后,重启SSH服务,以便让新的变更生效。

# /etc/init.d/sshd restart

#p#

配置谷歌身份验证器应用程序

在智能手机中运行谷歌身份验证器应用程序。摁Menu(菜单),选择“Setup an account”(建立一个帐户)。如果你没有这个应用程序,可以下载谷歌身份验证器应用程序(http://support.google.com/accounts/bin/answer.py?hl=en&answer=1066447),并安装到你的安卓/iPhone/黑莓设备上。

谷歌身份验证器建立帐户

谷歌身份验证器建立帐户

摁“Enter key provided”(输入提供的密钥)。

输入谷歌身份验证器密钥

输入谷歌身份验证器密钥

添加你的帐户“Name”(名称),输入之前生成的“密钥”。

谷歌身份验证器帐户名称和密钥

谷歌身份验证器帐户名称和密钥

它会生成一次性密码(验证码),该一次性密码在你的手机上每30秒就会不断变化。

谷歌身份验证器一次性密码

谷歌身份验证器一次性密码

现在试着通过SSH进行登录,你会发现:每当你试图通过SSH进行登录,系统会提示你输入谷歌身份验证器代码(验证码)和密码。你只有30秒的时间来输入这个验证码;要是错过了时间,它会重新生成新的验证码。

login as: tecmint

Access denied

Using keyboard-interactive authentication.

Verification code:

Using keyboard-interactive authentication.

Password:

Last login: Tue Apr 23 13:58:29 2013 from 172.16.25.125

[root@tecmint ~]#

如果你没有智能手机,还可以使用一个名为GAuth Authenticator的火狐插件(https://marketplace.firefox.com/app/gauth-authenticator/),实现双因子验证。

这点很重要:双因子验证与基于密码的SSH登录配套使用。要是你使用任何私钥/公匙SSH会话,它会忽视双因子验证,让你直接登录进去。

原文地址:http://www.tecmint.com/ssh-two-factor-authentication/

责任编辑:蓝雨泪 来源: 51CTO.com
相关推荐

2018-10-23 15:31:12

2019-05-07 08:15:21

2010-09-06 11:24:47

CHAP验证PPP身份验证

2021-08-03 14:48:05

ChromeAndroid身份验证

2012-02-20 09:55:41

ibmdw

2019-03-24 19:16:35

FedoraSSH系统运维

2014-05-21 13:39:56

ApacheSSH双因子验证

2020-08-23 09:04:04

SSH身份验证FIDO2 USB

2011-06-28 11:10:19

2022-05-07 13:19:24

GitHub2FA

2022-04-01 09:00:00

Linux安全SSH

2010-04-20 10:00:29

2013-05-02 16:31:12

双因素身份验证微软

2010-07-19 17:30:47

2016-06-14 10:01:12

2022-10-31 10:00:00

2012-04-10 09:36:58

2019-11-21 14:53:10

多因子身份验证MFA密码

2013-07-21 18:32:13

iOS开发ASIHTTPRequ

2011-02-21 10:54:45

点赞
收藏

51CTO技术栈公众号