用Google Authenticator加强SSH登录安全性

简介:

环境:
CentOS 6.3 x86_64

基础组件安装:

1
yum -y  install  wget gcc  make  pam-devel libpng-devel


一、安装qrencode
在Linux上,有一个名为 QrenCode 的命令行工具可以很容易帮我们生成二维码,google authenticator命令行生成二维码就是调用它


1
2
3
4
wget http: //fukuchi .org /works/qrencode/qrencode-3 .3.1. tar .gz
tar  zxf qrencode-3.3.1. tar .gz
cd  qrencode-3.3.1
. /configure  --prefix= /usr  &&  make  &&  make  install


二、安装google authenticator PAM插件



1
2
3
4
wget http: //google-authenticator .googlecode.com /files/libpam-google-authenticator-1 .0- source . tar .bz2
tar  jxf libpam-google-authenticator-1.0- source . tar .bz2
cd  libpam-google-authenticator-1.0
make  &&  make  install


三、配置google authenticator

Google Authenticator 其实是一套开源的解决方案,所以不仅在 Google 的网站上能用,在其他地方也能用的。然而,在 Google 的网站上,会直接给你一个 QR 码让你扫的,而自己配置的 Google Authenticator 则要自己生成了。
首先需要切换到对应的用户,如果 VPS 上只有一个用户的话,自然是可以省略这一步的,但是多用户的 VPS 需要先切换到对应的用户,再运行 google-authenticator 命令,程序会问你Do you want authentication tokens to be time-based (y/n),大意是基于时间生成验证码(及TOTP),这里选择y。结果类似这样:

ssh-ga01.png
这个 QR 码自然是给 Google Authenticator 应用程序来扫描的,也可以访问上面的那个链接,用 Google Chart API 生成的 QR 码来扫描。还可以照着 QR 码下面的文字密钥手工输入。当 Google Authenticator 识别了这个账号之后,验证器就配置好了。在文字密钥下面还提供了几个应急码,为手机丢了等情况下所用的,可以妥善保管。
这时 Google Authenticator 虽然运行了,但是相关设置还没有保存,程序会问你 Do you want me to update your “/root/.google_authenticator” file (y/n) (是否将配置写入家目录的配置文件),当然是回答 y 了。又会问

Do you want to disallow multiple uses of the same authenticationtoken? This restricts you to one login about every 30s, but it increasesyour chances to notice or even prevent man-in-the-middle attacks (y/n)

大意是说是否禁止一个口令多用,自然也是答 y。下一个问题是

By default, tokens are good for 30 seconds and in order to compensate forpossible time-skew between the client and the server, we allow an extratoken before and after the current time. If you experience problems with poortime synchronization, you can increase the window from its defaultsize of 1:30min to about 4min. Do you want to do so (y/n)

大意是问是否打开时间容错以防止客户端与服务器时间相差太大导致认证失败。这个可以根据实际情况来。我的ipad时间很准(与网络同步的),所以答 n,如果一些平板电脑不怎么连网的,可以答 y 以防止时间错误导致认证失败。再一个问题是

If the computer that you are logging into isn't hardened against brute-forcelogin 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。
问题答完了,家目录中多出一个 .google_authenticator 文件(默认权限为 400),这时客户端与服务端已经配套起来了,以后不用再运行 google-authenticator 命令了,否则会重新生成一组密码。

四、配置SSH验证
此时虽然 Google Authenticator 已经配置好了,但是并没有任何程序会去调用它。所以需要设置 SSH 登录的时候去通过它验证。
打开 /etc/pam.d/sshd 文件,添加
auth required pam_google_authenticator.so

这一行,保存。再打开 /etc/ssh/sshd_config 文件,找到
ChallengeResponseAuthentication no

把它改成
ChallengeResponseAuthentication yes

并保存。最后,输入
service ssh restart

来重启 SSH 服务以应用新的配置。
这时候再用 SSH 登录的话就会这样了:

ssh-ga02.png

这样就成功了。






     本文转自ljl_19880709 51CTO博客,原文链接:http://blog.51cto.com/luojianlong/1381725 ,如需转载请自行联系原作者




相关文章
|
5月前
|
Shell 网络安全 数据安全/隐私保护
debian安装ssh(傻瓜教程)+证书免密登录
debian安装ssh(傻瓜教程)+证书免密登录
361 0
|
5月前
|
网络安全
SNETCracker.exe 2022/1/26 31:13:29----检查12.26.20.1:SSH登录发生异常!未能加载文件或程序集“ChilkatDotNet4.dll”或它的某一个依赖项
SNETCracker.exe 2022/1/26 31:13:29----检查12.26.20.1:SSH登录发生异常!未能加载文件或程序集“ChilkatDotNet4.dll”或它的某一个依赖项
146 0
|
2月前
|
安全 网络协议 Linux
|
6月前
|
运维 搜索推荐 Shell
Ansible自动化运维工具之个性化定制SSH连接登录端口(3)
Ansible自动化运维工具之个性化定制SSH连接登录端口(3)
186 0
|
6月前
|
安全 Linux 网络安全
百度搜索:蓝易云 ,Linux Debian11服务器安装SSH,创建新用户并允许SSH远程登录,及SSH安全登录配置!
这些步骤提供了在Debian 11服务器上安装SSH,创建新用户并允许SSH远程登录以及进行SSH安全登录配置的指南。请确保按照步骤操作,并根据您的需求进行必要的修改。
100 0
|
2月前
|
关系型数据库 网络安全 数据库
通过SSH登录OceanBase数据库需要修改用户密码,然后使用SSH客户端进行远程登录
通过SSH登录OceanBase数据库需要修改用户密码,然后使用SSH客户端进行远程登录
76 6
|
7月前
|
Ubuntu 安全 Shell
Ubuntu服务器安全性提升:修改SSH默认端口号
在Ubuntu服务器上,SSH(Secure Shell)是一种至关重要的远程连接工具。它提供了一种安全的方式来远程连接和管理计算机系统,通过加密通信来确保数据的保密性和完整性。SSH协议广泛用于计算机网络中,用于远程管理、文件传输和安全通信等任务。然而,SSH默认使用的端口号是22,这也是黑客们常常尝试入侵的目标之一。为了提高服务器的安全性,我们通常建议修改SSH的默认端口号,以减少未经授权的访问和潜在的风险。接下来,我们将详细介绍如何在Ubuntu上执行这一关键操作。
247 4
Ubuntu服务器安全性提升:修改SSH默认端口号
|
2月前
|
网络安全 数据安全/隐私保护
如何使用ssh key免密码登录服务器?
如何使用ssh key免密码登录服务器?
|
3月前
|
安全 Linux Shell
Linux服务器配置SSH免密码登录后,登录仍提示输入密码(一次真实的问题排查解决记录)
Linux服务器配置SSH免密码登录后,登录仍提示输入密码(一次真实的问题排查解决记录)
94 0
|
3月前
|
安全 Linux 网络安全
服务器设置 SSH 通过密钥登录
服务器设置 SSH 通过密钥登录