坐看云起时
← 返回首页

SSH 密钥登录配置详解

2026-10-26 · 技术 · SSH

相比密码登录,SSH 密钥登录更安全、更方便。本文讲解完整配置流程。

生成密钥对

ssh-keygen -t ed25519 -C "your_email@example.com"

推荐使用 ed25519 算法,安全性高且密钥短。生成后会在 ~/.ssh/ 下得到:

上传公钥

ssh-copy-id root@your-server-ip

或者手动复制:

cat ~/.ssh/id_ed25519.pub | ssh root@your-server \
  "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

关闭密码登录(可选,更安全)

# 编辑 /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes

# 重启 sshd
systemctl restart sshd

务必先确认密钥能登录成功,再关闭密码登录,否则可能把自己锁在门外。

日常使用

ssh root@your-server-ip   # 自动用密钥免密登录