在 Linux 上设置 git 私有 SSH 密钥的自定义路径
我正在尝试在 Linux 上设置 git 客户端。我将私钥上传到计算机,并且我知道我应该将其放在 ~/.ssh 中,但我无权访问该文件夹。
我如何告诉 git 在其他地方寻找私钥?
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.
How can I tell git to look for the private key somewhere else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 ssh 配置文件 来实现这一点。
首先在您的
~/.ssh
文件夹中创建一个名为config
的文件,您可以使用如下命令然后,文件的内容应该包含您的密钥的位置基于每个主机名。例如:
因此,当 git 尝试访问每个主机时,它将根据您尝试访问的 git 主机遵循此配置文件中的规则
You can achieve that using a ssh config file.
First create a file inside your
~/.ssh
folder namedconfig
, you can use some command like the followingThen, the content of the file should have the location of your key based on each host name. for example:
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
一种选择是使用 ssh-agent 并向 ssh-add 提供文件名。
例如:
One option is to use
ssh-agent
and provide a file name tossh-add
.For example:
我会说将文件名放入
~/.ssh/config
,但您可能也无权访问该文件。您可以为
ssh
提供私钥,以便与-i keyfile
选项一起使用。现在怎么说 git 将哪些选项传递给 ssh?
GitTips 页面 表示创建一个包装器脚本并指向它使用
GIT_SSH
环境变量。看起来你也可以使用git配置
core.gitProxy
,但我没有找到一个很好的例子一些邮件列表消息表明它仅适用于git:
协议。I would have said put the file name in
~/.ssh/config
, but you likely would not have access to this file, too.You can give
ssh
the private key to use with the-i keyfile
option.Now how to say git which options to pass to ssh?
The GitTips page says create a wrapper script and point to it with the
GIT_SSH
environment variable.It looks like you also can use the git configuration
core.gitProxy
, but I did not find a good example and some mailing list message suggests it is only for thegit:
protocol.使用 ssh 代理
Use ssh-agent
对于我正在开发的应用程序的项目,需要生成一个 shell 脚本,其中包含所有 git 命令来初始化/提交/推送到外部存储库。 ~/.ssh/config 是禁止的,所以我的公钥/私钥在我的应用程序目录中。我使用了 vhallac 的答案。这是我必须在 shell 脚本中执行的操作才能使用我的密钥:
希望这对某人有帮助
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:
hope this helps someone