Ubuntu 服务器配置 GitHub SSH 密钥与代理访问
本文记录如何在 Ubuntu 服务器上配置 GitHub SSH 密钥,用于:
- 克隆私有仓库
- 拉取代码
- 提交并推送代码
- 避免重复输入 GitHub 用户名和密码
- 在特殊网络环境下通过 443 端口或代理访问 GitHub SSH
当前服务器示例:
主机名:ai-server
用户:kevin
GitHub:antzw
仓库:git@github.com:antzw/tdia.git
一、检查现有 SSH 密钥
ls -al ~/.ssh
常见密钥文件:
id_ed25519
id_ed25519.pub
id_rsa
id_rsa.pub
为了避免和登录服务器的密钥混用,建议为 GitHub 单独创建:
~/.ssh/id_ed25519_github
二、创建 GitHub 专用 SSH 密钥
执行:
ssh-keygen -t ed25519 -C "你的 GitHub 邮箱" -f ~/.ssh/id_ed25519_github
例如:
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_ed25519_github
系统会询问:
Enter passphrase
长期运行的家庭服务器有两种选择。
设置密码
优点:
- 私钥泄露后仍有额外保护
缺点:
- 重启后需要重新添加到
ssh-agent - 自动化拉取代码更复杂
留空密码
优点:
- 适合无人值守任务
- Git 自动部署和定时任务方便
缺点:
- 私钥文件一旦泄露,可直接被使用
家庭内网服务器可以留空,但必须严格控制私钥文件权限。
三、设置密钥权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_github
chmod 644 ~/.ssh/id_ed25519_github.pub
检查:
ls -l ~/.ssh/id_ed25519_github*
四、把公钥添加到 GitHub
查看公钥:
cat ~/.ssh/id_ed25519_github.pub
输出类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... your-email@example.com
复制整行。
在 GitHub 中进入:
Settings
→ SSH and GPG keys
→ New SSH key
建议标题:
Ubuntu ai-server
Key type 选择:
Authentication Key
粘贴公钥并保存。
注意:只能上传 .pub 公钥,绝不能上传私钥。
五、配置 SSH 使用指定密钥
创建或编辑:
nano ~/.ssh/config
先使用标准 22 端口配置:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ServerAliveInterval 30
ServerAliveCountMax 3
设置权限:
chmod 600 ~/.ssh/config
解释:
Host github.com:匹配 GitHub SSH 地址User git:GitHub SSH 固定用户名IdentityFile:指定 GitHub 专用私钥IdentitiesOnly yes:只尝试这把密钥,避免密钥过多导致认证失败ServerAliveInterval:避免连接长时间空闲后被中断
六、测试 GitHub SSH 认证
执行:
ssh -T git@github.com
第一次连接可能询问:
Are you sure you want to continue connecting (yes/no/[fingerprint])?
输入:
yes
成功时应看到:
Hi antzw! You've successfully authenticated, but GitHub does not provide shell access.
GitHub 不提供普通 Shell,这是正常现象。
七、使用详细日志排查
如果连接失败:
ssh -vT git@github.com
更详细:
ssh -vvvT git@github.com
重点观察:
Connecting to github.com port 22
Offering public key
Server accepts key
Authenticated to github.com
如果连接在 Offering public key 之前关闭,通常是网络或端口问题。
如果出现:
Permission denied (publickey)
通常是密钥、GitHub 公钥或 SSH 配置问题。
八、克隆、拉取和推送仓库
克隆:
git clone git@github.com:antzw/tdia.git
进入仓库:
cd tdia
查看远程地址:
git remote -v
正常应为:
origin git@github.com:antzw/tdia.git (fetch)
origin git@github.com:antzw/tdia.git (push)
拉取:
git pull
推送:
git add .
git commit -m "update"
git push
九、配置 Git 提交身份
SSH 密钥负责认证,Git 用户名和邮箱负责提交记录中的作者身份,两者不是一回事。
配置:
git config --global user.name "antzw"
git config --global user.email "你的 GitHub 邮箱"
检查:
git config --global --list
如果不希望公开真实邮箱,可以使用 GitHub 提供的 noreply 邮箱。
十、将现有 HTTPS 仓库切换为 SSH
查看:
git remote -v
如果是:
https://github.com/antzw/tdia.git
改为 SSH:
git remote set-url origin git@github.com:antzw/tdia.git
再次确认:
git remote -v
十一、Git HTTP 代理不会作用于 SSH
下面的配置:
git config --global http.proxy http://192.168.2.2:7897
git config --global https.proxy http://192.168.2.2:7897
只影响:
https://github.com/...
不影响:
git@github.com:...
原因是 SSH 和 HTTP 是不同协议。
因此,即使 gpon 已开启,下面命令仍不会自动经过 Git HTTP 代理:
git clone git@github.com:antzw/tdia.git
SSH 要使用代理,必须在 ~/.ssh/config 中配置 ProxyCommand。
十二、尝试 GitHub SSH 443 端口
如果运营商或网络屏蔽 TCP 22,可以尝试 GitHub 官方提供的 SSH 443 入口。
编辑:
nano ~/.ssh/config
改为:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ServerAliveInterval 30
ServerAliveCountMax 3
测试:
ssh -vT git@github.com
日志应显示:
Connecting to ssh.github.com port 443
成功后仍会显示:
Hi antzw! You've successfully authenticated...
443 端口被关闭
如果出现:
Connection closed by xxx.xxx.xxx.xxx port 443
说明 TCP 连接已建立,但在完成 SSH 握手前被网络设备、代理软件或上游链路关闭。
这不代表仓库不存在,也通常还没有进入密钥认证阶段。
此时可以:
- 改回标准 22 端口测试
- 让 SSH 明确通过 Mac 的代理
- 暂时使用 HTTPS 仓库地址
十三、通过 Mac 的 HTTP 代理连接 GitHub SSH
当前代理:
Mac:192.168.2.2
HTTP/Mixed Port:7897
先安装代理连接工具:
sudo apt update
sudo apt install -y connect-proxy
测试 Mac 代理端口:
nc -vz 192.168.2.2 7897
如果使用 GitHub SSH 443:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ProxyCommand connect-proxy -H 192.168.2.2:7897 %h %p
ServerAliveInterval 30
ServerAliveCountMax 3
其中:
-H
表示使用 HTTP CONNECT 代理。
测试:
ssh -vvT git@github.com
如果 Mac 的 7897 是 mixed-port 或 HTTP 端口,该配置通常可用。
十四、通过 SOCKS5 代理连接
如果 Mac 提供 SOCKS5 端口,例如:
192.168.2.2:7898
可配置:
Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ProxyCommand connect-proxy -S 192.168.2.2:7898 %h %p
也可以让目标使用 GitHub 443:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ProxyCommand connect-proxy -S 192.168.2.2:7898 %h %p
使用哪个端口取决于实际网络环境。
十五、使用独立 Host 别名,避免频繁改配置
为了方便切换,可以同时保留三个配置。
Host github-direct
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
Host github-443
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
Host github-proxy
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ProxyCommand connect-proxy -H 192.168.2.2:7897 %h %p
分别测试:
ssh -T git@github-direct
ssh -T git@github-443
ssh -T git@github-proxy
克隆时:
git clone git@github-direct:antzw/tdia.git
或者:
git clone git@github-proxy:antzw/tdia.git
这种方案不需要反复编辑 ~/.ssh/config。
十六、为 GitHub 代理地址设置仓库远程
如果使用 github-proxy 别名:
git remote set-url origin git@github-proxy:antzw/tdia.git
查看:
git remote -v
输出:
origin git@github-proxy:antzw/tdia.git (fetch)
origin git@github-proxy:antzw/tdia.git (push)
SSH 会自动匹配:
Host github-proxy
然后通过 Mac 代理连接 GitHub。
十七、检查密钥是否真正被使用
查看最终生效配置:
ssh -G github.com | grep -E 'hostname|user|port|identityfile|proxycommand'
对于别名:
ssh -G github-proxy | grep -E 'hostname|user|port|identityfile|proxycommand'
检查 SSH Agent:
ssh-add -l
如果使用无密码私钥,通常不强制依赖 Agent,因为 IdentityFile 可以直接读取私钥。
如果密钥带密码:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_github
十八、确认仓库存在和账号权限
SSH 认证成功不等于拥有所有仓库权限。
测试账号认证:
ssh -T git@github.com
成功后,再检查仓库地址:
git@github.com:antzw/tdia.git
需要满足:
- 仓库确实存在
- 仓库名大小写正确
- 当前 GitHub 账号是仓库所有者或协作者
- 上传的 SSH Key 属于正确 GitHub 账号
对于私有仓库,如果密钥添加到了其他账号,仍会出现仓库读取失败。
十九、常见错误与判断
Connection refused
connect to host ... port ...: Connection refused
目标端口没有服务、被本机防火墙拒绝,或代理地址错误。
Connection timed out
Connection timed out
网络丢弃了连接请求,常见于 TCP 22 被屏蔽。
Connection closed by ... port 443
连接到目标后,SSH 握手被中途关闭。优先检查网络环境并尝试 SSH 代理。
Permission denied (publickey)
已连到 GitHub,但密钥认证失败。检查:
ssh -vvT git@github.com
以及:
cat ~/.ssh/id_ed25519_github.pub
确认公钥已添加到正确账号。
Repository not found
可能是:
- 仓库地址错误
- 私有仓库没有权限
- 认证到了错误的 GitHub 账号
Host key verification failed
可检查旧记录:
ssh-keygen -F github.com
ssh-keygen -F ssh.github.com
不要在未核对原因时随意删除所有 known_hosts。
二十、推荐的最终配置
对于 Ubuntu AI 服务器,推荐:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
ProxyCommand connect-proxy -H 192.168.2.2:7897 %h %p
ServerAliveInterval 30
ServerAliveCountMax 3
如果直连 22 可以稳定使用,则优先简化为:
Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
核心原则:
- SSH 密钥只负责身份认证
- Git
http.proxy不影响 SSH - TCP 22 或 SSH 443 不稳定时,需要单独配置
ProxyCommand - 为服务器创建 GitHub 专用密钥,不要和其他设备共用私钥
- 私钥永远不能上传到 GitHub、网盘或仓库