Technology Sharing

  • 首页
  • 资料分享
  • 在线工具
    • 随机密码生成器
  • 介绍
  • RSS
  • privacy
  • 云产品推广
    • 腾讯云
    • 阿里云
Share IT knowledge
  1. 首页
  2. linux
  3. 正文

ssh 2FA 服务器登录动态口令

2022年6月27日 242点热度 0人点赞 0条评论

采用google开源的二次认证系统Google Authenticator实现2FA,实现在管理员持有密钥的情况下,也需要同时获取动态口令才能进入服务器(动态口令30s刷新一次,每个动态口令只能使用一次【可设置多次】)。

2FA(双因素身份认证)就是通过你所知道(动态口令)再加上你所拥有(服务器密钥)的这二个要素组合到一起才能发挥作用的身份认证系统。
双因素认证是一种采用时间同步技术的系统,采用了基于时间、事件和密钥三变量而产生的一次性密码来代替传统的静态密码。
每个动态密码卡都有一个唯一的密钥,该密钥同时存放在服务器端,每次认证时动态密码卡与服务器分别根据同样的密钥,同样的随机参数(时间、事件)和同样的算法计算了认证的动态密码,从而确保密码的一致性,从而实现了用户的认证。

安装 google-authenticator

yum install epel-release 
yum install google-authenticator -y

注册 google-authenticator

google-authenticator

会有如下提示:

Do you want me to update your "/home/demo/.google_authenticator" file? (y/n) <是否更新“.google_authenticator”文件,输入“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, a new token is generated every 30 seconds by the mobile app.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. This allows for atime skew of up to 30 seconds between authentication server and client. If youexperience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the currentcode, the next code) to 17 permitted codes (the 8 previous codes, the currentcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server.
Do you want to do so? (y/n) <是否开启时间容错机制,密码通常是30s生成一次,输入“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”,每30s至多能够尝试登录3次> 

配置成功后,会输出二维码以及相关密钥等信息,系统也会在用户的主目录生成 .google_authenticator 配置文件(存放密钥以及备用口令)

配置ssh服务的验证方式

vim /etc/pam.d/sshd 

将ssh纯明文密码验证这一行注释掉:

#auth substack password-auth

在末尾加入以下行:

设置google_authenticator的使用对象,+号表示不使用,-号表示要使用

vim /etc/security/access-local.conf 

在所有地方(ALL),使用root身份登陆ssh,要使用google_authenticator

- : root : ALL

其他示例:(可选)
从某个IP地址,使用root身份登陆ssh,不使用双因素,也就是会绕过google_authenticator

+ : root : IP

修改sshd配置

vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
UsePAM yes
#设置第一步访问认证为服务器密钥,第二步为动态口令
AuthenticationMethods publickey,keyboard-interactive:pam

重启ssh服务

systemctl restart sshd

Google Authenticator 客户端下载

安卓下载地址(浏览器或者软件商店搜索Google Authenticator下载)
苹果下载地址(app store搜索Google Authenticator下载)
chrome插件:https://chrome.google.com/webstore/detail/gauth-authenticator/ilgcnhelpchnceeipipijaljkblbcobl?utm_source=chrome-ntp-ico

安装完成之后启动添加,会有两个填写框(用户名,密钥),用户名填写主机名字即可,密钥则由注册时生成,以下是浏览器插件示范:

注意:

此服务必须保证服务器时间和当地时间的同步

若想所有主机使用同一个google authenticator,需要将.google_authenticator 的内容复制到某个用户的家目录下,并修改权限

chmod 400 .google_authenticator  
chown 用户:用户 .google_authenticator

一定要进行权限的修改,否则程序识别不出文件将无法进行ssh访问

打赏微海报分享
标签: 暂无
最后更新:2024年1月12日

nico

Linux运维工程师 软考网络工程师 && 软考信息安全工程师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

nico

Linux运维工程师
软考网络工程师 && 软考信息安全工程师

最新 热点 随机
最新 热点 随机
linux部署JDK环境 [Solved] MariaDB import issue: Error at line 1: Unknown command '\-'. Could not retrieve mirrorlist http://mirrorlist.centos.org/ CentOS 7 Kubernetes如何删除卡在“Terminating”状态的命名空间 解决GitLab Runner签名无效 如何禁用 Ubuntu "Daemons using outdated libraries" 弹出窗口
如何在PHP中修改时区设置 Nginx 设置浏览器缓存过期时间 Openai Fine-tuning 介绍 Nginx 的 Mirror 功能简介 docker-compose部署elasticsearch+kibana Supervisor – 简化进程管理的得力工具
最近评论
woodcockkienzlelsj8o9+73s48g9rr3m0@gmail.com 发布于 10 个月前(07月17日) necessitatibus corporis et odit nam quo harum et c...
RonaldG 发布于 10 个月前(07月07日) Very interesting topic, thank you for putting up.&...
小黑 发布于 1 年前(12月28日) 不错
nico 发布于 2 年前(02月09日) 嘻嘻嘻!!!
Bruse 发布于 2 年前(02月09日) 我来啦!!!
nico 发布于 2 年前(12月10日) 方便查询
Justin 发布于 2 年前(12月10日) 写的很好,谢谢分享!我 Mark 一下~
nico 发布于 3 年前(11月18日) 主题的原因吧
Justin 发布于 3 年前(09月22日) 这篇文章的质量很高呀!写得很详细~ 话说你的文章是隐藏了发布时间吗?
Justin 发布于 3 年前(08月27日) 好家伙,这标题乍一看还以为你打算进军英文技术写作领域了🤓

COPYRIGHT © 2023 Technology Sharing. ALL RIGHTS RESERVED.

备案图标 皖公网安备34132402000202 皖ICP备2023004851号-1