👮免密登录服务器/服务器登录加 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, 日志查看
相关推荐
一线大码1 小时前
Gradle 基础篇之基础知识的介绍和使用
后端·gradle
Java猿_1 小时前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
小王师傅662 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
码事漫谈2 小时前
C++高并发编程核心技能解析
后端
码事漫谈2 小时前
C++与浏览器交织-从Chrome插件到WebAssembly,开启性能之门
后端
源代码•宸3 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang
⑩-3 小时前
SpringCloud-Nacos 配置中心实战
后端·spring·spring cloud
java1234_小锋4 小时前
[免费]SpringBoot+Vue勤工助学管理系统【论文+源码+SQL脚本】
spring boot·后端·mybatis·勤工助学
踏浪无痕5 小时前
从 Guava ListenableFuture 学习生产级并发调用实践
后端·面试·架构