为给定域的git push指定SSH密钥

为给定域的git push指定SSH密钥,git,ssh,gitolite,Git,Ssh,Gitolite,我有以下用例:我希望能够推送到git@git.company.com:gitolite admin使用用户的私钥gitolite admin,而我想推送到git@git.company.com:一些使用“我自己的”私钥的回复。好吧,我不能用~/.ssh/config解决这个问题,因为用户名和服务器名在这两种情况下都是相同的。由于我主要使用自己的私钥,我在~/.ssh/config中为git@git.company.com。有人知道一种方法来覆盖用于单个git调用的键吗 (旁白:gitolite根

我有以下用例:我希望能够推送到
git@git.company.com:gitolite admin
使用用户的私钥
gitolite admin
,而我想推送到
git@git.company.com:一些使用“我自己的”私钥的回复
。好吧,我不能用
~/.ssh/config
解决这个问题,因为用户名和服务器名在这两种情况下都是相同的。由于我主要使用自己的私钥,我在
~/.ssh/config
中为
git@git.company.com
。有人知道一种方法来覆盖用于单个
git
调用的键吗


(旁白:gitolite根据密钥区分谁在进行推送,因此,从访问、所有权和审计的角度来看,推送并不是一个问题user@server字符串对于不同的用户是相同的。)

即使用户和主机是相同的,也可以在
~/.ssh/config
中区分它们。例如,如果您的配置如下所示:

Host gitolite-as-alice
  HostName git.company.com
  User git
  IdentityFile /home/whoever/.ssh/id_rsa.alice
  IdentitiesOnly yes

Host gitolite-as-bob
  HostName git.company.com
  User git
  IdentityFile /home/whoever/.ssh/id_dsa.bob
  IdentitiesOnly yes
然后您只需使用
gitolite作为alice
gitolite作为bob
而不是URL中的主机名:

git remote add alice git@gitolite-as-alice:whatever.git
git remote add bob git@gitolite-as-bob:whatever.git

您希望包括选项
identiesonly yes
,以防止使用默认ID。否则,如果您还有与默认名称匹配的id文件,则会首先尝试这些文件,因为与其他配置选项(遵循“wins中的第一个”不同,
IdentityFile
选项会将其添加到要尝试的身份列表中。请参阅:

解决此问题的另一种方法是使用别名,该别名将使用备用SSH密钥在任何远程设备上运行任何git命令。其基本思想是在运行git命令时切换SSH标识

与另一个答案中的主机别名方法相比的优势:

  • 将与任何git命令或别名一起使用,即使您不能明确指定
    远程
  • 使用多个存储库更容易,因为您只需要在每台客户机上设置一次,而不需要在每台客户机上的每个存储库中设置一次
我使用了一些小脚本和git别名
admin
。我可以这样做,例如:

git admin push 
使用备用(“admin”)SSH密钥推送到默认远程设备。同样,您可以对该别名使用任何命令(不仅仅是
push
)。您甚至可以执行
git admin clone…
来克隆一个只有使用“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}"
第4步:添加别名(使用适用于下面“路径到脚本\u目录”的内容):


更多详细信息请访问:

另一种选择是使用


它会根据您当前的工作目录、ssh选项等自动加载和使用不同的密钥。。。这意味着您可以很容易地拥有一个工作/目录和私有/目录,它们透明地使用不同的密钥和ssh标识。

如果在windows上使用Git版本的ssh,ssh配置中的标识文件行如下所示

IdentityFile /c/Users/Whoever/.ssh/id_rsa.alice
其中
/c
表示
c:

检查一下,在git的bash中

cd ~/.ssh
pwd 

我在Win7上使用Git Bash。以下几点对我有用

在~/.ssh/config或c:/users/[your_user\u 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提供的解决方案也很棒。

一个基于Unix的系统(Linux、BSD、Mac OS X),默认标识存储在目录$HOME/.ssh中的两个文件中:

私钥:$HOME/.ssh/id\u rsa
公钥:$HOME/.ssh/id_rsa.pub
当您不使用选项
-i
而使用
ssh
时,它使用默认私钥向远程系统进行身份验证

如果要使用另一个私钥,例如$HOME/.ssh/deploy\u key,则必须使用
ssh-i~/.ssh/deploy\u key…

真烦人。您可以将以下行添加到$HOME/.bash\u配置文件中:

ssh添加~/.ssh/deploy\u密钥
ssh add~/.ssh/id\u rsa

因此,每次使用
ssh
git
scp
(基本上也是
ssh
),就不必再使用选项
-i


您可以在文件$HOME/.bash\u profile

中添加任意数量的密钥。您可以使用git环境变量
git\u SSH\u命令
。在git存储库下的终端中运行此命令:

GIT_SSH_COMMAND='ssh -i ~/.ssh/your_private_key' git submodule update --init
用您想要使用的ssh私钥路径替换
~/.ssh/您的\u private\u key
。您可以将随后的git命令(示例中是
git子模块更新--init
)更改为其他命令,如
git pull
git fetch
,等等。

您可能需要删除(或注释掉)默认主机配置

您在文件配置密钥ssh中指定的最多的内容是:

# Default GitHub user
Host one
 HostName gitlab.com
 User git
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/key-one
 IdentitiesOnly yes

#two user
Host two
 HostName gitlab.com
 User git
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/key-two
 IdentitiesOnly yes

从git 2.10以上,还可以使用gitconfig sshCommand设置:

如果设置了此变量,git fetch和git push将在需要连接到远程系统时使用指定的命令而不是ssh。该命令与GIT_SSH_command环境变量的形式相同,并且在设置环境变量时被重写

一个使用示例是:
git config core.sshCommand“ssh-i~/.ssh/[insert\u your\u keyname]

在某些情况下,这不起作用,因为ssh_config覆盖了co
# Default GitHub user
Host one
 HostName gitlab.com
 User git
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/key-one
 IdentitiesOnly yes

#two user
Host two
 HostName gitlab.com
 User git
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/key-two
 IdentitiesOnly yes
# Personal GitHub
Host github.com
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/github_id_rsa

# Work GitHub
Host github-work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/work_github_id_rsa

Host *
  IdentitiesOnly yes
[user]
    name = My Name
    email = personal@personal.email

[includeIf "gitdir:~/dev/work/"]
    path = ~/dev/work/.gitconfig

[url "github-work:work-github-org/"]
    insteadOf = git@github.com:work-github-org/
[user]
    email = work@work.email
[core]
  sshCommand = "ssh -i ~/.ssh/workrsa"
[includeIf "gitdir:~/work/"]
  path = ~/work/.gitconfig
[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]
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
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
git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>'
git config core.sshCommand "ssh -i ~/.ssh/github-personal -o IdentitiesOnly=yes -F /dev/null"