gitlab 使用 SSH 地址提交远程库失败
前言
www 和 blog 两个站点都指向了博客站,想着哪天有空,也要像 熊猫小A 的站点一样,给 www 搞一个好看的首页。问 chatgpt 推荐一个可以做出类似有格调站点的框架,Astro.js。安装 nvm 并创建好项目之后才发现,就是一个很接近于 Next.js 之类的框架,页面什么的还是需要自己写的(有模版,按照AI的指示,没用)。
让 chatgpt 出了一版,修修改改,结果做着做着,又变成了经典的 layout 布局:顶部导航,中间内容,还有一个底栏。这是个经典布局,看着也还行,但很遗憾想要的“格调”没有出来。

这样的项目当然要存到代码库里,方便后续可能的迭代之类的。结果 gitlab 创建好库之后,按照已存在的库进行提交的时候失败了。
使用 SSH 地址提交远程库失败
按照 git push 给的提示指令提交代码:
git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
git push --set-upstream origin main
Connection closed by 127.0.0.1 port 17897
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.经过排查,比如用户名是否一致,SSH Keys 是够添加,最终确认是开了 VPN 导致的。
scutil --proxy
<dictionary> {
ExceptionsList : <array> {
0 : 127.0.0.1
1 : 192.168.0.0/16
2 : 10.0.0.0/8
3 : 172.16.0.0/12
4 : localhost
5 : *.local
6 : *.crashlytics.com
7 : <local>
}
HTTPEnable : 1
HTTPPort : 17897
HTTPProxy : 127.0.0.1
HTTPSEnable : 1
HTTPSPort : 17897
HTTPSProxy : 127.0.0.1
ProxyAutoConfigEnable : 0
SOCKSEnable : 1
SOCKSPort : 17897
SOCKSProxy : 127.0.0.1
}关闭 VPN 之后再提交:
scutil --proxy
<dictionary> {
ExceptionsList : <array> {
0 : 127.0.0.1
1 : 192.168.0.0/16
2 : 10.0.0.0/8
3 : 172.16.0.0/12
4 : localhost
5 : *.local
6 : *.crashlytics.com
7 : <local>
}
HTTPEnable : 0
HTTPSEnable : 0
ProxyAutoConfigEnable : 0
SOCKSEnable : 0
}
git push --set-upstream origin main
ssh: connect to host gitlab.seasidecrab.com port 2222: Operation timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.依然报错,但现在变成了 2222 端口连接超时。查看远程库地址:
git remote -v
origin ssh://git@gitlab.seasidecrab.com:2222/lihai/web-index.git (fetch)
origin ssh://git@gitlab.seasidecrab.com:2222/lihai/web-index.git (push)对比其他的库的地址发现,多了一个端口 2222。查看 git 配置:
cat ~/.ssh/config
Host gitlab.seasidecrab.com
HostName gitlab.seasidecrab.com
User git
PubkeyAcceptedAlgorithms +ssh-rsa
HostKeyAlgorithms +ssh-rsa
# HostKeyAlgorithms ssh-ed25519,ecdsa-sha2-nistp384,rsa-sha2-512,rsa-sha2-256
KexAlgorithms diffie-hellman-group-exchange-sha256
IdentityFile ~/.ssh/id_rsa本地公钥:
ll ~/.ssh/id_*
-rw-------@ 1 jasonli staff 513B 8月 8 2025 /Users/jasonli/.ssh/id_ecdsa
-rw-r--r--@ 1 jasonli staff 182B 8月 8 2025 /Users/jasonli/.ssh/id_ecdsa.pub
-rw-------@ 1 jasonli staff 411B 8月 8 2025 /Users/jasonli/.ssh/id_ed25519
-rw-r--r--@ 1 jasonli staff 102B 8月 8 2025 /Users/jasonli/.ssh/id_ed25519.pub
-rw-------@ 1 jasonli staff 3.3K 8月 8 2025 /Users/jasonli/.ssh/id_rsa
-rw-r--r--@ 1 jasonli staff 746B 8月 8 2025 /Users/jasonli/.ssh/id_rsa.pub对比 SSH Keys 发现,id_rsa.pub 加过了,其他两种新类型的也加上,还是不行。
测试基本的域名、端口是否通:
nslookup gitlab.seasidecrab.com
nc -zv gitlab.seasidecrab.com 2222
nc: connectx to gitlab.seasidecrab.com port 2222 (tcp) failed: Operation timed out
ssh -vT git@gitlab.seasidecrab.com -p 2222
debug1: OpenSSH_10.2p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/jasonli/.ssh/config
debug1: /Users/jasonli/.ssh/config line 1: Applying options for gitlab.seasidecrab.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/100-macos.conf
debug1: /etc/ssh/ssh_config.d/100-macos.conf line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/crypto.conf
debug1: /etc/ssh/ssh_config line 55: Applying options for *
debug1: /etc/ssh/ssh_config line 62: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to gitlab.seasidecrab.com port 2222.
ssh: connect to host gitlab.seasidecrab.com port 2222: Operation timed out这进一步确认了连接端口 2222 问题。
gitlab SSH 端口 2222 连接失败解决
查看了一下上次部署的文档:gitlab-ee 13.6.7 迁移并使用 docker compose 部署,发现 2222 端口是 gitlab docker 容器化部署 SSH 协议映射到宿主机的端口,即 gitlab SSH 服务的对外端口。所以新的代码库 SSH 地址默认都会携带上 2222 端口。服务器默认的 22 端口已经绑定了 sshd 服务,如果想要 gitlab SSH 协议不额外添加端口,即映射端口 22:22,就需要先把 sshd 服务的端口给改掉,不然容器都启动不起来。
之前项目能够正常使用,说明有配置过。让 deepseek 出解决方法,比较简单就是加个端口配置:Port 2222。
git remote set-url origin ssh://git@gitlab.seasidecrab.com/lihai/web-index.git
cat ~/.ssh/config
Host gitlab.seasidecrab.com
HostName gitlab.seasidecrab.com
User git
Port 2222
PubkeyAcceptedAlgorithms +ssh-rsa
HostKeyAlgorithms +ssh-rsa
# HostKeyAlgorithms ssh-ed25519,ecdsa-sha2-nistp384,rsa-sha2-512,rsa-sha2-256
KexAlgorithms diffie-hellman-group-exchange-sha256
IdentityFile ~/.ssh/id_rsa还有阿里云服务器的安全组,添加对应的入方向端口访问规则。
不确定之前为什么去掉,还有 config 配置部分,也有点印象。
最后推送成功!
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
海滨擎蟹
微信
支付宝