自建 GitLab 服务 SSH 连接出现 Permission denied 问题

自建 GitLab 服务 SSH 连接出现 Permission denied 问题

问题背景

换了单位,拉取 GitLab 代码时,总是提示要输入用户名和密码;虽然使用 IDEA 的图形界面很方便,但是我还是习惯于使用命令行;为了不用每次都输入用户名和密码,生成 SSH 密钥对和配置的过程这里省略;但是,进行 ssh -T 进行测试的时候出现了问题;

报错信息

$ ssh -T git@gitlab.xxxx.com
The authenticity of host 'gitlab.xxxx.com (82.xxx.xxx.xxx)' can't be established.
ED25519 key fingerprint is SHA256:i/CLVPwNoJw+lVB81Y/uuzA/aBiT+WzipKj4dTI9J38.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
git@gitlab.xxx.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

从表面上看,貌似是权限问题,网上也找了很多资料;但是,不知道那些是谁复制的谁的(Root 权限问题,改权限,修改服务端sshd_config配置等),最终也没有解决;

解决过程

根据 GitLab 官网指南,执行ssh -vT命令查询详情:

$ ssh -vT git@gitlab.xxxx.com
OpenSSH_9.5p1, OpenSSL 3.1.4 24 Oct 2023
debug1: Reading configuration data /c/Users/echoo/.ssh/config
debug1: /c/Users/echoo/.ssh/config line 16: Applying options for gitlab.xxxx.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to gitlab.igming.com [82.xxx.xxx.195] port 22.
debug1: Connection established.
debug1: identity file /c/Users/echoo/.ssh/id_ed25519 type 3
debug1: identity file /c/Users/echoo/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.5
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0
debug1: compat_banner: match: OpenSSH_8.0 pat OpenSSH* compat 0x04000000
debug1: Authenticating to gitlab.xxx.com:22 as 'git'
debug1: load_hostkeys: fopen /c/Users/echoo/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:i/CLVPwNoJw+lVB81Y/uuzA/aBiT+WzipKj4dTI9J38
debug1: load_hostkeys: fopen /c/Users/echoo/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'gitlab.igming.com' is known and matches the ED25519 host key.
debug1: Found key in /c/Users/echoo/.ssh/known_hosts:5
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /c/Users/echoo/.ssh/id_ed25519 ED25519 SHA256:HHLBBswMLDw3Z97WSOtxjcpbrwtf2maiHNji69HfEsk explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Offering public key: /c/Users/echoo/.ssh/id_ed25519 ED25519 SHA256:HHLBBswMLDw3Z97WSOtxjcpbrwtf2maiHNji69HfEsk explicit
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: No more authentication methods to try.
git@gitlab.xxx.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

从输出上看,也没有看出什么原因;
然后,参考了Github关于 Permission denied(publickey)的相关方案:Error: Permission denied (publickey) - GitHub Docs ,重新检查了自己的配置,好像也没有匹配的;

最终方案

这样前前后后折腾了半天,后来在GitLabWeb页面的Clone按钮上无意发现:
在这里插入图片描述
我们自建的 GitLab 服务的 ssh 连接并不是 22端口;而是8084端口,但是,碰巧服务器的22端口也能用;所以,试着修改~/.ssh/config文件:
在这里插入图片描述把配置中添加Port选项,设置为自己的8084端口;并且使用HostName选项,设置为服务器的 IP 地址;
然后,再进行测试:
在这里插入图片描述由于是第一次连接,会询问是否添加到已知的主机中,输入:yes;再次使用ssh -T git@gitlab.xxx.com进行测试:
在这里插入图片描述
可以看见,Welcome to GitLab @xxxx!的信息了;至此问题解决;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值