使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:13:06  来源:igfitidea点击:

Git clone with custom SSH using GIT_SSH error

gitsshenvironment-variablesclonegit-clone

提问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_SSHenvironment variable; from the gitman 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/configfile with the appropriate configuration:

一种选择是.ssh/config使用适当的配置向您的文件添加一个节:

Host bitbucket.org
  StrictHostKeyChecking no
  IdentityFile /home/me/my_private_key

Another option is to point GIT_SSHto 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_SSHat /home/me/bin/bitbucket_ssh.

然后指向GIT_SSH/home/me/bin/bitbucket_ssh

I prefer using .ssh/configwhen 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):

犯3994276托马斯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_SSHwhen 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 the GIT_SSH_COMMANDenvironment variable, which behaves like GIT_SSHbut 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 using GIT_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 default SSHport.

$GIT_SSH_COMMANDtakes precedence over $GIT_SSH, and is interpreted by the shell, which allows additional arguments to be included.
$GIT_SSHon 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 pushssh”在需要连接到远程系统时将使用指定的命令而不是“ ”。
该命令将给出两个或四个参数:

  • 来自 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.shscript 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 gitcommand 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_COMMANDtakes precedence over $GIT_SSH, but older versions don't respect $GIT_SSH_COMMANDat all.

在 Git v2.3+$GIT_SSH_COMMAND中优先于$GIT_SSH,但旧版本根本不尊重$GIT_SSH_COMMAND

$GIT_SSHcan hold only a path to the sshcommand 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 sshcommand and its extra arguments. This is exactly what the git_ssh.shis all about: Since we already set $GIT_SSH_COMMANDto 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.shby 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.shGit 本身传递给的参数传递给$GIT_SSH_COMMAND.

The ${...:-ssh}part, while not strictly needed is a nice touch that will make $GIT_SSH_COMMANDdefault to the sshcommand, and thus setting GIT_SSH=git_ssh.shwill not break a normal gitexecution.

${...:-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_COMMANDis 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.

详情见链接。