🧰 一、命令结构详解

ssh-keygen [-t 类型] [-b 位数] [-C 注释] [-f 文件路径] [其他参数]
参数 说明
-t 指定密钥类型:常见有 rsadsaecdsaed25519
-b 指定密钥长度:RSA 推荐 2048 或 4096
-C 添加注释,通常是邮箱或用户名
-f 指定密钥文件保存路径
-N 设置密钥密码(空字符串表示无密码)
-q 静默模式,不显示提示信息

🧪 二、常见使用场景

1. 生成 RSA 密钥对

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • 默认保存为 ~/.ssh/id_rsa~/.ssh/id_rsa.pub
  • 可用于 Git、远程服务器登录等

2. 生成 ED25519 密钥(更安全更快)

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

3. 指定保存路径和密码

ssh-keygen -t rsa -b 2048 -C "user@host" -f ~/.ssh/my_key -N "your_password"

🔐 三、部署公钥到远程服务器

方法一:手动复制

ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_host

方法二:手动追加

cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

🧭 四、其他实用命令

查看密钥指纹

ssh-keygen -l -f ~/.ssh/id_rsa.pub

修改密钥密码

ssh-keygen -p -f ~/.ssh/id_rsa

从私钥提取公钥

ssh-keygen -y -f ~/.ssh/id_rsa > id_rsa.pub

转换公钥格式(OpenSSH → RFC4716)

ssh-keygen -e -f ~/.ssh/id_rsa.pub > id_rsa_rfc.pub

🧠 五、密钥类型对比

类型 安全性 性能 推荐用途
RSA 中等 通用
DSA 较低 已过时,不推荐
ECDSA 移动设备
ED25519 极高 极快 推荐使用

📁 六、密钥文件权限建议

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
Copyright © https://yan-jian.com 2023 all right reserved更新时间: 2025-10-20 17:21:53

results matching ""

    No results matching ""