SSH密钥生成指南
生成 SSH 密钥
# Ed25519 — 推荐(现代、小巧、快速)
ssh-keygen -t ed25519 -C "your_email@example.com"
# RSA 4096 — 兼容旧系统
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 指定输出文件
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work key"
添加密钥到 ssh-agent
# 启动 ssh-agent
eval "$(ssh-agent -s)"
# 添加密钥
ssh-add ~/.ssh/id_ed25519
# macOS — 添加到钥匙串
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# 列出已加载密钥
ssh-add -l
复制公钥到服务器
# 推荐方式
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# 手动方式
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
~/.ssh/config
Host work
HostName work.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes
Host github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
密钥类型对比
| 类型 | 安全性 | 速度 | 兼容性 |
|---|---|---|---|
| Ed25519 | 极佳 | 快 | OpenSSH 6.5+ |
| RSA 4096 | 强 | 中等 | 通用 |
| ECDSA | 良好 | 快 | 广泛 |
| RSA 2048 | 可接受 | 中等 | 通用 |