如何实现SSH通过扫二维码登录Linux服务器
发表于:2025-12-05 作者:千家信息网编辑
千家信息网最后更新 2025年12月05日,背景Linux服务器通过ssh远程连接的时候,如果使用的是用户名+密码的验证方式,万一密码泄露或者密码过于简单被暴li破解,服务器的安全就得不到保障。由此,可以结合谷歌动态验证口令来为服务器再加一道安
千家信息网最后更新 2025年12月05日如何实现SSH通过扫二维码登录Linux服务器
背景
Linux服务器通过ssh远程连接的时候,如果使用的是用户名+密码的验证方式,万一密码泄露或者密码过于简单被暴li破解,服务器的安全就得不到保障。由此,可以结合谷歌动态验证口令来为服务器再加一道安全防护门。此时,就算是用户密码被泄露,但没有动态验证口令也是无法登录服务器的。
环境
OS:CentOS7
软件包:google-authenticator.x86_64
安卓手机一部(某米)
谷歌动态口令的下载及使用自行百度
操作步骤
1.一键安装脚本
#安装epelyum install -y epel-release.noarch &> /dev/nullyum makecache &> /dev/null#安装google authenticatoryum install -y google-authenticator.x86_64 &> /dev/nullecho -e "\033[31mDo you want me to update your "/root/.google_authenticator" file? (y/n) y"echo -e "\033[31m你希望我更新你的"/root/.google_authenticator"文件吗(y/n)?\033[0m"echo -e "\033[31mDo you want to disallow multiple uses of the same authentication"echo -e "\033[31mtoken? This restricts you to one login about every 30s, but it increases"echo -e "\033[31myour chances to notice or even prevent man-in-the-middle attacks (y/n) y"echo -e "\033[31m你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现甚至防止中间人攻ji的可能性(y/n)?\033[0m"echo -e "\033[31mBy default, a new token is generated every 30 seconds by the mobile app."echo -e "\033[31mIn order to compensate for possible time-skew between the client and the server,"echo -e "\033[31mwe allow an extra token before and after the current time. This allows for a"echo -e "\033[31mtime skew of up to 30 seconds between authentication server and client. If you"echo -e "\033[31mexperience problems with poor time synchronization, you can increase the window"echo -e "\033[31mfrom its default size of 3 permitted codes (one previous code, the current"echo -e "\033[31mcode, the next code) to 17 permitted codes (the 8 previous codes, the current"echo -e "\033[31mcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes"echo -e "\033[31mbetween client and server."echo -e "\033[31mDo you want to do so? (y/n) y"echo -e "\033[31m默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,\033[0m"echo -e "\033[31m我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3个可通过验证码增加到17个可通过验证码,\033[0m"echo -e "\033[31m这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)?\033[0m"echo -e "\033[31mIf the computer that you are logging into isn't hardened against brute-force"echo -e "\033[31mlogin attempts, you can enable rate-limiting for the authentication module."echo -e "\033[31mBy default, this limits attackers to no more than 3 login attempts every 30s."echo -e "\033[31mDo you want to enable rate-limiting? (y/n) y"echo -e "\033[31m如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块\033[0m"echo -e "\033[31m启用尝试次数限制。默认情况下,这限制攻ji者每30秒试图登录的次数只有3次。 你希望启用尝试次数限制吗(y/n)?\033[0m"echo -e "\033[32m 在App Store 搜索Google Authenticator 进行App安装 \033[0m"expect << EOFspawn google-authenticatorexpect { "y/n" {send "y\n";exp_continue} "y/n" {send "y\n"} "y/n" {send "y\n"} "y/n" {send "y\n"}}EOF#/etc/pam.d/sshd文件,修改或添加下行保存#auth required pam_google_authenticator.sosed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd#编辑/etc/ssh/sshd_config找到下行#ChallengeResponseAuthentication no#更改为#ChallengeResponseAuthentication yessed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config#重启SSH服务systemctl restart sshd2.运行脚本后直接扫屏幕生成的二维码或者复制二维码上方的URL到浏览器打开后扫码,然后使用扫码生成的动态口令进行登录
2.1打开远程连接工具xshell,如图
2.2输入动态口令
2.3输入服务器密码
2.4查看/var/log/secure日志可以发现ssh远程登录通过了pam_google_authenticator动态模块的验证并且成功登录服务器
遇到的问题
1.使用某米手机扫二维码的时候出现如下提示
于是安装了Google play应用,但是打开后闪退,搜索相关资料后貌似是没有谷歌服务框架的原因,于是又下载了谷歌服务框架应用,仍然没有成功,可能科学上网可以解决此问题。
由于时间问题没再折腾,后面换了某为手机就行了。
2.如果手机扫码不成功,可以chrome 网上应用店搜索"身份验证器"插件,然后添加到Chrome浏览器进行扫码
3.如果手机丢失或者其它情况导致无法获取到动态口令,可以使用紧急刮刮码来登录服务器,默认配置文件是/root/.google_authenticator,不过这里面的刮刮码用一个少一个,建议保存到安全的地方。
服务
服务器
登录
验证
动态
口令
密码
手机
时间
问题
限制
二维
二维码
安全
成功
令牌
情况
文件
次数
应用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网站服务器设在美国为华人
舟山专业电子书软件开发靠谱团队
电源服务器适用哪些邻域
荣华网络技术服务有限公司
双核服务器怎么改单核运行
跟随网络技术
软件开发源码定制
大数据平台软件开发工程师
数据库大神都是怎么设计表的
kms服务器激活安全嘛
华为服务器如何清理数据
一般情况下密码储存在数据库哪里
网络安全文科生报哪个学校可以学
苹果手机服务器聊天记录
乌鲁木齐软件开发需要多少钱
计算机网络技术学测测试7
网络安全管理员配备要求
sas配置保存数据库
长春管理软件开发公司
我国网络安全面临问题
网络安全者包括
服务器虚拟化技术分几类
第一章网络技术基础与计算思维
直销软件开发xazxrj
上海数据传输网络技术哪家好
计算机网络安全有什么作用
服务器关硬盘报警声
启动管理服务器的快捷键
蓝田嵌入式软件开发
scala 数据库连接池