用 ssh-keygen 生成 SSH 密钥对(Ed25519)

在 ~/.ssh 中生成现代的 Ed25519 公私钥对,用于免密码登录服务器和使用 Git 托管平台。

Linux终端开发安全低风险
执行 ssh-keygen -t ed25519 -C "user@machine" 并按回车接受默认路径,即可生成 ~/.ssh/id_ed25519 和 id_ed25519.pub。

01命令

命令
$ ssh-keygen -t ed25519 -C "user@machine"

02作用说明

-C 只是给密钥加个备注(通常是 user@host),方便以后区分;工具会提示设置可选的口令(passphrase),建议设置——这样即使设备丢失,私钥也是加密的。部署到服务器上的是 .pub 公钥:ssh-copy-id user@server 会把它追加到服务器的 ~/.ssh/authorized_keys,也可以手动粘贴到 Git 平台的 SSH 设置里。ssh-keygen 只负责生成密钥,不会自动部署。如果目标服务器太老不支持 Ed25519,退回 ssh-keygen -t rsa -b 4096。用 ls ~/.ssh/*.pub 可列出已有密钥。

03风险

风险低风险
使用条件OpenSSH client; the key pair is written to ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519.pub
!注意: 绝不要外传或提交私钥(id_ed25519)——只有 .pub 公钥才放到服务器和 Git 平台;私钥一旦泄露必须视为已失窃并立即更换。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • ssh-keygen(1) — OpenSSH authentication key utility
    man7.org · technical · 2026-09-17
    依据: The ssh-keygen man page documents -t to select the key type (including ed25519) and -C to provide a new comment.
  • OpenSSH - ArchWiki
    Arch Linux Wiki · community · 2026-09-17
    依据: The ArchWiki OpenSSH page recommends ssh-keygen -t ed25519 for generating Ed25519 keys and describes copying the public key to servers with ssh-copy-id.

最后验证: 2026-09-17 · 分类: 安全与隐私

06相关方案