ssh-keygen

生成、管理和转换 SSH 认证密钥对,支持多种加密算法

语法

ssh-keygen [options]

参数

参数说明示例级别
-t 指定密钥类型(ed25519/rsa/ecdsa) ssh-keygen -t ed25519 常用
-C 添加注释(通常为邮箱) ssh-keygen -t ed25519 -C "dev@example.com" 常用
-f 指定密钥文件路径 ssh-keygen -t ed25519 -f ~/.ssh/github_key 常用
-b 指定密钥位数(RSA 建议 4096) ssh-keygen -t rsa -b 4096 常用
-N 指定密钥密码(passphrase) ssh-keygen -t ed25519 -N "mypassphrase" 进阶
-R 从 known_hosts 中移除指定主机 ssh-keygen -R 192.168.1.100 进阶

示例

生成 Ed25519 密钥(推荐)

ssh-keygen -t ed25519 -C "your@email.com"
Ed25519 更安全、密钥更短、性能更好

生成 RSA 4096 位密钥

ssh-keygen -t rsa -b 4096 -C "your@email.com"
兼容性最好,老系统可能不支持 ed25519

生成指定文件名的密钥

ssh-keygen -t ed25519 -f ~/.ssh/work_key -C "work@company.com"
多账号场景下为不同服务生成独立密钥

移除 known_hosts 中的旧主机记录

ssh-keygen -R 192.168.1.100
服务器重装后主机密钥变更时使用

常见错误

Overwrite (y/n)? 已存在同名密钥文件,输入 n 取消,用 -f 指定新文件名避免覆盖
Permissions 0644 for 'id_rsa' are too open 私钥权限过大,执行 chmod 600 ~/.ssh/id_rsa 修复

技巧

相关命令