bash 在 Git 上执行 shell 命令时如何指定要使用的私有 SSH 密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565700/
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
How to specify the private SSH-key to use when executing shell command on Git?
提问by Christoffer
A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (git) command from the local computer.
可能是一种相当不寻常的情况,但我想指定一个私有 SSH 密钥,以便在从本地计算机执行 shell (git) 命令时使用。
Basically like this:
基本上是这样的:
git clone [email protected]:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"
Or even better (in Ruby):
甚至更好(在 Ruby 中):
with_key("/home/christoffer/ssh_keys/theuser") do
sh("git clone [email protected]:TheUser/TheProject.git")
end
I have seen examples of connecting to a remote server with Net::SSH that uses a specified private key, but this is a local command. Is it possible?
我已经看到使用 Net::SSH 连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。是否可以?
采纳答案by Martin v. L?wis
Something like this should work (suggested by orip):
这样的事情应该可以工作(orip建议):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git'
if you prefer subshells, you could try the following (though it is more fragile):
如果您更喜欢子外壳,则可以尝试以下操作(尽管它更脆弱):
ssh-agent $(ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git)
Git will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.
Git 将调用 SSH,它将通过环境变量找到它的代理;反过来,这将加载密钥。
Alternatively, setting HOME
may also do the trick, provided you are willing to setup a directory that contains only a .ssh
directory as HOME
; this may either contain an identity.pub, or a config filesetting IdentityFile.
或者,设置HOME
也可以解决问题,前提是您愿意设置一个仅包含一个.ssh
目录的目录HOME
;这可能包含一个 identity.pub,或一个配置文件设置 IdentityFile。
回答by HeyWatchThis
None of these solutions worked for me.
这些解决方案都不适合我。
Instead, I elaborate on @Martin v. L?wis 's mention of setting a config
file for SSH.
相反,我详细说明了 @Martin v. L?wis 提到的config
为 SSH设置文件的内容。
SSH will look for the user's ~/.ssh/config
file. I have mine setup as:
SSH 将查找用户的~/.ssh/config
文件。我的设置为:
Host gitserv
Hostname remote.server.com
IdentityFile ~/.ssh/id_rsa.github
IdentitiesOnly yes # see NOTES below
And I add a remote git repository:
我添加了一个远程 git 存储库:
git remote add origin git@gitserv:myrepo.git
And then git commands work normally for me.
然后 git 命令对我来说正常工作。
git push -v origin master
NOTES
笔记
- The
IdentitiesOnly yes
is required to prevent the SSH default behaviorof sending the identity file matching the default filename for each protocol. If you have a file named~/.ssh/id_rsa
that will get tried BEFORE your~/.ssh/id_rsa.github
without this option.
- 在
IdentitiesOnly yes
需要防止SSH默认行为发送所述身份文件匹配每个协议的默认的文件名的。如果您有一个名为的文件~/.ssh/id_rsa
,它将在~/.ssh/id_rsa.github
没有此选项的情况下在您之前尝试。
References
参考
回答by Robert Hyman Will
Starting from Git 2.3.0 we also have the simple command (no config file needed):
从 Git 2.3.0 开始,我们还有简单的命令(不需要配置文件):
GIT_SSH_COMMAND='ssh -i private_key_file' git clone user@host:repo.git
回答by philfreo
Other people's suggestions about ~/.ssh/config
are extra complicated. It can be as simple as:
其他人的建议~/.ssh/config
比较复杂。它可以很简单:
Host github.com
IdentityFile ~/.ssh/github_rsa
回答by Joe Block
Contents of my_git_ssh_wrapper:
my_git_ssh_wrapper 的内容:
#!/bin/bash
ssh -i /path/to/ssh/secret/key
Then you can use the key by doing:
然后您可以通过执行以下操作来使用密钥:
GIT_SSH=my_git_ssh_wrapper git clone [email protected]:TheUser/TheProject.git
回答by VonC
With git 2.10+ (Q3 2016: released Sept. 2d, 2016), you have the possibility to set a configfor GIT_SSH_COMMAND
(and not just an environment variable as described in Rober Hyman Will's answer)
使用Git 2.10+(Q3 2016年公布的九月2D中,2016),你必须设置一个可能配置的GIT_SSH_COMMAND
(如描述,而不仅仅是一个环境变量罗伯特·Hyman威尔的回答)
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 forGIT_SSH_COMMAND
to use per repository.
添加了一个新的配置变量
core.sshCommand
来指定GIT_SSH_COMMAND
每个存储库使用的值。
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 -i private_key_file'
# later on
git clone host:repo.git
You can even set it for just one command:
你甚至可以只为一个命令设置它:
git -c core.sshCommand="ssh -i private_key_file" clone host:repo.git
This is easier than setting a GIT_SSH_COMMAND
environment variable, which, on Windows, as notedby Mátyás Kuti-Kreszács, would be
这比设定更简单GIT_SSH_COMMAND
的环境变量,其中,在Windows上,作为指出通过马亚斯·库蒂- Kreszács,将
set "GIT_SSH_COMMAND=ssh -i private_key_file"
回答by Dan Dascalescu
To sum up answers and comments, the best way to set up git to use different key files and then forget about it, which also supports different users for the same host (e.g. a personal GitHub account and a work one), which works on Windows as well, is to edit ~/.ssh/config
(or c:\Users\<your user>\.ssh\config
) and specify multiple identities:
总结一下答案和评论,最好的办法是设置git使用不同的key文件然后算了,它也支持同一主机的不同用户(例如个人GitHub帐户和工作帐户),适用于Windows同样,是编辑~/.ssh/config
(或c:\Users\<your user>\.ssh\config
)并指定多个身份:
Host github.com
HostName github.com
IdentityFile /path/to/your/personal/github/private/key
User dandv
Host github-work
HostName github.com
IdentityFile /path/to/your/work/github/private/key
User workuser
Then, to clone a project as your personal user, just run the regular git clone
command.
然后,要以您的个人用户身份克隆项目,只需运行常规git clone
命令即可。
To clone the repo as the workuser
, run git clone git@github-work:company/project.git
.
要将 repo 克隆为workuser
,请运行git clone git@github-work:company/project.git
.
回答by David
As stated here: https://superuser.com/a/912281/607049
如此处所述:https: //superuser.com/a/912281/607049
You can configure it per-repo:
您可以为每个 repo 配置它:
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push
回答by rodzmkii
The problem is when you have different remote repositories on the same host(say github.com), and you want to interact with them using different ssh keys(i.e. different GitHub accounts).
问题是当您在同一主机(例如 github.com)上有不同的远程存储库,并且您想使用不同的 ssh 密钥(即不同的 GitHub 帐户)与它们进行交互时。
In order to do that:
为了做到这一点:
First you should declare your different keys in
~/.ssh/config
file.# Key for usual repositories on github.com Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa # Key for a particular repository on github.com Host XXX HostName github.com User git IdentityFile ~/.ssh/id_other_rsa
By doing this you associate the second key with a new friendly name "XXX" for github.com.
Then you must change the remote origin of your particular repository, so that it uses the friendly name you've just defined.
Go to your local repository folder within a command prompt, and display the current remote origin:
>git remote -v origin [email protected]:myuser/myrepo.git (fetch) origin [email protected]:myuser/myrepo.git (push)
Then change origin with:
>git remote set-url origin git@XXX:myuser/myrepo.git >git remote -v origin git@XXX:myuser/myrepo.git (fetch) origin git@XXX:myuser/myrepo.git (push)
Now you can push, fetch... with the right key automatically.
首先,您应该在
~/.ssh/config
文件中声明不同的键。# Key for usual repositories on github.com Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa # Key for a particular repository on github.com Host XXX HostName github.com User git IdentityFile ~/.ssh/id_other_rsa
通过这样做,您可以将第二个密钥与 github.com 的新友好名称“XXX”相关联。
然后您必须更改特定存储库的远程来源,以便它使用您刚刚定义的友好名称。
在命令提示符下转到本地存储库文件夹,并显示当前的远程源:
>git remote -v origin [email protected]:myuser/myrepo.git (fetch) origin [email protected]:myuser/myrepo.git (push)
然后更改原点:
>git remote set-url origin git@XXX:myuser/myrepo.git >git remote -v origin git@XXX:myuser/myrepo.git (fetch) origin git@XXX:myuser/myrepo.git (push)
现在您可以使用正确的键自动推送、获取...。
回答by carlsborg
GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key" git clone $git_repo
or
或者
export GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key"
git clone REPO
git push