返回文章列表
6 分钟阅读

Ubuntu 生成 SSH Key 教程

记录在 Ubuntu 上生成 SSH Key、查看公钥、配置 GitHub/GitLab 和远程服务器免密登录的完整流程。

适用场景

SSH Key 常用于 Git 代码仓库认证和服务器免密登录。相比直接使用密码,SSH Key 更适合长期开发和服务器运维场景。

本文以 Ubuntu 为例,演示如何生成 SSH Key、查看公钥、配置 GitHub 或 GitLab,以及把公钥添加到远程服务器。

检查是否已有 SSH Key

生成新 Key 之前,先查看本机是否已经存在 SSH Key:

ls -al ~/.ssh

常见的密钥文件包括:

id_ed25519
id_ed25519.pub
id_rsa
id_rsa.pub

其中不带 .pub 的是私钥,带 .pub 的是公钥。

私钥不要发给任何人,也不要提交到 Git 仓库。公钥可以添加到 GitHub、GitLab 或服务器。

生成 SSH Key

推荐使用 ed25519 算法生成新的 SSH Key:

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

把邮箱替换成你自己的邮箱,例如:

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

执行后会看到类似提示:

Enter file in which to save the key (/home/ubuntu/.ssh/id_ed25519):

如果直接按回车,会使用默认路径:

/home/ubuntu/.ssh/id_ed25519

接着会提示输入 passphrase:

Enter passphrase (empty for no passphrase):

passphrase 是私钥密码。个人电脑建议设置,服务器自动化部署场景可以按需留空。

查看生成结果

生成完成后,再次查看 ~/.ssh 目录:

ls -al ~/.ssh

应该能看到:

id_ed25519
id_ed25519.pub

查看公钥内容:

cat ~/.ssh/id_ed25519.pub

输出内容通常以 ssh-ed25519 开头,末尾带有创建时填写的邮箱:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your_email@example.com

复制这一整行公钥内容,后面会用到。

添加到 GitHub

打开 GitHub 后进入:

Settings -> SSH and GPG keys -> New SSH key

填写:

Title: Ubuntu Laptop
Key: 粘贴 ~/.ssh/id_ed25519.pub 的内容

保存后,在 Ubuntu 终端测试连接:

ssh -T git@github.com

第一次连接时会提示是否信任主机,输入:

yes

如果看到类似 successfully authenticated 的提示,说明 SSH Key 配置成功。

添加到 GitLab

打开 GitLab 后进入:

Preferences -> SSH Keys

~/.ssh/id_ed25519.pub 的内容粘贴进去并保存。

测试连接:

ssh -T git@gitlab.com

如果是公司私有 GitLab,需要把 gitlab.com 替换成公司 GitLab 域名。

配置远程服务器免密登录

如果要用 SSH Key 登录远程服务器,需要把本机公钥追加到服务器的 ~/.ssh/authorized_keys

推荐使用 ssh-copy-id

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_ip

示例:

ssh-copy-id -i ~/.ssh/id_ed25519.pub ubuntu@192.168.1.100

然后测试登录:

ssh ubuntu@192.168.1.100

如果可以直接进入服务器,说明免密登录已经配置成功。

手动添加公钥到服务器

如果服务器没有 ssh-copy-id,也可以手动添加。

先在本机查看公钥:

cat ~/.ssh/id_ed25519.pub

复制输出的整行内容。

登录服务器后执行:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys

把公钥粘贴到 authorized_keys 中,保存后设置权限:

chmod 600 ~/.ssh/authorized_keys

退出服务器后重新测试登录:

ssh user@server_ip

配置多个 SSH Key

如果你同时使用 GitHub、GitLab 和公司代码仓库,可以为不同平台生成不同的 Key。

例如为 GitHub 单独生成:

ssh-keygen -t ed25519 -C "github@example.com" -f ~/.ssh/id_ed25519_github

为 GitLab 单独生成:

ssh-keygen -t ed25519 -C "gitlab@example.com" -f ~/.ssh/id_ed25519_gitlab

然后编辑 SSH 配置文件:

nano ~/.ssh/config

写入:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519_gitlab

设置配置文件权限:

chmod 600 ~/.ssh/config

分别测试:

ssh -T git@github.com
ssh -T git@gitlab.com

常见问题

Permission denied (publickey)

这个错误通常表示远程平台或服务器没有识别到你的公钥。

可以按顺序检查:

cat ~/.ssh/id_ed25519.pub
ssh -T git@github.com

确认公钥已经完整添加到 GitHub、GitLab 或服务器的 authorized_keys 中。

Bad permissions

如果 SSH 提示权限不安全,可以重置权限:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config

如果没有 ~/.ssh/config 文件,最后一条可以忽略。

每次都要求输入 passphrase

如果生成 Key 时设置了 passphrase,可以使用 ssh-agent 缓存:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

之后当前会话内再次使用 SSH Key 时,一般不需要重复输入 passphrase。

Git 远程地址不是 SSH

如果仓库远程地址是 HTTPS,SSH Key 不会生效。

查看远程地址:

git remote -v

HTTPS 地址通常长这样:

https://github.com/user/repo.git

可以改成 SSH 地址:

git remote set-url origin git@github.com:user/repo.git

小结

Ubuntu 生成 SSH Key 的核心流程是:使用 ssh-keygen 生成密钥,复制 .pub 公钥内容,添加到 GitHub、GitLab 或服务器,再通过 ssh -Tssh user@server_ip 验证连接。日常使用时需要保护好私钥文件,不要把私钥发送给他人或提交到代码仓库。