将 ssh 选项传递给 git clone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7772190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing ssh options to git clone
提问by Daniel Velkov
I'm trying to run git clone
without ssh checking the repository host's key. I can do it from ssh like that:
我试图在git clone
没有 ssh 检查存储库主机密钥的情况下运行。我可以像这样从 ssh 做到这一点:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@host
Is there any way to pass the same ssh options to the git clone command?
有没有办法将相同的 ssh 选项传递给 git clone 命令?
Edit: There is a restriction that I can't modify ~/.ssh/config
or any other files on that machine.
编辑:有一个限制,我不能修改~/.ssh/config
该机器上的任何其他文件。
采纳答案by Josh Lee
Add them to your ~/.ssh/config
:
将它们添加到您的~/.ssh/config
:
Host host
HostName host
User user
SshOption1 Value1
SshOption2 Value2
The Host
entry is what you'll specify on the command line, and the HostName
is the true hostname. They can be the same, or the Host
entry can be an alias. The User
entry is used if you do not specify user@
on the command line.
该Host
条目是您将在命令行中指定的内容,而HostName
是真正的主机名。它们可以相同,或者Host
条目可以是别名。User
如果未user@
在命令行中指定,则使用该条目。
If you must configure this on the command line, set the GIT_SSH
environment variable to point to a script with your options in it.
如果您必须在命令行上进行配置,请将GIT_SSH
环境变量设置为指向包含您的选项的脚本。
回答by Boris
The recently released git 2.3 supports a new variable "GIT_SSH_COMMAND" which can be used to define a command WITH parameters.
最近发布的 git 2.3 支持一个新变量“GIT_SSH_COMMAND”,可用于定义带参数的命令。
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone user@host
$GIT_SSH_COMMAND
takes precedence over$GIT_SSH
, and is interpreted by the shell, which allows additional arguments to be included.
$GIT_SSH_COMMAND
优先于$GIT_SSH
,并由 shell 解释,允许包含其他参数。
回答by VonC
Another option made to specify different keys is git config core.sshCommand
with git 2.10 + (Q3 2016).
指定不同键的另一个选项是git config core.sshCommand
使用 git 2.10 +(2016 年第三季度)。
This is an alternative to the environment variable described in Boris's answer)
See commit 3c8ede3 (26 Jun 2016) by Nguy?n Thái Ng?c Duy (pclouds
).
(Merged by Junio C Hamano -- gitster
--in commit dc21164, 19 Jul 2016)
参见Nguy?n Thái Ng?c Duy ( ) 的commit 3c8ede3(26 Jun 2016 )。(由Junio C Hamano合并-- --在dc21164 提交中,2016 年 7 月 19 日)pclouds
gitster
A new configuration variable
core.sshCommand
has been added to specify what value for GIT_SSH_COMMAND to use per repository.Similar to
$GIT_ASKPASS
or$GIT_PROXY_COMMAND
, we also read from config file first then fall back to$GIT_SSH_COMMAND
.This is useful for selecting different private keys targetting the same host (e.g. github)
添加了一个新的配置变量
core.sshCommand
来指定每个存储库使用的 GIT_SSH_COMMAND 值。与
$GIT_ASKPASS
or类似$GIT_PROXY_COMMAND
,我们也首先从配置文件中读取,然后回退到$GIT_SSH_COMMAND
.这对于选择针对同一主机(例如 github)的不同私钥很有用
core.sshCommand:
If this variable is set,
git fetch
andgit push
will use the specified command instead ofssh
when they need to connect to a remote system.
The command is in the same form as theGIT_SSH_COMMAND
environment variable and is overridden when the environment variable is set.
如果这个变量被设置,
git fetch
并且git push
将使用指定的命令而不是ssh
当它们需要连接到远程系统时。
该命令与GIT_SSH_COMMAND
环境变量的形式相同,并在设置环境变量时被覆盖。
It means the git clone
can be:
这意味着git clone
可以是:
cd /path/to/my/repo
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
# later on
git clone host:repo.git
If you want to apply that for allrepos, as user1300959adds in the comments, you would use a global configuration.
如果您想将其应用于所有存储库,正如user1300959在评论中添加的那样,您将使用全局配置。
git config --global core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
回答by kenorb
Here is tricky example how to pass the ssh arguments by using GIT_SSH variable:
这是如何使用 GIT_SSH 变量传递 ssh 参数的棘手示例:
$ echo 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
$ chmod +x ssh
$ GIT_TRACE=1 GIT_SSH="$PWD/ssh" git clone user@host
Note: Above lines are terminal command-lines which you should paste into your terminal. It'll create a file ssh, make it executable and executes it.
注意:以上几行是终端命令行,您应该将其粘贴到终端中。它将创建一个文件ssh,使其可执行并执行它。
If you'd like to pass the private key option, please check How to tell git which private key to use?.
如果您想传递私钥选项,请检查如何告诉 git 使用哪个私钥?.
回答by Pavan Kumar
Repository level configuration without impacting the system level settings
不影响系统级设置的存储库级配置
Consolidating the already available answers, I am choosing the below steps. This ensures that the configuration changes do not impact at the machine level, but just for the repository being worked on. This is needed in my case as my script needs to be executed on a shared Bamboo agent.
整合已有的答案,我选择以下步骤。这确保配置更改不会影响机器级别,而只会影响正在处理的存储库。这在我的情况下是必需的,因为我的脚本需要在共享的 Bamboo 代理上执行。
1.Clone the repository taking the GIT_SSH_COMMAND
approach.
1.采用该GIT_SSH_COMMAND
方法克隆存储库。
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone ssh://url
2.Once cloned, navigate into repository directory.
2.一旦克隆,导航到存储库目录。
cd repo-dir
3.Set core.sshCommand
configuration so that all future calls can be just run with git commands like usual, but internally consuming the provided git options.
3.设置core.sshCommand
配置,以便所有未来的调用都可以像往常一样使用 git 命令运行,但在内部使用提供的 git 选项。
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
回答by Jonathan Beber
I think that update git to an version >= 2.3 and use GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone user@host
is the bet option, but if it not possible, @josh-lee gave a good option, but please, update your answer indenting the ssh config file.
我认为将 git 更新到版本 >= 2.3 并使用GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone user@host
是赌注的选择,但如果不可能,@josh-lee 提供了一个不错的选择,但请更新您的答案缩进 ssh 配置文件。
Host host
HostName host
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
回答by Ajay Kumar
This issue has been fixed by doing follow step's in Window machine:-
通过在 Window 机器中执行以下步骤已修复此问题:-
Create config file under C:\Users\username.ssh folder.
add the following line to a config file.
host <HOST> hostname <HOSTNAME> user <USER_NAME> IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes port <PORT_NUMBER> KexAlgorithms +diffie-hellman-group1-sha1
then try again.
在 C:\Users\username.ssh 文件夹下创建配置文件。
将以下行添加到配置文件中。
host <HOST> hostname <HOSTNAME> user <USER_NAME> IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes port <PORT_NUMBER> KexAlgorithms +diffie-hellman-group1-sha1
然后再试一次。