这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行shell(git)命令时要使用的私有SSH密钥。

基本上是这样的:

git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"

或者更好(用Ruby):

with_key("/home/christoffer/ssh_keys/theuser") do
  sh("git clone git@github.com:TheUser/TheProject.git")
end

我见过使用Net::SSH连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?


当前回答

有很多好答案,但其中一些人假定事先具备管理知识。

我认为必须明确强调,如果您通过克隆web URL开始项目- https://github.com/<用户名>/<项目名称>.git那么您需要确保.git/config中[remote“origin”]下的url值已更改为SSH url(请参见下面的代码块)。

除此之外,请确保添加sshCommand,如下所述:

user@workstation:~/workspace/project-name/.git$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    sshCommand = ssh -i ~/location-of/.ssh/private_key -F /dev/null <--Check that this command exist
[remote "origin"]
    url = git@github.com:<user-name>/<project-name>.git  <-- Make sure its the SSH URL and not the WEB URL
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

在这里了解更多信息。

其他回答

最快、最简单的方法是:

使用ssh克隆您的repo:

git-c core.sshCommand=“ssh-i~/.ssh/<your_key>”克隆git@github.com:<user>/<repo>.git

然后cd到克隆的repo中,然后:

git-config-core.shCommand'ssh-i~/.ssh/<your_key>'

要测试它是否工作:

git--git-dir=/path/to/repo/.git pull

所以你可能会想:为什么我在github中安装了.pub,而private在默认目录中之后,我创建的ssh密钥就不起作用了?

该文档为我们提供了一个澄清问题的命令:ssh-vTgit@github.com

输出显示了git查找的ssh密钥列表。因此,您可能希望使用这些名称之一创建密钥,或者使用上述过程来包含所需的密钥。

这些解决方案都不适合我。

相反,我详细介绍了@Martin v.Löwis提到的为SSH设置配置文件。

SSH将查找用户的~/.SSH/config文件。我的设置如下:

Host gitserv
    Hostname remote.server.com
    IdentityFile ~/.ssh/id_rsa.github
    IdentitiesOnly yes # see NOTES below

我添加了一个远程git存储库:

git remote add origin git@gitserv:myrepo.git

然后git命令对我来说正常工作。

git push -v origin master

笔记

IdentitesOnly yes是必需的,以防止SSH默认行为发送与每个协议的默认文件名匹配的标识文件。如果您有一个名为~/.ssh/id_rsa的文件,则在不使用此选项的情况下,将在~/.sss/id_rsa.gitub之前尝试该文件。

工具书类

在一个客户端上使用多个SSH私钥的最佳方法我怎样才能阻止ssh提供错误的密钥

2021。如果你在Mac上。

假设你在aws上有一个ubuntu服务器,你通常会这样连接:

% ssh -i blah/yourkeypair.pem ubuntu@test.fattie.com

仅在终端中

% export GIT_SSH_COMMAND="ssh -i /Users/fattie/Desktop/blah/yourkeypair.pem"

在你完成之后。然后你可以自由地。。。

% git clone ubuntu@test.fattie.com:/home/ubuntu/teste.git  

这会将服务器上的repo克隆到本地文件夹“teste”,

然后,在teste/执行通常的命令时,您可以自由地执行。。。

% git push origin master

等等

--

另请注意:https://stackoverflow.com/a/67287133/294884


至于在服务器上,似乎基本上

] git clone --bare the-actual-folder teste.git

然后在teste.git中

] git init --bare --shared

其中许多解决方案看起来很诱人。然而,我发现以下链接中的通用git包装脚本方法最有用:

如何使用git命令指定ssh密钥文件

问题是没有git命令,例如:

git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git

Alvin的解决方案是使用一个定义良好的bash包装器脚本来填补这一空白:

git.sh -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git

其中git.sh为:

#!/bin/bash

# The MIT License (MIT)
# Copyright (c) 2013 Alvin Abad
# https://alvinabad.wordpress.com/2013/03/23/how-to-specify-an-ssh-key-file-with-the-git-command

if [ $# -eq 0 ]; then
    echo "Git wrapper script that can specify an ssh-key file
Usage:
    git.sh -i ssh-key-file git-command
    "
    exit 1
fi

# remove temporary file on exit
trap 'rm -f /tmp/.git_ssh.$$' 0

if [ "$1" = "-i" ]; then
    SSH_KEY=$2; shift; shift
    echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
    chmod +x /tmp/.git_ssh.$$
    export GIT_SSH=/tmp/.git_ssh.$$
fi

# in case the git command is repeated
[ "$1" = "git" ] && shift

# Run the git command
git "$@"

我可以验证这是否解决了我在使用git远程更新、git pull和git克隆进行远程比特桶回购时遇到的用户/密钥识别问题;所有这些现在都可以在cron作业脚本中正常工作,否则在有限的shell中导航会遇到困难。我还能够从R中调用这个脚本,并且仍然可以解决完全相同的cron执行问题(例如系统(“bash-git.sh-i~/.ssh/thatuserkey.pem pull”)。

并不是说R和Ruby一样,但如果R能做到这一点…O:-)

要使GIT_SSH_COMMAND环境变量在Windows(CMD)下工作,而不是:

set GIT_SSH_COMMAND="ssh -i private_key_file"

Use:

set "GIT_SSH_COMMAND=ssh -i private_key_file"

报价必须是这样的

set "variable=value" 

一些背景:https://stackoverflow.com/a/34402887/10671021