使用 GIT_SSH 错误使用自定义 SSH 进行 Git 克隆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14220929/
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
Git clone with custom SSH using GIT_SSH error
提问by Paris
I am trying to clone a Git repo using a custom SSH command. I set the SSH command in the GIT_SSH environmental variably be running
我正在尝试使用自定义 SSH 命令克隆 Git 存储库。我在 GIT_SSH 环境中设置 SSH 命令可变地运行
export GIT_SSH="/usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key"
.
export GIT_SSH="/usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key"
.
But when, after the previous command I run
但是当我运行上一个命令之后
git clone [email protected]:uname/test-git-repo.git
, I get the following weird error
git clone [email protected]:uname/test-git-repo.git
,我收到以下奇怪的错误
error: cannot run /usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key
fatal: unable to fork
Can you please help me out solve this issue?
你能帮我解决这个问题吗?
回答by larsks
You cannot provide options in the GIT_SSH
environment variable; from the git
man page:
您不能在GIT_SSH
环境变量中提供选项;从git
手册页:
GIT_SSH
If this environment variable is set then git fetch and git push will use this command instead of ssh when they need to connect
to a remote system. The $GIT_SSH command will be given exactly two arguments: the username@host (or just host) from the URL
and the shell command to execute on that remote system.
To pass options to the program that you want to list in GIT_SSH you will need to wrap the program and options into a shell
script, then set GIT_SSH to refer to the shell script.
One option is to add a stanza to your .ssh/config
file with the appropriate configuration:
一种选择是.ssh/config
使用适当的配置向您的文件添加一个节:
Host bitbucket.org
StrictHostKeyChecking no
IdentityFile /home/me/my_private_key
Another option is to point GIT_SSH
to a shell script that does what you want. E.g., in /home/me/bin/bitbucket_ssh
, put:
另一种选择是指向GIT_SSH
执行您想要的操作的 shell 脚本。例如,在 中/home/me/bin/bitbucket_ssh
,输入:
#!/bin/sh
exec /usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key "$@"
And then point GIT_SSH
at /home/me/bin/bitbucket_ssh
.
然后指向GIT_SSH
的/home/me/bin/bitbucket_ssh
。
I prefer using .ssh/config
when possible, because this avoids the need to create a per-destination script for each remote.
我更喜欢.ssh/config
在可能的情况下使用,因为这避免了为每个远程创建每个目标脚本的需要。
回答by VonC
Note that starting with git 2.3+ (Q1 2015), what you initially tried would work, with the new environment variable GIT_SSH_COMMAND
.
请注意,从 git 2.3+(2015 年第 1 季度)开始,您最初尝试使用新的环境变量GIT_SSH_COMMAND
.
See commit 3994276from Thomas Quinot (quinot
):
git_connect
: set ssh shell command in GIT_SSH_COMMAND
git_connect
: 设置 ssh shell 命令 GIT_SSH_COMMAND
It may be impractical to install a wrapper script for
GIT_SSH
when additional parameters need to be passed.
Provide an alternative way of specifying a shell command to be run, including command line arguments, by means of theGIT_SSH_COMMAND
environment variable, which behaves likeGIT_SSH
but is passed to the shell.The special circuitry to modify parameters in the case of using PuTTY's plink/tortoiseplink is activated only when using
GIT_SSH
; in the case of usingGIT_SSH_COMMAND
, it is deliberately left up to the user to make any required parameters adaptation before calling the underlying ssh implementation.
GIT_SSH
在需要传递附加参数时安装包装器脚本可能是不切实际的。
提供另一种指定要运行的 shell 命令的方法,包括命令行参数,通过GIT_SSH_COMMAND
环境变量,它的行为类似于GIT_SSH
但传递给 shell。在使用 PuTTY 的 plink/tortoiseplink 的情况下修改参数的特殊电路仅在使用时激活
GIT_SSH
;在 using 的情况下,GIT_SSH_COMMAND
在调用底层 ssh 实现之前,故意让用户进行任何所需的参数调整。
GIT_SSH_COMMAND
:
GIT_SSH_COMMAND
:
If either of these environment variables is set then '
git fetch
' and 'git push
' will use the specified command instead of 'ssh
' when they need to connect to a remote system.
The command will be given exactly two or four arguments:
- the '
username@host
' (or just 'host
') from the URL and the shell command to execute on that remote system, optionally preceded by '-p
' (literally) and- the '
port
' from the URL when it specifies something other than the defaultSSH
port.
$GIT_SSH_COMMAND
takes precedence over$GIT_SSH
, and is interpreted by the shell, which allows additional arguments to be included.$GIT_SSH
on the other hand must be just the path to a program (which can be a wrapper shell script, if additional arguments are needed).
如果设置了这些环境变量中的任何一个,则“
git fetch
”和git push
“ssh
”在需要连接到远程系统时将使用指定的命令而不是“ ”。
该命令将给出两个或四个参数:
- 来自 URL的“
username@host
”(或只是“host
”)和要在该远程系统上执行的 shell 命令,可选地以“-p
”(字面意思)和port
来自 URL的“ ”,当它指定除默认SSH
端口之外的其他内容时。
$GIT_SSH_COMMAND
优先于$GIT_SSH
,并由 shell 解释,允许包含其他参数。$GIT_SSH
另一方面,必须只是程序的路径(如果需要额外的参数,它可以是一个包装壳脚本)。
回答by TlmaK0
Use ssh-agent
使用 ssh-agent
ssh-agent bash -c 'ssh-add /home/me/my_private_key; git clone [email protected]:uname/test-git-repo.git'
回答by Chen Levy
Building on larsk's answerand VonC's answer, you can create a git_ssh.sh
script such as:
基于larsk的回答和VonC的回答,您可以创建一个git_ssh.sh
脚本,例如:
#!/bin/sh
# Workaround: GIT_SSH_COMMAND isn't supported by Git < 2.3
exec ${GIT_SSH_COMMAND:-ssh} "$@"
Then invoke your git
command like this:
然后git
像这样调用你的命令:
export GIT_SSH_COMMAND="/usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key"
export GIT_SSH=path/to/git_ssh.sh
git ...
This is how it works:
这是它的工作原理:
In Git v2.3+ $GIT_SSH_COMMAND
takes precedence over $GIT_SSH
, but older versions don't respect $GIT_SSH_COMMAND
at all.
在 Git v2.3+$GIT_SSH_COMMAND
中优先于$GIT_SSH
,但旧版本根本不尊重$GIT_SSH_COMMAND
。
$GIT_SSH
can hold only a path to the ssh
command on the system. It can't pass extra command line arguments to that command, so how can we pass extra arguments to ssh
?
$GIT_SSH
只能保存ssh
系统上命令的路径。它不能将额外的命令行参数传递给该命令,那么我们如何将额外的参数传递给ssh
?
A workaround is to create a script that includes the ssh
command and its extra arguments. This is exactly what the git_ssh.sh
is all about: Since we already set $GIT_SSH_COMMAND
to be /usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key
, it is exactly what we need to exec
, and the "$@"
is here to pass the arguments passed to git_ssh.sh
by Git itself to the $GIT_SSH_COMMAND.
解决方法是创建一个包含ssh
命令及其额外参数的脚本。这正是 thegit_ssh.sh
的全部内容:由于我们已经设置$GIT_SSH_COMMAND
为/usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key
,这正是我们需要的exec
,并且 在"$@"
这里将git_ssh.sh
Git 本身传递给的参数传递给$GIT_SSH_COMMAND.
The ${...:-ssh}
part, while not strictly needed is a nice touch that will make $GIT_SSH_COMMAND
default to the ssh
command, and thus setting GIT_SSH=git_ssh.sh
will not break a normal git
execution.
该${...:-ssh}
部分虽然不是严格需要的,但它是一个很好的接触,它将$GIT_SSH_COMMAND
默认为ssh
命令,因此设置GIT_SSH=git_ssh.sh
不会破坏正常git
执行。
As added value, this script is totally ignored by Git v2.3+, and the $GIT_SSH_COMMAND
is used directly in this case.
作为附加值,这个脚本被 Git v2.3+ 完全忽略了,$GIT_SSH_COMMAND
在这种情况下直接使用。
回答by alvinabad
You can supply any keyfile you wish to use with the Git command like this:
你可以提供任何你想与 Git 命令一起使用的密钥文件,如下所示:
$ PKEY=~/.ssh/keyfile.pem git clone [email protected]:me/repo.git
or this:
或这个:
$ git.sh -i ~/.ssh/keyfile.pem clone [email protected]:me/repo.git
I answered the same question here: https://stackoverflow.com/a/15596980
我在这里回答了同样的问题:https: //stackoverflow.com/a/15596980
See link for details.
详情见链接。