SSH 连接测试成功,但 git clone 却失败了?解密非标准端口的“坑”
本文记录了一次排查和解决 `git clone` 通过自定义 SSH 端口(10022)访问 Gogs 仓库时,即使配置了 SSH 密钥,依然反复提示输入密码的问题。
解决 git clone在自定义 SSH 端口下反复提示输入密码的问题
本文记录了一次排查和解决 git clone 通过自定义 SSH 端口(10022)访问 Gogs 仓库时,即使配置了 SSH 密钥,依然反复提示输入密码的问题。
1. 问题描述
尝试通过 SSH 方式克隆一个 Gogs 仓库,但 git 命令持续提示输入密码,而非使用已配置的 SSH 密钥。
初始克隆命令:
# 使用 SSH 方式克隆,指定了非标准端口 10022
git clone git@localhost:10022/shouzhi/test.git
2. 故障排查之旅
步骤 1:检查 SSH Agent 中的密钥
首先,确认 SSH Agent 是否已加载了正确的身份密钥。
# 1. 检查当前 agent 中的密钥列表
shouzhi@192 gogo % ssh-add -l
The agent has no identities.
发现 Agent 中没有密钥,于是手动添加。
# 2. 添加你的 SSH 密钥(请替换为你的密钥路径)
ssh-add ~/.ssh/id_ed25519
# 3. 再次确认密钥已加载
shouzhi@192 gogo % ssh-add -l
256 SHA256:s5W0atDQPBeQBlBmpSePcZSLZbn3Q4T90BoyjOqkFww shouzhi.duan2702@nvxclouds.com (ED25519)
密钥已成功加载到 SSH Agent。
步骤 2:直接测试 SSH 连接
直接使用 ssh 命令测试到 Gogs 服务的连通性,以排除网络或 Gogs 服务端的问题。
# 使用 -p 参数指定端口进行测试
shouzhi@192 gogo % ssh -T git@localhost -p 10022
The authenticity of host '[localhost]:10022 ([::1]:10022)' can't be established.
ED25519 key fingerprint is SHA256:a5NKIEvejvpPU7b5YjMF76UT5WkP5/qqkVDF098nbAw.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:10022' (ED25519) to the list of known hosts.
Hi there, You've successfully authenticated, but Gogs does not provide shell access.
If this is unexpected, please log in with password and setup Gogs under another user.
关键发现:SSH 连接测试成功。这表明身份验证本身是有效的,但 git 客户端在执行 clone 命令时可能没有正确使用这个连接。
步骤 3:再次尝试 git clone 依旧失败
尽管 SSH 连接测试成功,但 git clone 命令仍然提示输入密码。
shouzhi@192 gogo % git clone git@localhost:10022/shouzhi/test.git test-clone
Cloning into 'test-clone'...
(git@localhost) Password: # 仍然提示输入密码
这让我们将怀疑的焦点转向了 Git 和 SSH 客户端的配置交互上。
步骤 4:检查本地 SSH 配置文件
检查 ~/.ssh/config 文件,看看是否有影响连接的配置。
shouzhi@192 gogo % cat ~/.ssh/config
Host ypa-ys-50.44
HostName 192.168.50.44
User root
IdentityFile ~/.ssh/id_ed25519
Host ypa-ys-50.47
HostName 192.168.50.47
User root
IdentityFile ~/.ssh/id_ed25519
Host ypa-ys-50.48
HostName 192.168.50.48
User root
IdentityFile ~/.ssh/id_ed25519
3. 定位根本原因
问题根源终于明了:~/.ssh/config 文件中缺少一个针对 localhost:10022 的主机配置。
虽然 ssh 命令可以通过 -p 参数手动指定端口来成功连接,但 git 在解析 git@localhost:10022 这样的 URL 时,由于没有在 config 文件中找到匹配的 Host,它不知道应该使用哪个端口和哪个密钥文件,因此退回到了密码验证。
4. 最终解决方案
通过在 ~/.ssh/config 文件中为 Gogs 服务创建一个专用的 Host 别名,可以彻底解决此问题。
步骤 1:更新 ~/.ssh/config 文件
添加一个新的 Host 配置块。我们将其命名为 gogs。
# 将以下配置追加到 ~/.ssh/config 文件中
echo '
Host gogs
HostName localhost
Port 10022
User git
IdentityFile ~/.ssh/id_ed25519' >> ~/.ssh/config
这个配置告诉 SSH 客户端:当连接 gogs 这个主机时,实际应连接 localhost 的 10022 端口,并使用 git 用户和指定的密钥文件。
步骤 2:验证新的 SSH 别名
测试新配置的 gogs 别名是否能成功连接。
shouzhi@192 gogo % ssh -T gogs
Hi there, You've successfully authenticated, but Gogs does not provide shell access.
If this is unexpected, please log in with password and setup Gogs under another user.
连接成功!
步骤 3:使用新别名克隆仓库
现在,使用这个简洁的别名来克隆仓库。
shouzhi@192 gogo % git clone gogs:shouzhi/test.git test-clone
Cloning into 'test-clone'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (5/5), done.
克隆成功!问题解决。
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)