👮免密登录服务器/服务器登录加 Auth

免密登录服务器

  1. 本地创建密钥 ssh-keygen -t rsa -b 4096
  1. 公钥复制到服务器,默认22端口,如果有改动,就 -p 端口 ssh-copy-id username@hostname
  1. 服务端SSH配置 路径/etc/ssh/sshd_config 修改

    bash 复制代码
    PubkeyAuthentication yes
    PasswordAuthentication no
    AuthorizedKeysFile .ssh/authorized_keys
    ClientAliveInterval 60
    ClientAliveCountMax 3
    1. Shell
    2. PasswordAuthentication 会允许有密钥客户端免密登录,后两处修改增加超时配置 注意文件权限配置
    3. ~/.ssh 目录设置为700, authorized_key设置为600
    bash 复制代码
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    1. Shell
  1. 重启SSH服务 sudo systemctl restart sshd

服务器登录加 Auth(使用 Google Auth)

  1. 在 Ubuntu 服务器上安装所需的软件包

    sql 复制代码
    sudo apt update
    sudo apt install libpam-google-authenticator
    1. Shell
  1. 切换到目标用户,运行 auth

    bash 复制代码
    # 切换到目标用户
    sudo su - your_username
    # 运行 google-authenticator 命令
    google-authenticator
    1. Shell
  • auth运行之后,会出现
    • "Do you want authentication tokens to be time-based (y/n)" : 输入 y。这是基于时间的 OTP (TOTP),Google Authenticator 应用使用的就是这种方式。
    • 接下来,屏幕会显示:
      • 一个大的二维码 (QR code) :需要用手机上的 Google Authenticator 应用扫描这个二维码。
      • "Your new secret key is: [一串字母数字]" :这是秘密密钥。务必安全地记录下这串密钥。如果手机丢失或损坏,可以用它来重新设置 Google Authenticator。
      • "Your verification code is: [6位数字]" :当前的验证码。
      • "Your emergency scratch codes are:" :这是 非常重要 的一组紧急备用码。请务必将它们打印出来或安全地存储在离线的地方。 如果手机无法使用,或者验证码无法通过,这些备用码可以登录一次。每个备用码只能使用一次。
    • "Do you want to update the "~/.google_authenticator" file? (y/n)" : 输入 y。这会将配置保存到用户的家目录下的 .google_authenticator 文件中。
    • "Do you want to disallow multiple uses of the same authentication token? (y/n)" : 输入 y。这会防止重复使用同一个验证码,增强安全性。
    • "By default, tokens are good for 30 seconds. Do you want to increase the window of vulnerability to 90 seconds? (y/n)" : 输入 n。保持默认的 30 秒窗口更安全。如果经常遇到验证码过期的问题,可以考虑改为 y,但会略微降低安全性。
    • "Do you want to enable rate-limiting? (y/n)" : 输入 y。这会限制在短时间内尝试的验证码次数,防止暴力破解。
    • 用手机扫描二维码或手动输入密钥: 打开手机上的 Google Authenticator 应用 (或 Authy 等兼容应用),添加一个新的账户。选择扫描二维码,或者选择手动输入密钥,然后输入屏幕上显示的 "Your new secret key"。 验证应用是否能生成与服务器上验证码同步的 6 位数字。
  1. 修改 PAM 配置以启用 Google Authenticator
      • *编辑 PAM SSH 配置文件:**Bash
        • sudo nano /etc/pam.d/sshd
      • 在文件顶部,找到以下行:
        • # Standard SSHD setup, see sshd(8) for details. @include common-auth
      • @include common-auth 这一行的上方添加以下行:
        • # For Google Authenticator auth required pam_google_authenticator.so
        • 注意 auth required 这表示 Google Authenticator 认证是强制性的。如果认证失败,用户将无法登录
    1. 修改 SSH Daemon 配置
        • *编辑 SSH Daemon 配置文件:**Bash
          • sudo nano /etc/ssh/sshd_config
        • 查找并修改以下参数:
          • KbdInteractiveAuthentication: 当前显示为 KbdInteractiveAuthentication no。为了让 Google Authenticator PAM 模块工作,需要将其设置为 yes
            • 找到这一行:
            • KbdInteractiveAuthentication no
            • 修改为:
            • KbdInteractiveAuthentication yes
          • UsePAM: 确保它设置为 yes。你提供的文件中已经设置好了。
            • UsePAM yes
            • (如果这行前面有 #,请移除 #
          • PasswordAuthentication: 根据你的需求选择:
            • 如果你希望仍然可以通过密码登录,然后输入 2FA 码:
              • PasswordAuthentication yes
              • (如果这行前面有 #,请移除 #
            • 如果你主要使用 SSH 密钥登录,并且希望在密钥认证后输入 2FA 码,且禁用密码登录(更安全):
              • PasswordAuthentication no
              • (如果这行前面有 #,请移除 #
        • 保存并退出文件。
      1. *重启 SSH 服务:**Bash
        1. sudo systemctl restart sshd
      2. *从新的终端窗口,尝试使用配置了 2FA 的用户登录你的服务器。**Bash
        1. ssh your_username@your_server_ip_address
      3. sudo tail -f /var/log/auth.log, 日志查看
相关推荐
小码哥_常4 分钟前
告别臃肿!Elasticsearch平替Manticore登场
后端
苍何1 小时前
万字保姆级教程:Hermes+Kimi K2.6 打造7x24h Agent军团
后端
我叫黑大帅1 小时前
为什么map查找时间复杂度是O(1)?
后端·算法·面试
FreeCultureBoy3 小时前
用 phpbrew 管理 php 环境:从安装到多版本切换
后端·php
FreeCultureBoy4 小时前
用 jenv 管理 Java 环境:从安装 JDK 到多版本切换
后端
IT_陈寒4 小时前
Vite的热更新突然失效,原来是因为这个配置
前端·人工智能·后端
考虑考虑4 小时前
SQL语句中的order by可能造成时间重复
数据库·后端·mysql
Pkmer4 小时前
古法编程: 代理模式
后端·设计模式
文心快码BaiduComate5 小时前
Comate搭载Kimi K2.6,长程13h!
前端·后端·程序员
Pkmer5 小时前
古法编程: 责任链模式
后端·设计模式