千家信息网

用Google Authenticator加强SSH登录安全性

发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,环境:CentOS 6.3 x86_64基础组件安装:yum -y install wget gcc make pam-devel libpng-devel一、安装qrencode在Linux上,有一
千家信息网最后更新 2025年12月04日用Google Authenticator加强SSH登录安全性

环境:
CentOS 6.3 x86_64

基础组件安装:

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


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


wget http://fukuchi.org/works/qrencode/qrencode-3.3.1.tar.gztar zxf qrencode-3.3.1.tar.gzcd qrencode-3.3.1./configure --prefix=/usr && make && make install


二、安装google authenticator PAM插件



wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2tar jxf libpam-google-authenticator-1.0-source.tar.bz2cd libpam-google-authenticator-1.0make && 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。结果类似这样:


这个 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 登录的话就会这样了:


这样就成功了。


配置 生成 时间 命令 文件 用户 程序 自然 验证 大意 问题 服务 运行 登录 客户 客户端 密钥 情况 文字 目录 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手机软件开发app软件 河北省二院网络安全招标 天津拓普网络技术公司 如何用笔记本配置服务器ip 网络安全王柱宇 重庆惠普服务器续保一年多少钱 客户机没办法访问虚拟服务器 公安类网络安全专业能分配到哪里 如何登陆服务器管理网站 教室管理系统数据库设计说明书 txt文件追加数据库 华为高斯数据库日期函数 数据库系统的外模式又称子模式吗 诺特软件开发有限公司 概况 简单的网络安全手抄报一个字 快吧游戏盒服务器维修多久 天津将至网络技术有限公司副总 大国网络安全博弈论文 数据库技术与项目笔记 网络安全改编歌曲少年 贺州市国产服务器供应公司 网络安全法实施影响 山西职业软件开发系统 ftp服务器连不上 山西java软件开发咨询客服 上官网络软件开发公司 安徽网络时间同步服务器云服务器 厦门赢飞软件开发有限公司 打电话报服务器错误 网络安全保护工程师考试科目
0