Skip to content

使用 ssh-keygen 生成 SSH 密钥对

ssh-keygen 是专门为 SSH 协议设计的密钥生成工具,比 OpenSSL 更简单直接。以下是详细的使用方法:

基本用法

1. 生成 RSA 密钥对(最常用)

bash
# 生成 2048 位 RSA 密钥对
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -C "your_email@example.com"

# 或者生成 4096 位 RSA 密钥对(更安全)
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_gitea -C "your_email@example.com"

参数说明:

  • -t rsa: 指定密钥类型为 RSA
  • -b 4096: 指定密钥位数(2048/4096)
  • -f ~/.ssh/id_rsa_gitea: 指定密钥文件路径和名称
  • -C "comment": 添加注释,通常是邮箱或标识

2. 生成其他类型的密钥

bash
# 生成 ED25519 密钥(更安全、更快速)
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "your_email@example.com"

# 生成 ECDSA 密钥
ssh-keygen -t ecdsa -b 256 -f ~/.ssh/id_ecdsa -C "your_email@example.com"

完整生成流程

bash
# 1. 确保 ~/.ssh 目录存在
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# 2. 生成密钥对(以 Gitea 专用密钥为例)
ssh-keygen -t rsa -b 4096 -f ~/.ssh/gitea_rsa -C "gitea_deploy_key"

# 3. 设置正确的文件权限
chmod 600 ~/.ssh/gitea_rsa      # 私钥只有用户可读
chmod 644 ~/.ssh/gitea_rsa.pub  # 公钥可读

执行过程示例:

txt
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): [输入密码或直接回车]
Enter same passphrase again: [再次输入密码或直接回车]
Your identification has been saved in /home/user/.ssh/gitea_rsa
Your public key has been saved in /home/user/.ssh/gitea_rsa.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx gitea_deploy_key
The key's randomart image is:
+---[RSA 4096]----+
|          ...    |
|         . . .   |
|        . . . .  |
|       . . . . . |
|      . S . . . .|
|     . o * . . . |
|      . . B . . .|
|       . . B . . |
|        .=.=o. . |
+----[SHA256]-----+

密钥管理命令

查看密钥信息

bash
# 查看公钥指纹
ssh-keygen -lf ~/.ssh/gitea_rsa.pub

# 查看私钥指纹
ssh-keygen -lf ~/.ssh/gitea_rsa

# 显示随机艺术图
ssh-keygen -lvf ~/.ssh/gitea_rsa.pub

密钥格式转换

bash
# 将 OpenSSH 格式转换为 RFC4716 格式
ssh-keygen -e -f ~/.ssh/gitea_rsa.pub -m RFC4716 > gitea_rsa_rfc.pub

# 将 RFC4716 格式转换回 OpenSSH 格式
ssh-keygen -i -f gitea_rsa_rfc.pub -m RFC4716 > gitea_rsa_openssh.pub

更改密钥密码

bash
# 为现有密钥添加或更改密码
ssh-keygen -p -f ~/.ssh/gitea_rsa

安全最佳实践

1. 使用密码保护(推荐)

bash
# 生成带密码的密钥
ssh-keygen -t rsa -b 4096 -f ~/.ssh/gitea_rsa -C "gitea_deploy_key"

注意: 系统会提示输入密码,建议使用强密码。

2. 批量生成脚本示例

bash
#!/bin/bash

KEY_NAME="gitea_$(date +%Y%m%d)"
KEY_PATH="$HOME/.ssh/$KEY_NAME"
COMMENT="gitea_deploy_$(date +%Y%m%d)"

echo "生成 Gitea 部署密钥..."
ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -C "$COMMENT" -N "" -q

echo "密钥已生成:"
echo "私钥: $KEY_PATH"
echo "公钥: $KEY_PATH.pub"
echo "公钥内容:"
cat "$KEY_PATH.pub"

3. 验证密钥文件

bash
# 检查私钥是否有效
ssh-keygen -y -f ~/.ssh/gitea_rsa

# 检查公钥格式
ssh-keygen -l -f ~/.ssh/gitea_rsa.pub

生成的文件说明

  • 私钥文件: ~/.ssh/gitea_rsa

    • -----BEGIN OPENSSH PRIVATE KEY----- 开头
    • 必须严格保密,权限设置为 600
  • 公钥文件: ~/.ssh/gitea_rsa.pub

    • ssh-rsa AAAAB3Nza... 开头
    • 可以安全共享,用于添加到 Gitea 等服务

在 Gitea 中使用

  1. 复制公钥内容

    bash
    cat ~/.ssh/gitea_rsa.pub
  2. 在 Gitea 中添加

    • 登录 Gitea → 用户设置 → SSH 密钥
    • 或仓库设置 → 部署密钥
    • 粘贴公钥内容并保存
  3. 测试连接

    bash
    ssh -T git@your-gitea-server.com -i ~/.ssh/gitea_rsa

实用提示

  • 无密码密钥: 使用 -N "" 参数生成无密码密钥(适用于自动化脚本)
  • 自定义注释: -C 参数帮助识别密钥用途
  • 密钥轮换: 定期更新密钥增强安全性
  • 备份: 安全备份私钥文件

使用 ssh-keygen 生成的密钥对专门针对 SSH 协议优化,与各种 Git 服务(如 Gitea、GitHub、GitLab)完美兼容。