我选择了GIT_SSH环境变量。 这是我的包装器,类似于上面的Joe Block,但处理任意数量的参数。
文件〜/ gitwrap.sh
#!/bin/bash ssh -i ~/.ssh/gitkey_rsa "$@"
然后,在我的.bashrc中,添加以下内容:
export GIT_SSH=~/gitwrap.sh
如上所述: https://superuser.com/a/912281/607049
您可以按每个仓库配置它:
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null" git pull git push
您可以使用GIT_SSH环境变量。但是您需要将ssh和options包装到shell脚本中。
见git手册: man git 在你的命令shell中。
man git
我用 zsh 并且不同的键被加载到我的zsh shell中 ssh-agent 在我的笔记本电脑上自动用于其他目的(即访问远程服务器)。我修改了@ Nick的答案,我将它用于我的一个需要经常更新的回购。 (在这种情况下,这是我的 dotfiles 我想在所有机器上使用相同和最新版本,无论我在哪里工作。)
zsh
ssh-agent
dotfiles
bash -c 'eval `ssh-agent`; ssh-add /home/myname/.dotfiles/gitread; ssh-add -L; cd /home/myname/.dotfiles && git pull; kill $SSH_AGENT_PID'
cd
这些解决方案都不适合我。
相反,我详细阐述了@Martin v.L?wis提到的设置 config SSH文件。
config
SSH将查找用户的 ~/.ssh/config 文件。我的设置如下:
~/.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
的 笔记 强>
IdentitiesOnly yes
~/.ssh/id_rsa
~/.ssh/id_rsa.github
的 参考 强>
my_git_ssh_wrapper的内容:
#!/bin/bash ssh -i /path/to/ssh/secret/key $1 $2
然后你可以通过这样做来使用密钥:
GIT_SSH=my_git_ssh_wrapper git clone git@github.com:TheUser/TheProject.git
我的诀窍是使用git @ hostname而不是 HTTP://主机名
其中许多解决方案看起来很诱人。但是,我发现以下链接中的通用git-wrapping-script方法是最有用的:
如何使用。指定ssh密钥文件 git 命令
git
关键在于没有 git 命令如下:
git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Alvin的解决方案是使用一个明确定义的bash-wrapper脚本来填补这个空白:
git.sh -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
哪里 git.sh 是:
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 "$@"
我可以验证这解决了我用远程bitbucket repo的用户/密钥识别问题 git remote update , git pull ,和 git clone ;所有这些现在都可以正常工作了 cron 在导航有限shell时遇到问题的作业脚本。我也能从R中调用这个脚本,但仍然可以完全相同 cron 执行问题 (例如。 system("bash git.sh -i ~/.ssh/thatuserkey.pem pull") )。
git remote update
git pull
git clone
cron
system("bash git.sh -i ~/.ssh/thatuserkey.pem pull")
不是R和Ruby一样,但如果R可以做到...... O :-)
从Git 2.3.0开始,我们也有简单的命令(不需要配置文件):
GIT_SSH_COMMAND='ssh -i private_key_file' git clone user@host:repo.git
您可能需要重新启动计算机上的ssh服务。
为了 的 gitlab 强> RSAAuthentication yes
RSAAuthentication yes
Host gitlab.com RSAAuthentication yes IdentityFile ~/.ssh/your_private_key_name IdentitiesOnly yes
doc就在这里
如果您的路径上有要使用给定标识文件进行签名的目录,则可以通过设置为.ssh / config文件指定使用特定标识文件 ControlPath 例如。:
ControlPath
host github.com ControlPath ~/Projects/work/** HostName github.com IdentityFile ~/.ssh/id_work User git
然后 ssh 在给定的工作路径下执行git命令时将使用指定的标识文件。
ssh
GIT_SSH_COMMAND =“ssh -i / path / to / git-private-access-key”git clone $ git_repo
这是我在找到这个问题的解决方案时发现的ssh密钥hack:
例如,您有两组不同的键:
key1, key1.pub, key2, key2.pub
把这些钥匙留在你的 .ssh 目录
.ssh
现在在你的 .bashrc 要么 .bash_profile 别名文件,添加这些命令
.bashrc
.bash_profile
alias key1='cp ~/.ssh/key1 id_rsa && cp ~/.ssh/key1.pub id_rsa.pub'
alias key2='cp ~/.ssh/key2 id_rsa && cp ~/.ssh/key2.pub id_rsa.pub'
瞧!你有一个快捷方式可以随时切换键!
希望这对你有用。
当您需要使用正常请求连接到github时( git pull origin master ),将主机设置为 * 在 ~/.ssh/config 为我工作,任何其他主机(例如,“github”或“gb”)都无法正常工作。
git pull origin master
*
Host * User git Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_xxx
这样的东西应该工作(由orip建议):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
如果您更喜欢子壳,您可以尝试以下(虽然它更脆弱):
ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)
Git将调用SSH,它将通过环境变量找到它的代理;反过来,这将加载密钥。
或者,设置 HOME 如果你愿意设置一个只包含一个的目录,也可以做到这一点 .ssh 目录为 HOME ;这可能包含identity.pub或a 配置文件 设置IdentityFile。
HOME
更好的想法是将主机或ip添加到 .ssh/config 像这样的文件:
.ssh/config
Host (a space separated list of made up aliases you want to use for the host) User git Hostname (ip or hostname of git server) PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_(the key you want for this repo)
如果你像我一样,你可以:
保持你的ssh键有条理
保持你的git clone命令简单
处理任意数量的存储库的任意数量的密钥。
减少ssh密钥维护。
我把钥匙放在我的钥匙里 ~/.ssh/keys 目录。
~/.ssh/keys
我更喜欢约定优于配置。
我认为代码是法律;它越简单越好。
的 第1步 - 创建别名 强>
将此别名添加到shell: alias git-clone='GIT_SSH=ssh_wrapper git clone'
alias git-clone='GIT_SSH=ssh_wrapper git clone'
的 第2步 - 创建脚本 强>
加上这个 的 ssh_wrapper 强> 脚本到您的路径:
#!/bin/bash # Filename: ssh_wrapper if [ -z ${SSH_KEY} ]; then SSH_KEY='github.com/l3x' # <= Default key fi SSH_KEY="~/.ssh/keys/${SSH_KEY}/id_rsa" ssh -i "${SSH_KEY}" "$@"
的 例子 强>
使用github.com/l3x密钥:
KEY=github.com/l3x git-clone https://github.com/l3x/learn-fp-go
以下示例也使用了 的 github.com/l3x 强> 键(默认情况下):
git-clone https://github.com/l3x/learn-fp-go
使用bitbucket.org/lsheehan密钥:
KEY=bitbucket.org/lsheehan git-clone git@bitbucket.org:dave_andersen/exchange.git
将ssh_wrapper脚本中的默认SSH_KEY更改为您大多数时间使用的内容。这样,您不需要在大多数时间使用KEY变量。
你可能会想,“嘿!这是一个别名,一个脚本和一些密钥目录,但是对我来说这是常规。”我的几乎所有工作站(以及相关的服务器)都配置相似。
我的目标是简化我定期执行的命令。
我的约定,例如Bash脚本,别名等,创建了一致的环境,并帮助我保持简单。
KISS和名字很重要。
有关更多设计提示,请查看 第4章Go中的SOLID设计 从我的书: https://www.amazon.com/Learning-Functional-Programming-Lex-Sheehan-ebook/dp/B0725B8MYW
希望有所帮助。 - Lex
总结答案和 评论 ,设置git以使用不同密钥文件然后忘记它的最佳方法,它也支持同一主机的不同用户(例如个人GitHub帐户和工作帐户),也适用于Windows,是编辑 ~/.ssh/config (要么 c:\Users\<your user>\.ssh\config )并指定多个身份:
c:\Users\<your user>\.ssh\config
Host github.com HostName github.com IdentityFile /path/to/your/personal/github/private/key User dandv Host github-work HostName github.com IdentityFile /path/to/your/work/github/private/key User workuser
然后,要将项目克隆为您的个人用户,只需运行常规项目即可 git clone 命令。
将repo克隆为 workuser , 跑 git clone git@github-work:company/project.git 。
workuser
git clone git@github-work:company/project.git
您需要创建一个〜/ .ssh / config,如下所示
Host <Your bitbucket server> User <userid> Hostname <Your bitbucket server as above> IdentitiesOnly yes IdentityFile ~/.ssh/id_rsa<file> This is your private key file
许可如下
-rw------- $HOME/.ssh/config
将您的公钥添加到您的git(cat~ / .ssh / id_rsa_pub [或simillar name])
然后git克隆如下
git clone ssh://blahblah@blah.com/userid/test.git