我使用 GIT 作为 Ubuntu Linux 上自动构建过程的一部分。因为它是一个自动化的构建过程,所以最好将所有配置都放在版本控制中,这样我就不必在配置更改时登录每个构建代理。
为了使其正常工作,我有一个私钥(具有只读存储库访问权限)和一个签入版本控制的known_hosts 文件。
我将 HOME 环境变量设置为版本控制中的某个位置 (build/ssh_home),以便允许 GIT 找到我的known_hosts 文件 (build/ssh_home/.ssh/known_hosts)。
我使用以下命令“激活”私钥并从 GIT 中提取私钥。“git clone”步骤类似。
ssh-agent bash -c "echo $HOME; ssh-add '${deploymentKey}' >/dev/null 2>&1; git pull ${repository} --quiet"
Run Code Online (Sandbox Code Playgroud)
这适用于 Windows。然而,在 Ubuntu Linux 上,这还不够。
“HOME”环境变量设置如下(在 bash 脚本中)
export HOME=`pwd`/build/ssh_home
Run Code Online (Sandbox Code Playgroud)
但是,它显然没有使用正确的known_hosts 文件,它仍在尝试使用 ~/.ssh/known_hosts 中的文件,下面消息中引用的路径证明了这一点。
The authenticity of host 'bitbucket.org (131.103.20.168)' can't be established.
RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/myusername/.ssh/known_hosts).
Run Code Online (Sandbox Code Playgroud)
如何让 GIT (SSH) 使用特定位置(而不是我的主目录)中的known_hosts 文件?
编辑:
我的“build/ssh_home”目录的权限(在评论中请求)
ls -la build/ssh_home
total 16
drwx------ 3 ben ben 4096 Jun 18 10:52 .
drwxrwxr-x 39 ben ben 4096 Jun 18 10:52 ..
-rw------- 1 ben ben 209 Jun 18 10:52 readme.txt
drwx------ 2 ben ben 4096 Jun 18 10:52 .ssh
Run Code Online (Sandbox Code Playgroud)
build/ssh_home/.ssh 的权限
ls -la build/ssh_home/.ssh
total 12
drwx------ 2 ben ben 4096 Jun 18 10:52 .
drwx------ 3 ben ben 4096 Jun 18 10:52 ..
-rw------- 1 ben ben 2402 Jun 18 10:52 known_hosts
Run Code Online (Sandbox Code Playgroud)
我的“~/.ssh”目录的权限(在评论中请求)
myusername@myhostname:~$ ls -la ~/.ssh
total 24
drwxr-xr-x 2 root root 4096 Jun 29 10:28 .
drwxr-xr-x 16 ben ben 4096 May 21 15:39 ..
-rw-r--r-- 1 root root 427 May 19 11:27 authorized_keys
-rw------- 1 root root 1679 May 19 11:27 bitbucket
-rw-r--r-- 1 root root 398 May 19 11:27 bitbucket.pub
-rw-r--r-- 1 root root 63 May 19 11:27 config
Run Code Online (Sandbox Code Playgroud)
root
拥有该目录,而不是您。你必须改变这一点:
sudo chown -R ben:ben ~/.ssh
Run Code Online (Sandbox Code Playgroud)
然后您将需要更改该目录中的权限。整个目录应该是0600。
chmod 0600 ~/.ssh
Run Code Online (Sandbox Code Playgroud)
我积极反对将known_hosts 文件添加到源代码管理中,因为这确实不应该“公开” - 并且有一些方法可以避免不自动接受系统新密钥的问题。