SSH 密钥完全指南:生成、配置、多账号管理与安全加固

开发工具

SSH 密钥:比密码更安全的认证方式

对比 密码 SSH 密钥
认证方式 知识因子(你知道的) 持有因子(你拥有的)
暴力破解 可能 几乎不可能
钓鱼风险 高 低(私钥不离开本机)
服务器泄露 密码明文/哈希可撞库 仅存公钥,无法反推私钥
便捷性 每次输入 一次配置,自动认证

一、生成密钥对

Ed25519(推荐)

ssh-keygen -t ed25519 -C "your@email.com"
参数 说明
-t ed25519 算法类型(Ed25519,最安全最快)
-C 注释(通常用邮箱标识)

RSA(兼容性备选)

ssh-keygen -t rsa -b 4096 -C "your@email.com"
算法 密钥长度 安全性 性能 推荐
Ed25519 256 bit 最高 最快 ✅
ECDSA P256 256 bit 高 快 可选
RSA 4096 4096 bit 高 慢 兼容性备选
RSA 2048 2048 bit 中 慢 ❌ 不推荐

生成过程

$ ssh-keygen -t ed25519 -C "dev@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): ****
Enter same passphrase again: ****
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub

强烈建议设置 passphrase:即使私钥泄露,有 passphrase 也能防止直接使用。


二、部署公钥

GitHub / GitLab

# 复制公钥
cat ~/.ssh/id_ed25519.pub | clip

# GitHub → Settings → SSH and GPG keys → New SSH key
# 粘贴公钥内容

服务器

# 方法一:ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.com

# 方法二:手动追加
cat ~/.ssh/id_ed25519.pub | ssh user@server.com 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

验证连接

ssh -T git@github.com
# Hi username! You've successfully authenticated...

ssh user@server.com
# 直接登录,无需密码

三、多账号配置

场景:工作 GitHub + 个人 GitHub

# 生成两对密钥
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_work
ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_personal

SSH Config

# ~/.ssh/config
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_work
  IdentitiesOnly yes

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_personal
  IdentitiesOnly yes

Git 使用

# 工作仓库
git clone git@github-work:company/project.git

# 个人仓库
git clone git@github-personal:username/project.git

多平台配置

# ~/.ssh/config
Host github.com
  User git
  IdentityFile ~/.ssh/id_github

Host gitlab.com
  User git
  IdentityFile ~/.ssh/id_gitlab

Host gitee.com
  User git
  IdentityFile ~/.ssh/id_gitee

Host dev.azure.com
  User git
  IdentityFile ~/.ssh/id_azure

四、SSH Agent 管理

启动 Agent

eval "$(ssh-agent -s)"

添加密钥

ssh-add ~/.ssh/id_ed25519
# Enter passphrase: ****

# 查看已添加的密钥
ssh-add -l
# 256 SHA256:xxx... dev@example.com (ED25519)

自动启动(bashrc)

# ~/.bashrc 或 ~/.zshrc
if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)"
  ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi

keychain 持久化

# macOS 使用 Keychain 存储 passphrase
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

# Linux 使用 keychain
eval "$(keychain --eval id_ed25519)"

五、跳板机配置

场景:本地 → 跳板机 → 生产服务器

# ~/.ssh/config
Host jumpbox
  HostName jump.example.com
  User jump-user
  IdentityFile ~/.ssh/id_jump

Host production
  HostName 10.0.1.100
  User deploy
  IdentityFile ~/.ssh/id_prod
  ProxyJump jumpbox
# 直接连接生产服务器(自动经过跳板机)
ssh production

Agent 转发

Host jumpbox
  HostName jump.example.com
  ForwardAgent yes

安全提示:仅在信任跳板机时启用 Agent 转发。


六、安全加固

服务器端 sshd_config

# /etc/ssh/sshd_config
PermitRootLogin no                    # 禁止 root 登录
PasswordAuthentication no             # 禁止密码登录
PubkeyAuthentication yes             # 仅允许密钥登录
AuthorizedKeysFile .ssh/authorized_keys
MaxAuthTries 3                       # 最大尝试次数
LoginGraceTime 30                    # 登录超时 30 秒
AllowUsers deploy admin              # 白名单用户

密钥权限

# 私钥必须 600
chmod 600 ~/.ssh/id_ed25519

# .ssh 目录必须 700
chmod 700 ~/.ssh

# authorized_keys 必须 600
chmod 600 ~/.ssh/authorized_keys

密钥轮换

# 1. 生成新密钥
ssh-keygen -t ed25519 -C "dev@example.com" -f ~/.ssh/id_ed25519_new

# 2. 部署新公钥到所有服务器/Git 平台
ssh-copy-id -i ~/.ssh/id_ed25519_new.pub user@server

# 3. 验证新密钥可用
ssh -i ~/.ssh/id_ed25519_new user@server

# 4. 移除旧公钥
# 编辑服务器 ~/.ssh/authorized_keys,删除旧公钥行

# 5. 删除旧密钥
rm ~/.ssh/id_ed25519_old

七、常见问题排查

问题 原因 解决
Permission denied 公钥未部署 ssh-copy-id
Too many authentication failures Agent 中密钥过多 IdentitiesOnly yes
Are you sure you want to continue connecting 首次连接 StrictHostKeyChecking=accept-new
连接慢 DNS 反查 UseDNS no(服务端)
密钥不生效 权限错误 chmod 600 私钥

调试模式

ssh -vvv user@server.com
# 详细输出连接过程,定位认证失败原因
#SSH#密钥#Git#免密登录#安全