Loading

SSH同时开启key和2FA两步认证

背景

对于暴露在公网的SSH端口一定要做好安全防范
关闭密码认证强制公钥是最基本的要求无许多言
通常还会安装fail2ban对爆破扫描进行拦截
为了进一步提高安全性还可以开启2FA两步认证

过程

所谓2FA(two factor authentication)是传统认证的额外保障
类似早年网银的令牌,2FA使用具有时效性的动态验证码实现,具体通过
移动端安装Google Authenticator配合服务端libpam-google-authenticator完成鉴权

安装

参考ubuntu官方教程进行安装

sudo apt install libpam-google-authenticator

绑定

切换到你需要登陆的用户执行google-authenticator命令,建议设置:

  • Make tokens “time-base”: yes
  • Update the .google_authenticator file: yes
  • Disallow multiple uses: yes
  • Increase the original generation time limit: no
  • Enable rate-limiting: yes

随后使用Google Authenticator扫描终端二维码进行绑定,保存紧急代码以备不时之需。

配置

/etc/pam.d/sshd进行修改,注意这里跟官方建议不同
官方修改方式在使用公钥认证后直接跳过了2FA的验证码
本文修改确保使用公钥认证的前提下强制追加2FA验证码

# Standard Un*x authentication.
#@include common-auth

# Require authenticator, if not configured then allow
auth    required    pam_google_authenticator.so nullok
auth    required    pam_permit.so

随后修改/etc/ssh/sshd_config绑定2FA验证

UsePAM yes
PasswordAuthentication no
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
PermitEmptyPasswords no

最后重启sshd服务生效

sudo /etc/init.d/ssh restart

分发

针对多人使用场景,可以将token分发到其他人的Google Authenticator实现登陆,
具体方法在app中选择export后用其他终端扫描生成的二维码,选择保留原token即可。

后记

引用一段全球通用的安全准则:

  1. 对于暴露在网络上的任何东西,密码一定要足够强
  2. 保持谦逊是美德,也是保护您不被人攻击的隐身咒
  3. 如果您知道一个东西以漏洞闻名,那就为它多加防护
  4. 如果没有特殊需求,使用最新版程序通常都是一个好选择

参考

Configure SSH to use two-factor authentication | Ubuntu
debian - SSH public key authentication with google authenticator still asks for password - Server Fault
安全指南

posted @ 2022-07-09 16:16  azureology  阅读(767)  评论(0)    收藏  举报