450 字
2 分钟
SSH 代理与多 GitHub 账号配置速查
这篇用来复制 SSH 代理和多账号配置。
基础 GitHub SSH 配置
编辑:
nano ~/.ssh/config普通 GitHub SSH:
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yesGitHub SSH 走 443 端口
有些网络环境下,22 端口不稳定,可以改用 GitHub 提供的 SSH over HTTPS 端口:
Host github.com HostName ssh.github.com User git Port 443 IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yes测试:
ssh -T git@github.comSSH 通过 SOCKS5 代理(本地代理环境推荐)
Host github.com HostName ssh.github.com User git Port 443 IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yes ProxyCommand nc -X 5 -x 127.0.0.1:10808 %h %p这里适合本地代理监听在 127.0.0.1:10808 的情况。
多 GitHub 账号 Host 别名
默认账号:
Host github.com HostName ssh.github.com User git Port 443 IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yes ProxyCommand nc -X 5 -x 127.0.0.1:10808 %h %p博客账号:
Host github-r-gc HostName ssh.github.com User git Port 443 IdentityFile ~/.ssh/id_ed25519_r_gc IdentitiesOnly yes ProxyCommand nc -X 5 -x 127.0.0.1:10808 %h %p使用别名测试:
ssh -T git@github-r-gc仓库 remote 使用别名:
git remote set-url origin git@github-r-gc:r-gc/r-gc.github.io.git这样默认 github.com 可以继续给主力账号使用,github-r-gc 专门给博客账号使用。
查看 SSH 实际使用的配置
ssh -G github.com | lessssh -G github-r-gc | less调试连接过程:
ssh -vT git@github.comssh -vT git@github-r-gc如果信息不够,再增加 v:
ssh -vvvT git@github.com常用参数解释
Host:本地别名。Git remote 里写的主机名会匹配它。
HostName:真实连接的服务器地址。
User git:GitHub SSH 固定使用 git 用户。
Port 443:使用 443 端口连接 GitHub SSH,适合 22 端口不通或不稳定时。
IdentityFile:指定私钥路径。
IdentitiesOnly yes:只使用 IdentityFile 指定的 key,避免 SSH 尝试一堆其它 key。
ProxyCommand:连接目标服务器前,先通过这个命令建立网络通道。
nc:netcat,用来建立代理连接。
-X 5:告诉 nc 使用 SOCKS5 代理。
-x 127.0.0.1:10808:指定代理地址和端口。
%h:SSH 会替换成目标主机,也就是 HostName。
%p:SSH 会替换成目标端口,也就是 Port。
SSH 代理与多 GitHub 账号配置速查
https://r-gc.github.io/posts/notes/git-ssh/ssh-proxy-and-config/