你是否厌倦了每次向 GitHub 推送代码时重复输入密码?HTTPS 协议的身份验证机制在便捷性上的缺陷,已经成为开发者高效协作的隐形障碍。本文将带你通过 SSH 密钥对(推荐 ED25519 算法)实现 GitHub 的无密码安全推送,让每一次 git push 都如德芙般顺滑

演示环境:

  • Ubuntu 24.04.1 LTS x86_64

生成密钥对

首先切换到 SSH 配置文件所在目录

cd ~/.ssh

如果没有这个目录,那么就需要你创建一个

mkdir ~/.ssh

再创建一下 SSH 配置文件

touch config

接下来在终端中输入以下命令

ssh-keygen -t ed25519 -C "your_email@example.com"
  • -t ed25519:指定密钥类型为 ED25519
  • -C "your_email@example.com":这是一个注释,通常用于标识密钥的用途或所有者。这里需要你替换为 GitHub 电子邮件地址

如果你的系统不支持ED25519算法,请使用以下命令

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

按下回车后,它会提示你设置密钥对保存路径

Enter file in which to save the key (/root/.ssh/id_ed25519): github_ed25519
  • 系统会提示你选择保存密钥对的位置。默认情况下,密钥对会保存在 ~/.ssh/id_ed25519(私钥)和 ~/.ssh/id_ed25519.pub(公钥)。你可以按 Enter 键接受默认位置,或者输入自定义路径(这里我将会设置为 github_ed25519

接下来,系统会提示你为私钥设置一个密码。这个密码用于加密私钥文件,增加安全性。你可以选择设置密码,或者直接按 Enter 键跳过

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

密钥对生成完成后,你会看到类似以下的输出:

Your identification has been saved in github_ed25519
Your public key has been saved in github_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

生成密钥之后,建议修改密钥文件权限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/github_ed25519  # 私钥文件权限
chmod 644 ~/.ssh/github_ed25519.pub  # 公钥文件权限(非必需但建议)

配置 Github

接下来需要打开你的 Github 主页(或者直接点击这里,如果可以看到 Add new SSH Key的话那么你可以直接跳过步骤1-3)

  1. 点击你的头像(通常在右上角)

  2. 找到 “Settings”

  3. 在左侧 “Access” 栏中,找到 “SSH and GPG keys”

  4. 点击 “New SSH key”

    接下来你会看到 “Title” 以及 “Key” 字段,这里分别是填写密钥备注以及公钥的地方

  5. 选择密钥类型

    找到(“Key type” 选择框),这里选择 “Authentication Key” 即可

  6. 粘贴公钥

    还记得刚才生成的公钥吗?现在请你找到刚才生成密钥的目录,执行以下命令来查看公钥

    cat ~/.ssh/github_ed25519.pub
    ssh-ed25519 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx your_email@example.com
    

    接下来你将会看到以 ssh-ed25519 开头的公钥,请复制所有输出的内容到 “Key” 字段

    完成后点击 “Add SSH key”

配置 SSH

接下来切换到 SSH 配置文件所在目录

cd ~/.ssh

编辑 config 这个文件

vim config

向文件中插入以下内容

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_ed25519
    IdentitiesOnly yes
  • Host:定义主机别名,这里我将设置为 github.com(其实可以不用和实际域名相同,理论上设置为 github或者 mygithub也是可以的)

  • HostName:指定实际连接的主机名或 IP 地址(即使 Host 定义了一个别名,例如 mygithub,SSH 仍然会连接到 github.com

  • User:指定 SSH 连接时使用的用户名,这里GitHub 要求使用 git 作为用户名进行 SSH 连接。因此,这里设置为 git

  • IdentityFile:指定用于身份验证的私钥文件路径

  • IdentitiesOnly:限制 SSH 客户端仅使用配置文件中指定的私钥文件

    默认情况下,SSH 客户端会尝试使用所有可用的私钥文件(例如 ~/.ssh/id_rsa~/.ssh/id_ed25519 等)进行身份验证。设置 IdentitiesOnly yes 后,SSH 客户端只会使用 IdentityFile 指定的私钥文件(这里是 ~/.ssh/ed25519_github),而不会尝试其他私钥

测试链接

如果以上配置没有问题,那么你就可以尝试连接 Github 了

ssh -T git@github.com

首次连接可能会提示

The authenticity of host 'github.com (IP)' can't be established.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

这里输入 yes 来确认继续连接

不出意外,你将会看到输出

Hi Your_username! You've successfully authenticated, but GitHub does not provide shell access.

配置 Git

配置 SSH 后,需将仓库 URL 从 HTTPS 切换为 SSH,首先检查当前远程仓库的 URL

在终端中进入你的本地 Git 项目目录,执行以下命令查看当前远程仓库的地址

git remote -v

如果输出类似以下内容,表示当前使用的是 HTTPS 协议

origin  https://github.com/username/repo.git (fetch)
origin  https://github.com/username/repo.git (push)

接下来,修改远程仓库 URL 为 SSH 格式。执行以下命令,将远程仓库 URL 切换为 SSH 协议

git remote set-url origin git@github.com:username/repo.git
  • origin:默认远程仓库名称(如果使用其他名称如 upstream,需对应修改)
  • git@github.com:username/repo.git:SSH 格式的仓库地址(需替换为你的实际地址)

接下来可以再次执行 git remote -v,确认 URL 已切换为 SSH 格式 (这里需要你替换为你的实际仓库地址)

origin  git@github.com:username/repo.git (fetch)
origin  git@github.com:username/repo.git (push)

尝试推送代码到远程仓库,验证是否不再需要输入账号密码:

git push origin main

如果配置正确,推送会直接成功,无需输入 GitHub 账号密码。

参考:

No Wiš'adel !