在 linux 上为 git 私有 SSH 密钥设置自定义路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5323778/
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
Setting a custom path for git private SSH key on linux
提问by ripper234
I'm trying to setup a git client on linux. I uploaded my private key to the machine, and I understand that I should put it in ~/.ssh, but I don't have access to that folder.
我正在尝试在 linux 上设置一个 git 客户端。我上传了我的私钥到机器上,我知道我应该把它放在 ~/.ssh 中,但我无权访问该文件夹。
How can I tell git to look for the private key somewhere else?
我如何告诉 git 在其他地方寻找私钥?
回答by Felipe Sabino
You can achieve that using a ssh config file.
您可以使用ssh 配置文件来实现这一点。
First create a file inside your ~/.ssh
folder named config
, you can use some command like the following
首先在您的~/.ssh
文件夹中创建一个名为的文件config
,您可以使用如下命令
$ nano ~/.ssh/config
Then, the content of the file should have the location of your key based on each host name. for example:
然后,文件的内容应该包含基于每个主机名的密钥位置。例如:
Host github.com
IdentityFile ~/myPublicKeyFolder/myGitHubFile
Host heroku.com
IdentityFile ~/myPublicKeyFolder/myHerokuFile
So, when git tries to access each host it will follow the rules inside this config file based on the git host your trying to reach
因此,当 git 尝试访问每个主机时,它将根据您尝试访问的 git 主机遵循此配置文件中的规则
回答by vhallac
One option is to use ssh-agent
and provide a file name to ssh-add
.
一种选择是使用ssh-agent
文件名并将其提供给ssh-add
.
For example:
例如:
$ ssh-agent /bin/bash
$ ssh-add ~/mykeys/id_rsa
回答by Pa?lo Ebermann
I would have said put the file name in ~/.ssh/config
, but you likely would not have access to this file, too.
我会说将文件名放在 中~/.ssh/config
,但您可能也无法访问此文件。
You can give ssh
the private key to use with the -i keyfile
option.
您可以提供ssh
与该-i keyfile
选项一起使用的私钥。
Now how to say git which options to pass to ssh?
现在如何说 git 将哪些选项传递给 ssh?
The GitTips pagesays create a wrapper script and point to it with the GIT_SSH
environment variable.
该GitTips页说,创建一个包装脚本和指向它与GIT_SSH
环境变量。
It looks like you also can use the git configuration core.gitProxy
, but I did not find a good example and some mailing list messagesuggests it is only for the git:
protocol.
看起来你也可以使用 git configuration core.gitProxy
,但我没有找到一个好的例子,一些邮件列表消息表明它仅适用于git:
协议。
回答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 m79lkm
For a project I am working on my app needs to spit out a shell script with all of the git commands to init/commit/push to an external repository. The ~/.ssh/config is off limits so I have my public/private keys in my app directory. I used vhallac's answer. This is what I had to do in my shell script to use my key:
对于我正在处理我的应用程序的项目,需要使用所有 git 命令生成一个 shell 脚本来初始化/提交/推送到外部存储库。~/.ssh/config 不受限制,所以我的应用程序目录中有我的公钥/私钥。我使用了 vhallac 的答案。这是我在我的 shell 脚本中必须做的才能使用我的密钥:
eval `/usr/bin/ssh-agent`
ssh-add /path/to/.ssh/id_rsa
hope this helps someone
希望这有助于某人