👮免密登录服务器/服务器登录加 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, 日志查看
相关推荐
神奇小汤圆22 分钟前
浅析二叉树、B树、B+树和MySQL索引底层原理
后端
文艺理科生31 分钟前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
前端·后端·架构
千寻girling32 分钟前
主管:”人家 Node 框架都用 Nest.js 了 , 你怎么还在用 Express ?“
前端·后端·面试
南极企鹅34 分钟前
springBoot项目有几个端口
java·spring boot·后端
Luke君6079736 分钟前
Spring Flux方法总结
后端
define952739 分钟前
高版本 MySQL 驱动的 DNS 陷阱
后端
忧郁的Mr.Li1 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端
暮色妖娆丶2 小时前
SpringBoot 启动流程源码分析 ~ 它其实不复杂
spring boot·后端·spring
Coder_Boy_2 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例中相关概念
java·人工智能·spring boot·后端·spring
Java后端的Ai之路2 小时前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway