👮免密登录服务器/服务器登录加 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, 日志查看
相关推荐
源代码•宸38 分钟前
大厂技术岗面试之谈薪资
经验分享·后端·面试·职场和发展·golang·大厂·职级水平的薪资
晚霞的不甘1 小时前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
喵叔哟1 小时前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
Charlie_lll2 小时前
力扣解题-移动零
后端·算法·leetcode
打工的小王3 小时前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
80530单词突击赢4 小时前
JavaWeb进阶:SpringBoot核心与Bean管理
java·spring boot·后端
爬山算法5 小时前
Hibernate(87)如何在安全测试中使用Hibernate?
java·后端·hibernate
WeiXiao_Hyy5 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
苏渡苇5 小时前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·设计模式·学习方法·责任链模式
long3165 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法