免密登录服务器
- 本地创建密钥
ssh-keygen -t rsa -b 4096
- 公钥复制到服务器,默认22端口,如果有改动,就 -p 端口
ssh-copy-id username@hostname
-
服务端SSH配置 路径
/etc/ssh/sshd_config修改 -
bash
PubkeyAuthentication yes PasswordAuthentication no AuthorizedKeysFile .ssh/authorized_keys ClientAliveInterval 60 ClientAliveCountMax 3- Shell
- PasswordAuthentication 会允许有密钥客户端免密登录,后两处修改增加超时配置 注意文件权限配置
- ~/.ssh 目录设置为700, authorized_key设置为600
bashchmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys- Shell
- 重启SSH服务
sudo systemctl restart sshd
服务器登录加 Auth(使用 Google Auth)
-
在 Ubuntu 服务器上安装所需的软件包
-
sql
sudo apt update sudo apt install libpam-google-authenticator- Shell
-
切换到目标用户,运行 auth
-
bash
# 切换到目标用户 sudo su - your_username # 运行 google-authenticator 命令 google-authenticator- 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 位数字。
- "Do you want authentication tokens to be time-based (y/n)" : 输入
- 修改 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 认证是强制性的。如果认证失败,用户将无法登录
- 在
- 修改 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- (如果这行前面有
#,请移除#)
-
- 保存并退出文件。
- *重启 SSH 服务:**Bash
-
sudo systemctl restart sshd
- *从新的终端窗口,尝试使用配置了 2FA 的用户登录你的服务器。**Bash
-
ssh your_username@your_server_ip_address
- sudo tail -f /var/log/auth.log, 日志查看
-
-