SSH密钥登录配置

SSH 密钥 + 密码双重认证配置指南

适用系统:Ubuntu 22.04 | OpenSSH 8.9+
目标:登录时密钥和密码缺一不可


一、原理说明

模式 authenticationmethods 说明
任意一种 any(默认) 有密钥 OR 密码即可登录
双重认证 publickey,password 密钥 AND 密码都必须通过

二、客户端:生成 SSH 密钥对

本地机器执行:

# 生成 ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"

# 或生成 RSA 4096 密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

交互提示:

Enter file in which to save the key: ~/.ssh/id_ed25519   # 默认路径,直接回车
Enter passphrase (empty for no passphrase):              # 建议设置密钥密语
Enter same passphrase again:

生成结果:

~/.ssh/id_ed25519      # 私钥(严禁外传)
~/.ssh/id_ed25519.pub  # 公钥(上传到服务器)

三、将公钥上传到服务器

方法 A:使用 ssh-copy-id(推荐)

ssh-copy-id -i ~/.ssh/id_ed25519.pub airsky@<服务器IP> -p 20001

方法 B:手动追加

cat ~/.ssh/id_ed25519.pub | ssh airsky@<服务器IP> -p 20001 \
  "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

验证公钥已写入

# 在服务器上执行
cat ~/.ssh/authorized_keys

四、服务器端:修改 sshd 配置

sudo nano /etc/ssh/sshd_config

在文件末尾添加或修改以下内容:

# 开启公钥认证
PubkeyAuthentication yes

# 保留密码认证
PasswordAuthentication yes

# Ubuntu 22.04 OpenSSH 8.9+ 需要同时开启这两项
KbdInteractiveAuthentication yes
ChallengeResponseAuthentication yes

# 核心配置:密钥和密码都必须验证通过
AuthenticationMethods publickey,password

⚠️ 注意:逗号表示"先验证密钥,再验证密码",两者缺一不可。


五、检查 sshd_config.d 目录是否有冲突

# 查看包含的额外配置文件
sudo ls /etc/ssh/sshd_config.d/

# 搜索是否有覆盖 AuthenticationMethods 的配置
sudo grep -r "AuthenticationMethods\|PasswordAuthentication\|PubkeyAuthentication" \
  /etc/ssh/sshd_config.d/

六、验证配置并重启服务

# 第一步:检查语法,没有输出表示正常
sudo sshd -t

# 第二步:确认关键配置生效
sudo sshd -T | grep -E "pubkeyauthentication|passwordauthentication|authenticationmethods|kbdinteractive"

# 期望输出:
# pubkeyauthentication yes
# passwordauthentication yes
# kbdinteractiveauthentication yes
# authenticationmethods publickey,password

# 第三步:重启 SSH 服务
sudo systemctl restart sshd

七、客户端测试登录

⚠️ 务必新开一个终端窗口测试,不要关闭当前连接!

ssh -i ~/.ssh/id_ed25519 airsky@<服务器IP> -p 20001

正常登录流程:

1. SSH 验证密钥        → 通过 ✅
2. 提示输入系统密码     → 输入 airsky 的登录密码
3. 密码正确            → 登录成功 ✅

若只有密钥没有密码,或只有密码没有密钥,均会被拒绝。


八、文件权限检查

权限不正确时 SSH 会静默忽略 authorized_keys,是最常见的故障原因:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519      # 本地私钥

# 验证
ls -la ~/.ssh/

正确权限应为:

drwx------  .ssh/                  (700)
-rw-------  .ssh/authorized_keys   (600)
-rw-------  .ssh/id_ed25519        (600)
-rw-r--r--  .ssh/id_ed25519.pub    (644)

九、故障排查

现象 原因 解决方案
直接用密码就能登录 authenticationmethods any 确认末尾 AuthenticationMethods publickey,password 已生效
密钥验证失败 权限问题或公钥未写入 检查 authorized_keys 权限和内容
密码验证失败 PasswordAuthentication no 改为 yes
配置修改不生效 sshd_config.d/ 有覆盖 检查 /etc/ssh/sshd_config.d/*.conf
连接被拒绝 sshd 未重启 sudo systemctl restart sshd

查看实时日志:

sudo journalctl -u ssh -f
# 或
sudo tail -f /var/log/auth.log

十、当前服务器实际生效配置(参考)

根据 sudo sshd -T 输出,修改前关键值:

port                     22 / 20001 / 20002
pubkeyauthentication     yes
passwordauthentication   yes
kbdinteractiveauthentication  no        ← 需要改为 yes
challengeresponseauthentication  no     ← 需要改为 yes
authenticationmethods    any            ← 需要改为 publickey,password
permitrootlogin          yes            ← 建议改为 prohibit-password

十一、安全加固建议(可选)

# 禁止 root 直接登录(推荐)
PermitRootLogin prohibit-password

# 限制登录用户
AllowUsers airsky

# 修改默认端口(已配置多端口)
# Port 20001

# 登录失败锁定(需安装 fail2ban)
# apt install fail2ban

posted @ 2026-05-29 09:53  陪你去流浪  阅读(55)  评论(0)    收藏  举报