ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Linux命令-ssh-keygen(生成 SSH 密钥对)

Linux命令-ssh-keygen(生成 SSH 密钥对) Linux命令-ssh-keygen生成 SSH 密钥对快速参考密钥类型对比生成密钥对常用选项详解密钥管理操作高级功能不同场景的密钥策略批量生成与自动化公钥文件格式ssh-keygen是 SSH 协议套件中的密钥生成工具用于创建、管理和转换 SSH 认证密钥。它支持多种密钥算法RSA、DSA、ECDSA、Ed25519可以生成公钥/私钥对是 SSH 安全认证体系的基础。快速参考ssh-keygen[选项]ssh-keygen生成的密钥对包括私钥默认为~/.ssh/id_type和公钥~/.ssh/id_type.pub。私钥永远不能分享公钥可以安全分发。密钥类型对比算法默认位数安全性性能兼容性推荐度Ed25519256极高最快OpenSSH 6.5首选ECDSA256/384/521高快较广泛推荐RSA3072高较慢最广泛可用DSA1024低慢废弃禁用生成密钥对# 推荐生成 Ed25519 密钥现代、快速、安全ssh-keygen-ted25519-Cuserexample.com# 生成 Ed25519 并指定文件名ssh-keygen-ted25519-f~/.ssh/id_ed25519_prod-Cproduction_key# 生成 RSA 密钥4096 位ssh-keygen-trsa-b4096-Crsa_legacy_key# 生成 ECDSA 密钥521 位曲线ssh-keygen-tecdsa-b521-Cecdsa_backup_key# 无交互生成用于脚本/CIssh-keygen-ted25519-f~/.ssh/deploy_key-N-Cci_deploy# 生成密钥并设置强密码ssh-keygen-ted25519-f~/.ssh/important_key-Cencrypted_key# 交互式输入密码推荐常用选项详解# -t: 指定密钥类型ssh-keygen-ted25519# 推荐ssh-keygen-tecdsa# 备用ssh-keygen-trsa# 传统兼容# -b: 密钥位数ssh-keygen-trsa-b4096ssh-keygen-tecdsa-b521# -C: 注释通常放邮箱/标识ssh-keygen-ted25519-Cadmincompany.com# -f: 输出文件名ssh-keygen-ted25519-f~/.ssh/github_key-Cgithub_personal# -N: 密码空串 无密码-N 为无密码ssh-keygen-ted25519-f~/.ssh/auto_key-N# -P: 修改密码时指定旧密码# -p: 修改密码交互式密钥管理操作# 修改已有密钥的密码ssh-keygen-p-f~/.ssh/id_ed25519# 修改指定密钥的密码非交互ssh-keygen-p-Pold_password-Nnew_password-f~/.ssh/id_ed25519# 移除密码ssh-keygen-p-Pcurrent_password-N-f~/.ssh/id_ed25519# 查看公钥指纹多种格式ssh-keygen-lf~/.ssh/id_ed25519.pub# 256 SHA256:xxxxx... userexample.com (ED25519)# 使用 MD5 格式指纹ssh-keygen-Emd5-lf~/.ssh/id_ed25519.pub# 从私钥重建公钥ssh-keygen-y-f~/.ssh/id_ed25519recovered.pub# 确认恢复的公钥与原始一致diffrecovered.pub ~/.ssh/id_ed25519.pubecho一致高级功能# 转换密钥格式PEM → OpenSSH 新格式ssh-keygen-p-mPEM-f~/.ssh/old_rsa_key# 从 PEM 转为 OpenSSH 格式ssh-keygen-p-mRFC4716-f~/.ssh/id_rsa# 生成主机密钥系统管理员操作sudossh-keygen-ted25519-f/etc/ssh/ssh_host_ed25519_key-N# 吊销已泄露的公钥创建 KRLssh-keygen-k-frevoked_keys.krl ~/.ssh/compromised_key.pub# 测试证书 KRLssh-keygen-Q-frevoked_keys.krl ~/.ssh/compromised_key.pub# 签署用户证书CA 操作ssh-keygen-sca_key-Iuser_id-nusername-V52w user_key.pub# 生成 SSHFP DNS 记录ssh-keygen-rhostname.example.com不同场景的密钥策略# 场景 1: GitHub 个人密钥ssh-keygen-ted25519-f~/.ssh/id_ed25519_github-Cgithubexample.com# 场景 2: 生产服务器部署密钥ssh-keygen-ted25519-f~/.ssh/prod_deploy-Cdeployproduction-N# ⚠️ 部署密钥无密码需严格保护私钥文件# 场景 3: 临时 CI 密钥TEMP_KEY/tmp/ci_temp_keyssh-keygen-ted25519-f$TEMP_KEY-N-Cci_temp# 使用后立即销毁shred-u$TEMP_KEY$TEMP_KEY.pub# 场景 4: 高安全需求密钥ssh-keygen-ted25519-a100-f~/.ssh/high_sec_key-Chigh_security# -a 100: KDF 轮数增加暴力破解难度批量生成与自动化# 为多台服务器生成统一密钥SERVERS(web1web2db1db2)forserverin${SERVERS[]};dossh-keygen-ted25519\-f~/.ssh/${server}_key\-N\-C${server}company.com\-q# 静默模式echo已生成:${server}_keydone# 密钥审计脚本audit_keys(){echo SSH 密钥审计 echo时间:$(date)echoforkeyin~/.ssh/id_*;doif[[$key!*.pub]];thenecho---$(basename$key)---ssh-keygen-lf$key2/dev/nullstat-c权限: %a 修改: %y$keyechofidone}audit_keys# 检查弱密钥# DSA 密钥不应再使用find~/.ssh/-nameid_dsa*-exececho⚠️ DSA 密钥已废弃: {}\;# RSA 2048 位的密钥不安全forkeyin~/.ssh/id_rsa;do[-f$key]||continueBITS$(ssh-keygen-lf$key|awk{print $1})if[$BITS-lt2048];thenecho⚠️ RSA 密钥位数不足:$key($BITSbit)fidone公钥文件格式# 公钥文件内容结构cat~/.ssh/id_ed25519.pub# ssh-ed25519 AAAAC3NzaC1lZ... commentexample.com# |算法类型| |------- Base64 编码公钥 --------| |---- 注释 ----|# 公钥的四种查看方式# 1. 直接读取文件cat~/.ssh/id_ed25519.pub# 2. 从私钥导出ssh-keygen-y-f~/.ssh/id_ed25519# 3. 查看指纹ssh-keygen-lf~/.ssh/id_ed25519.pub# 4. 从 ssh-agent 列出ssh-add-L安全准则Ed25519 是现代 Linux 环境的最佳选择绝不共享私钥私钥权限必须是 600为不同目的使用不同密钥对定期审查并吊销不再使用的公钥
返回列表