我已经部署了一个本地GitLab实例(使用https://docs.gitlab.com/ee/install/docker.html中描述的步骤),创建了一个用户,生成了一个密钥对,并按照https://docs.gitlab.com/ee/ssh/中的步骤上传了公钥。
私钥位于目录~/。SSH与600权限,和文件~/。Ssh/config命令如下:
Host gitlab.mycompany.com
Hostname gitlab.mycompany.com
User myusername
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
TCPKeepAlive yes
但是,当尝试建立SSH连接时,我得到错误'Permission denied (publickey)':
$ ssh -vT git@gitlab.mycompany.com
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f 31 Mar 2020
debug1: Reading configuration data /home/myusername/.ssh/config
debug1: /home/myusername/.ssh/config line 1: Applying options for gitlab.mycompany.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to gitlab.mycompany.com [12.345.678.90] port 22.
debug1: Connection established.
debug1: identity file /home/myusername/.ssh/id_rsa type 0
debug1: identity file /home/myusername/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.2
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to gitlab.mycompany.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
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: Server host key: ecdsa-sha2-nistp256 SHA256:9FbhbOAyNvky0M+CtyEOT8tBBimwF8aOpDH0zr+6+2Y
debug1: Host 'gitlab.mycompany.com' is known and matches the ECDSA host key.
debug1: Found key in /home/myusername/.ssh/known_hosts:29
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: /home/myusername/.ssh/id_rsa RSA SHA256:uekBVw1MVUz2V4LgS//w1mAP9wBRr1oomLK5uYtfJDE explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/myusername/.ssh/id_rsa RSA SHA256:uekBVw1MVUz2V4LgS//w1mAP9wBRr1oomLK5uYtfJDE explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
git@gitlab.mycompany.com: Permission denied (publickey).
我认为问题是在服务器端,因为我能够使用相同的密钥对建立到gitlab.com的成功连接。
对于如何解决这个问题有什么建议吗?
更多细节
我在最初的问题描述中没有提到的是,GitLab是作为容器部署的,所以我认为可能发生的事情是主机可以通过端口22访问,但它可能不会重定向到GitLab容器。
检查Gitlab主机上的/var/log/auth.log
。它往往有更多关于身份验证错误的细节。查找sshd
条目。
我设法解决了这个问题:)
原来,我在创建容器时错过了——publish 22:22指令。
sudo docker run --detach
--hostname gitlab.mycompany.com
--publish 443:443 --publish 80:80 --publish 22:22
--name gitlab
--restart always
--volume $GITLAB_HOME/config:/etc/gitlab:Z
--volume $GITLAB_HOME/logs:/var/log/gitlab:Z
--volume $GITLAB_HOME/data:/var/opt/gitlab:Z
gitlab/gitlab-ee:latest
分辨率为:
- 停止容器
$ sudo docker stop gitlab
- 关闭docker
$ sudo systemctl stop docker
- 编辑文件/var/lib/docker/containers/[容器id]/hostconfig. conf。从 更改PortBindings
"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"443"}],"80/tcp":[{"HostIp":"","HostPort":"80"}]}
"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"443"}],"80/tcp":[{"HostIp":"","HostPort":"80"}],"2222/tcp":[{"HostIp":"","HostPort":"22"}]}
(我选择使用端口2222代替)
- 启动docker
$ sudo systemctl start docker
- 启动容器
$ sudo docker start gitlab
以上内容允许我成功连接:
$ ssh -T git@gitlab.mycompany.com -p 2222
Enter passphrase for key '/home/<username>/.ssh/id_rsa':
Welcome to GitLab, @<username>!