在 Ubuntu 上生成 SSH 密钥非常简单,通常使用的是 OpenSSH 工具。以下是详细步骤:
先看看是否已经有密钥了:
ls ~/.ssh
如果看到 id_rsa / id_rsa.pub 或 id_ed25519 / id_ed25519.pub,说明已经生成过。
ssh-keygen -t ed25519 -C "your_email@example.com"
-C是注释,一般填邮箱,方便识别。
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
保存路径(默认即可):
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
直接回车
设置密码(可选):
Enter passphrase (empty for no passphrase):
生成后,公钥在:
cat ~/.ssh/id_ed25519.pub
复制输出的内容,用于添加到服务器或 Git 平台。
ssh-copy-id user@server_ip
把 id_ed25519.pub 内容粘贴到服务器的:
~/.ssh/authorized_keys
ssh user@server_ip
sudo systemctl status ssh
sudo systemctl start ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
如果你是想用于 GitHub / GitLab / 服务器登录 / WSL,可以告诉我具体场景,我可以给你更针对性的步骤。