I have the following use case: I would like to be able to push to git@git.company.com:gitolite-admin using the private key of user gitolite-admin, while I want to push to git@git.company.com:some_repo using 'my own' private key. AFAIK, I can't solve this using ~/.ssh/config, because the user name and server name are identical in both cases. As I mostly use my own private key, I have that defined in ~/.ssh/config for git@git.company.com. Does anyone know of a way to override the key that is used for a single git invocation?

(另外:gitolite根据键来区分谁在进行推送,所以就访问、所有权和审计而言,user@server字符串对于不同的用户是相同的,这不是问题。)


当前回答

另一种替代方法是使用ssh-ident来管理您的ssh身份。

它会根据您当前的工作目录、ssh选项等自动加载和使用不同的密钥……这意味着您可以轻松地拥有一个工作/目录和一个私有/目录,它们在ssh中透明地使用不同的密钥和身份。

其他回答

一个基于Unix的系统(Linux、BSD、Mac OS X),默认标识存储在$HOME/目录下。Ssh,分2个文件: 私钥:$HOME/.ssh/id_rsa 公钥:$HOME/.ssh/id_rsa.pub 当您使用没有选项-i的ssh时,它将使用默认的私钥对远程系统进行身份验证。

如果您有另一个想要使用的私钥,例如$HOME/。Ssh /deploy_key,您必须使用Ssh -i ~/。ssh / deploy_key……

这很烦人。您可以在$HOME/中添加以下行。bash_profile: ssh-add ~ / . ssh / deploy_key ssh-add ~ / . ssh / id_rsa

因此,每次使用ssh、git或scp(基本上也是ssh)时,都不必再使用option -i了。

您可以在$HOME/.bash_profile文件中添加任意多的键。

我在Win7上使用Git Bash。下面的方法对我很有效。

在~/处创建一个配置文件。Ssh /config或c:/users/[your_user_name]/. Ssh /config。在文件中输入:

Host your_host.com
     IdentityFile [absolute_path_to_your_.ssh]\id_rsa

我想主机必须是一个URL,而不仅仅是一个“名称”或参考您的主机。例如,

Host github.com
     IdentityFile c:/users/[user_name]/.ssh/id_rsa

也可以写入“/c/users/[user_name]/....”格式

Giordano Scalzo提供的解决方案也很棒。 https://stackoverflow.com/a/9149518/1738546

以下是简单的方法:

-从你的本地根git文件夹。

-检查当前本地存储库的认证方法类型为HTTPS或SSH。

   git remote -v

 Example using HTTPS:
 origin  https://github.com/juantorres9/SQLite_crash_cours.git (fetch)
 origin  https://github.com/juantorres9/SQLite_crash_cours.git (push)
 Example using SSH:
 origin  git@github.com:juantorres9/SQLite_crash_cours.git (fetch)
 origin  git@github.com:juantorres9/SQLite_crash_cours.git (push)

-设置您选择的身份验证方法,以防尚未设置。

 For setting SSH
 git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
 For setting HTTPS
 git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

-再检查一遍以防你想再次检查

 git remote -v

-你现在可以使用你配置的方法来使用git push:fetch。

git push origin master

请注意 最后但并非最不重要的,检查更新的官方文件 https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories

使用~/的一种可能性。ssh/config将使用Match限制而不是Host限制。特别是Match Exec调用shell命令来决定是否应用声明。在bash中,您可以使用以下命令:

[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]

它使用bash[命令来验证两个字符串是否相等。在这种情况下,它正在测试字符串git@git.company.com:gitolite-admin是否与从$(git config——get remote.origin.url) "命令获得的输出匹配。

您可以使用任何其他命令来标识shell所在的存储库。要做到这一点,重要的是在SHELL中定义$SHELL变量,在我的例子中是/bin/bash.完整的示例如下~/.ssh/config:

Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
  IdentityFile ~/.ssh/gitolite-admin
  IdentitiesOnly yes
  ForwardAgent no
  ForwardX11 no
  ForwardX11Trusted no

Match Exec "[ git@git.company.com:some_repo = $(git config --get remote.origin.url)'' ]"
  IdentityFile ~/.ssh/yourOwnPrivateKey
  IdentitiesOnly yes
  ForwardAgent no
  ForwardX11 no
  ForwardX11Trusted no

在这个例子中,我假设~/。ssh/yourOwnPrivateKey包含您自己的私钥和~/。Ssh /gitolite-admin包含gitolite-admin用户的私钥。我加入了IdentitiesOnly yes声明,以确保只有一个密钥提供给git服务器,Mark Longair提到过。其他声明只是git的标准ssh选项。

如果您有几个some_repo,想要与不同的键一起使用,您可以添加此配置。如果您在git@git.company.com上有多个存储库,并且大多数存储库使用~/。将此密钥作为主机的默认密钥更有意义。在这种情况下,~/。Ssh /config将是:

Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
  IdentityFile ~/.ssh/gitolite-admin
  IdentitiesOnly yes

Host git.company.com
  IdentityFile ~/.ssh/yourOwnPrivateKey
  IdentitiesOnly yes
  ForwardAgent no
  ForwardX11 no
  ForwardX11Trusted no

注意,顺序很重要,Host git.company.com限制应该出现在Match Exec一个或多个限制之后。

Mark Longair提供的上述方法的另一种替代方法是使用别名,该别名将在任何远程上运行任何git命令,并使用替代SSH密钥。这个想法基本上是在运行git命令时切换您的SSH身份。

相对于另一个答案中的主机别名方法的优点:

将与任何git命令或别名一起工作,即使您不能显式地指定远程。 更容易使用多个存储库,因为您只需要在每个客户端机器上设置一次,而不是在每个客户端机器上的每个存储库设置一次。

我使用了一些小脚本和一个git别名admin。这样我就可以做,例如:

git admin push 

使用替代SSH密钥(“admin”)推到默认远程。同样,对于这个别名,您可以使用任何命令(不仅仅是push)。你甚至可以做git管理克隆…克隆一个您只能使用“admin”密钥访问的存储库。

步骤1:创建替代SSH密钥,可选地设置一个密码短语,以防您在其他人的机器上执行此操作。

步骤2:创建一个名为“SSH -as.sh”的脚本,运行使用SSH的东西,但使用给定的SSH密钥,而不是默认的:

#!/bin/bash
exec ssh ${SSH_KEYFILE+-i "$SSH_KEYFILE"} "$@"

步骤3:创建一个名为“git-as.sh”的脚本,使用给定的SSH密钥运行git命令。

#!/bin/bash
SSH_KEYFILE=$1 GIT_SSH=${BASH_SOURCE%/*}/ssh-as.sh exec git "${@:2}"

第四步:添加一个别名(使用下面的“PATH_TO_SCRIPTS_DIR”):

# Run git commands as the SSH identity provided by the keyfile ~/.ssh/admin
git config --global alias.admin \!"PATH_TO_SCRIPTS_DIR/git-as.sh ~/.ssh/admin"

更多详情请访问:http://noamlewis.wordpress.com/2013/01/24/git-admin-an-alias-for-running-git-commands-as-a-privileged-ssh-identity/