如何让GIT / SSH查找" known_hosts"在特定的位置?

时间:2015-06-29 02:05:14

标签: git ubuntu ssh

我在Ubuntu Linux上使用GIT作为自动构建过程的一部分。因为它是一个自动构建过程,所以最好在版本控制中使用所有配置,这样我就不必在配置发生变化时登录每个构建代理。

为了使其工作,我有一个私钥(具有只读repo访问权限)和一个已签入版本控制的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"

这适用于Windows。但是,在Ubuntu Linux上,这还不够。

" HOME"环境变量设置如下(在bash脚本中)

export HOME=`pwd`/build/ssh_home

但是,它显然没有使用正确的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).

如何让GIT(SSH)在特定位置(不在我的主目录中)使用known_hosts文件?

修改

我和#34; 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

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

我和#34;〜/ .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

2 个答案:

答案 0 :(得分:1)

sudo chown -R ben:ben ~/.ssh 拥有该目录,而非您。你必须改变它:

chmod 0600 ~/.ssh

然后,您将要更改该目录中的权限。整个目录应该是0600。

UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)

我主动不鼓励将known_hosts文件添加到源代码控制中,因为这不是应该“公开”的东西 - 而there are ways可以避免不自动接受系统新密钥的问题。< / p>

答案 1 :(得分:1)

您可以通过以下方式指定known_hosts文件的位置:

ssh -o UserKnownHostsFile=/path/to/known_hosts

例如,如果要测试到GitHub的SSH连接,可以执行以下操作:

ssh -o UserKnownHostsFile=/path/to/known_hosts -T git@github.com

这会将github.com(IP地址)添加到known_hosts文件中,然后您将看到如下消息:

Hi <github_username>! You've successfully authenticated, but GitHub does not provide shell access.

然后您可以执行cat /path/to/known_hosts来检查内容。

我的样子:

github.com,192.30.255.112 ssh-rsa ...
...
...==
192.30.255.113 ssh-rsa ...
...
...==
相关问题