给Linux VPS折腾上totp(两步验证)用于登录验证

之前提到过使用密钥登录SSH,这种方式禁用了密码,安全性上是没问题的。。。除非,你的私钥也泄露啦~不怕,为了应对这种极端情况,我们不妨弄个2FA组成“多重验证”。

安装Google Authenticator PAM module

我这里用的是debian系统,运行以下命令把Google Authenticator PAM module 安装进系统

apt-get update
apt-get install libpam-google-authenticator 

安装完,使用以下命令 google-authenticator --qr-mode=none开始配置过程。由于我这里制定了“–qr-mode=none”,所以整个过程李是不包含QRcode的,手机上需要手动输入secret key~图方便的话去掉“–qr-mode=none”即可

   $ google-authenticator
   Do you want authentication tokens to be time-based (y/n) y
   <这里是自动生成的二维码>
   Your new secret key is: ZVZG5UZU4D7MY4DH          (验证器配置密钥)
   Your verification code is 269371                  (输入验证器生成的验证码)
   Your emergency scratch codes are:                 (备用令牌码)
     70058954
     97277505
     99684896
     56514332
     82717798
   
   Do you want me to update your "/home/username/.google_authenticator" file (y/n) y
   (是否重新生成登录配置文件?)
   
   Do you want to disallow multiple uses of the same authentication
   token? This restricts you to one login about every 30s, but it increases
   your chances to notice or even prevent man-in-the-middle attacks (y/n) y
   (是否拒绝多次重复使用相同的令牌?这将限制你每30s仅能登录一次,但会提醒/阻止中间人攻击。)
   
   By default, tokens are good for 30 seconds and 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. If you experience problems with poor
   time synchronization, you can increase the window from its default
   size of 1:30min to about 4min. Do you want to do so (y/n) n
   (是否将窗口时间由1分30秒增加到约4分钟?这将缓解时间同步问题。)
   
   If the computer that you are logging into is not hardened against brute-force
   login 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
   (是否启用此模块的登录频率限制,登录者将会被限制为最多在30秒内登录3次。)

设置完成后,开始配置SSH部分

配置openssh

配置前请保持当前shell连接,避免被“反锁”的尴尬情况。我这里参考了

  • Linux服务器启用两步验证方式登录
  • 的文章,内容有删改!
    配置 /etc/pam.d/sshd 加入以下内容
    @include common-password
    auth required pam_google_authenticator.so nullok

    注释以下内容
    #@include common-auth
    配置/etc/ssh/sshd_config
    更改以下选项
    # Change to yes to enable challenge-response passwords (beware issues with
    # some PAM modules and threads)
    ChallengeResponseAuthentication yes

    添加以下内容
    UsePAM yes
    AuthenticationMethods publickey,password publickey,keyboard-interactive

    以上内容搞定后,就可以新开个ssh试试效果啦
    值得一提的是,vps的时间要和登录设备的时间相同,否者会导致登录失败,如果你的服务器不方便更新时间,可以修改 .google_authenticator ,添加以下内容
    " WINDOW_SIZE 17
    这样,单个code的过期时间将延长到4分钟。不过这样做会降低2FA的安全性,具体怎么选择全看你自己。

    参考链接:

     
             

    分享几个临时邮箱合集

    临时邮箱合集,以下这些小编亲测可用,值得收藏哦 👉 Temp-Mail Temp-Mail 👉 AdGuard Temp Mail AdGuard Temp Mail 👉 Guerrilla Mail Guerrilla Mail 👉 Best T...

    注册bgm账号提示“抱歉抱歉,帐户激活服务暂时不可用,请稍后再试”之解决办法

    今天尝试注册https://bangumi.tv/,注册前半段没啥说的,很简单。结果跑去激活账户遇到了 “抱歉抱歉,帐户激活服务暂时不可用,请稍后再试”…一阵无语过...

    本富科技锂电池充电器wifi连接密码是多少?

    搜了一圈,在某音找到了答案。。。 默认wifi密码 BFKJ1688 登录页面 http://192.168.4.1 调试密码 666666 希望能帮到你哈

    pantaloon 6月解密可获得游戏 Polterguys: Possession Party(附简易答案)

    参与地址 pantaloon ,输入名字和邮箱地址join获取验证码登入。 送的游戏是这个Polterguys: Possession Party 简单说下答案哈。。。解密苦手表示猜了好久,也...

    手动部署tuicV5到linux vps上

    刚刚折腾了下,成功安装tuicV5到小鸡上2333 ,还别说,速度和hysteria比起来也不差,用起来还不错~ 我这里分享下我折腾安装全过程,其实还挺简单的 #新建个...

    API切换cloudflare边缘证书颁发机构

    已知cf边缘证书颁发机构有四种 cf自家 、Google、let‘s encrypt、setcigo。其中cf自家证书有效期最长,有一年,其余皆是三个月。。。 可以用命令行手动切换为...