GitLab服务器SSH端口配置异常导致代码推送失败求助
GitLab SSH代码推送问题解决方案
问题描述
在Ubuntu 22.04的VPS上完成GitLab安装后,添加SSH密钥推送代码时遇到两种异常:
- 将VPS的SSH端口改为23后,执行
git push --set-upstream origin master报错:
ssh: connect to host git.censored.de port 22: Connection refused fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
- 将SSH端口改回22后,执行推送命令时需要输入git用户的密码。
当前GitLab配置如下:
external_url 'https://git.censored.de' letsencrypt['enable'] = false nginx['ssl_certificate'] = "/etc/letsencrypt/live/git.censored.de/fullchain.pem" nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/git.censored.de/privkey.pem" gitlab_rails['gitlab_shell_ssh_port'] = 22
解决步骤
一、SSH端口改为23时的修复方案
修改VPS SSH端口后,需同步更新GitLab的SSH端口配置,否则本地Git会默认尝试连接22端口:
- 编辑GitLab配置文件(通常路径为
/etc/gitlab/gitlab.rb),将gitlab_rails['gitlab_shell_ssh_port']的值改为23:
gitlab_rails['gitlab_shell_ssh_port'] = 23
- 重新配置并重启GitLab服务:
sudo gitlab-ctl reconfigure sudo gitlab-ctl restart
- 配置本地SSH连接规则,避免每次操作手动指定端口:
在本地~/.ssh/config文件中添加以下内容(文件不存在则直接创建):
Host git.censored.de Port 23 User git
保存后执行chmod 600 ~/.ssh/config确保权限合规。
二、SSH端口改回22时的密码输入问题修复
此问题说明SSH密钥未生效,需从权限和配置两方面排查:
- 检查本地SSH密钥权限:
确保私钥、公钥及.ssh目录权限符合安全要求:
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub
- 验证GitLab服务器上的授权密钥配置:
登录VPS,检查Git用户的authorized_keys文件:
# 确认文件中包含你的公钥 cat /var/opt/gitlab/.ssh/authorized_keys # 修复文件权限 sudo chown git:git /var/opt/gitlab/.ssh/authorized_keys sudo chmod 600 /var/opt/gitlab/.ssh/authorized_keys
- 测试SSH连接有效性:
执行以下命令测试是否能免密登录GitLab服务器:
ssh -T git@git.censored.de
若返回Welcome to GitLab, @你的用户名!则说明密钥配置正常。
4. 检查本地仓库的远程地址:
确保远程仓库地址为SSH格式而非HTTPS格式:
git remote -v
若显示HTTPS地址,执行以下命令修改为SSH格式:
git remote set-url origin git@git.censored.de:你的用户名/仓库名.git
内容的提问来源于stack exchange,提问作者Niklas Forst
相关产品推荐
相关产品推荐

