Gitlab post-receive hook 主机密钥验证失败

Gitlab post-receive hook 主机密钥验证失败

我有 3 台服务器:

werkstation.example.com -> 克隆 gitlab 存储库并用于更改存储库文件的服务器

git.example.com -> 带有存储库“tomcat”的 gitab 服务器

docker.example.com -> 使用 git hook 复制文件的服务器

我的 git 钩子:

#!/bin/sh
read oldrev newrev refname

REPO="[email protected]:michaelv1234/tomcat.git"
BRANCH=`echo $refname | sed -n 's/^refs\/heads\///p'`
BRANCH_DIR="/home/michael"
SSH_DEST="[email protected]"

if [ "$newrev" -eq 0 ] 2> /dev/null; then
# branch is being deleted
echo "Deleting remote branch $SSH_DEST $BRANCH_DIR/$BRANCH"
echo "$SSH_DEST" /bin/sh
ssh  "$SSH_DEST" /bin/sh <<-EOF
            cd "$BRANCH_DIR" && rm -rf $BRANCH
    EOF
else
    # branch is being updated
    echo "Updating remote branch $SSH_DEST $BRANCH_DIR/$BRANCH"
    ssh "$SSH_DEST" /bin/sh <<-EOF
            { cd "$BRANCH_DIR/$BRANCH" || mkdir -p "$BRANCH_DIR/$BRANCH" && cd "$BRANCH_DIR/$BRANCH"; } \
            && { git fetch origin && git reset --hard origin/$BRANCH || { git clone "$REPO" ./ && git checkout $BRANCH; }; }
    EOF
fi

但我仍然收到这个错误:

michael@werkstation:~/tomcat$ git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Updating remote branch [email protected] /home/michael/master
remote: Host key verification failed.
To [email protected]:michaelv1234/tomcat.git
0032c02..6e8ef97  master -> master
Branch master set up to track remote branch master from origin.

权限文件和目录:

custom_hooks directory:
drwxr-xr-x 2 git git 4096 May 27 12:05 custom_hooks

post-receive file in custom_hooks:
rwxr-xr-x 1 git git 1435 May 27 12:05 post-receive

我已经删除了每台服务器上的“known_hosts”文件,但仍然收到错误

答案1

初始化 SSH 连接时,服务器会向客户端验证自身身份。然后,客户端会检查密钥是否已在 known_hosts 文件中。如果没有,它会提示用户。这就是为什么在第一次 SSH 连接时,您会收到一个提示,询问您是否要接受带有指纹 xyz 的密钥。然后,服务器公钥将写入 known_hosts 文件。

如果您从未通过 SSH 连接到服务器,则它不在 known_hosts 文件中,自动连接将失败。解决此问题的最简单方法是自己以应该调整 git 命令的用户身份通过​​ SSH 连接到服务器,并在提示符下回答“是”。

答案2

解决方案:在Gitlab服务器上:

sudo su - git
ssh-keygen
ssh-copy-id [email protected]
ssh [email protected]

相关内容