banner
yyh

Hi, I'm yyh

github
x
email

SSH密钥配置完整指南

SSH 密钥配置指南#

快速配置 SSH 密钥,用于服务器免密登录、Git 认证和提交签名


1. 生成 SSH 密钥对#

# 生成 Ed25519 密钥对(推荐)
ssh-keygen -t ed25519 -C "[email protected]"

# 交互提示:
# Enter file: 直接回车(使用默认路径 ~/.ssh/id_ed25519)
# Enter passphrase: 可选密码保护或直接回车跳过

生成结果

  • 私钥:~/.ssh/id_ed25519 (⚠️ 保密)
  • 公钥:~/.ssh/id_ed25519.pub (可公开)

2. 配置 SSH Config 文件#

# 创建/编辑配置文件
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config

配置模板

# 开发服务器
Host dev-server
    HostName 192.168.1.100
    User your_username
    Port 22
    IdentityFile ~/.ssh/id_ed25519

# 生产服务器
Host prod-server
    HostName prod.example.com
    User admin
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

# GitHub
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

配置项说明

  • Host: 别名(自定义)
  • HostName: 实际服务器地址
  • User: 登录用户名
  • Port: SSH 端口(默认 22)
  • IdentityFile: 私钥路径

使用效果

# 配置前
ssh [email protected]

# 配置后
ssh dev-server

3. 配置服务器免密登录#

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

# 使用完整地址
ssh-copy-id [email protected]

# 或使用别名
ssh-copy-id dev-server

方法 2:手动复制#

# 1. 复制公钥内容
cat ~/.ssh/id_ed25519.pub

# 2. 登录服务器
ssh [email protected]

# 3. 在服务器执行
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
exit

4. 配置 Git 提交签名#

# 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# 配置 SSH 签名
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true

配置文件效果 (~/.gitconfig):

[user]
    email = [email protected]
    name = Your Name
    signingkey = /Users/username/.ssh/id_ed25519.pub
[gpg]
    format = ssh
[commit]
    gpgsign = true

5. 添加公钥到 Git 平台#

GitHub#

# 复制公钥到剪贴板(macOS)
cat ~/.ssh/id_ed25519.pub | pbcopy
  1. 访问:https://github.com/settings/keys
  2. 点击 New SSH key
  3. 添加两次:
    • Key type: Authentication Key → Title: MacBook Pro → 粘贴公钥
    • Key type: Signing Key → Title: MacBook Pro Signing → 粘贴公钥

💡 同一公钥可同时用于认证和签名

GitLab#

  1. 复制公钥:cat ~/.ssh/id_ed25519.pub | pbcopy
  2. 访问:https://gitlab.com/-/profile/keys
  3. 粘贴公钥,Usage type 选择:Authentication & Signing

Gitee#

  1. 复制公钥:cat ~/.ssh/id_ed25519.pub | pbcopy
  2. 访问:https://gitee.com/profile/sshkeys
  3. 粘贴公钥,点击确定

6. 常见问题#

Q1: Permission denied (publickey)#

# 检查密钥权限
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

# 重新复制公钥
ssh-copy-id username@server

Q2: 提交没有 Verified 标记#

# 检查邮箱(必须与 GitHub 账号一致)
git config user.email

# 检查签名配置
git config --list | grep sign

# 确认已在 GitHub 添加 Signing Key

Q3: 签名时要求输入密码#

# 添加到 ssh-agent(macOS)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

# 或配置自动加载(编辑 ~/.ssh/config)
Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519

Q4: 多个密钥管理#

# ~/.ssh/config

# 个人 GitHub
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal

# 公司 GitHub
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

🎯 总体流程#

# 1. 生成密钥
ssh-keygen -t ed25519 -C "[email protected]"

# 2. 配置 Git 签名
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true

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

# 4. 添加到 GitHub
# Settings → SSH and GPG keys → New SSH key
# 添加两次:Authentication Key + Signing Key

# 5. 测试
ssh -T [email protected]

📋 常用命令#

# 密钥管理
ssh-keygen -t ed25519 -C "email"          # 生成密钥
cat ~/.ssh/id_ed25519.pub | pbcopy        # 复制公钥
ssh-keygen -p -f ~/.ssh/id_ed25519        # 修改密钥密码

# SSH 连接
ssh-copy-id user@host                     # 复制公钥到服务器
ssh -T [email protected]                     # 测试 GitHub 连接
ssh dev-server                            # 使用别名登录

# SSH Agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519  # 添加密钥(macOS)
ssh-add -l                                       # 查看已添加密钥

# Git 配置
git config --list | grep -E "(user|gpg|sign)"   # 查看签名配置
git log --show-signature                         # 查看提交签名
git commit --no-gpg-sign -m "msg"                # 临时禁用签名

# 权限修复
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

📚 附录#

SSH 密钥类型对比#

算法密钥长度推荐度
Ed25519256 位⭐⭐⭐⭐⭐ 首选(最安全、最快)
RSA4096 位⭐⭐⭐ 兼容性好
ECDSA256/384/521 位⭐⭐⭐ 部分老系统不支持
DSA1024 位❌ 已过时,不推荐

安全建议#

  • ✅ 为私钥设置密码保护
  • ✅ 私钥权限设为 600
  • ✅ 定期备份密钥到安全位置
  • ❌ 不要上传私钥到云存储
  • ❌ 不要通过聊天工具发送私钥

Blog 版本:1.0
适用平台:macOS / Linux / Windows (WSL)

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。