首页 文章

如何指定在Git上执行shell命令时使用的私有SSH密钥?

提问于
浏览
778

也许是一个相当不寻常的情况,但我想指定一个私有SSH密钥,以便在从本地计算机执行shell(git)命令时使用 .

基本上是这样的:

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连接到远程服务器的示例,它使用指定的私钥,但这是一个本地命令 . 可能吗?

20 回答

  • 57

    总结答案和comments,设置git以使用不同密钥文件然后忘记它的最佳方法,它也支持同一主机的不同用户(例如个人GitHub帐户和工作帐户),适用于Windows同样,是编辑 ~/.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 .

  • 49

    您需要创建一个〜/ .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
    
  • 946

    我使用 zsh 并在我的笔记本电脑上自动将不同的密钥加载到我的zsh shell的 ssh-agent 以用于其他目的(即访问远程服务器) . 我修改了@Nick 's answer and I' m,用于我需要经常刷新的一个repos . (在这种情况下,这是我的 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'
    
    • 生成一个ssh-agent

    • 向代理添加只读密钥

    • 将目录更改为我的git仓库

    • 如果 cd 到repo dir成功,请从远程仓库中取出

    • 杀死ssh-agent . (我不希望许多代理人徘徊 . )

  • 359

    gitlab RSAAuthentication yes

    Host gitlab.com
      RSAAuthentication yes
      IdentityFile ~/.ssh/your_private_key_name
      IdentitiesOnly yes
    

    doc is here

  • 571

    在使用Git Bash的Windows中,您可以使用以下内容添加存储库 ssh-agent bash -c 'ssh-add "key-address"; git remote add origin "rep-address"' ,例如: ssh-agent bash -c 'ssh-add /d/test/PrivateKey.ppk; git remote add origin git@git.test.com:test/test.git' 驱动器D中的哪个私钥,计算机的文件夹测试 . 此外,如果要克隆存储库,可以使用 git clone 更改 git remote add origin .

    输入Git Bash之后,它会询问你的密码!

    请注意openssh私钥和腻子私钥是不同的!

    如果您使用puttygen创建了密钥,则必须将私钥转换为openssh!

  • 79

    我使用了GIT_SSH环境变量 . 这是我的包装器,类似于上面的Joe Block,但处理任意数量的参数 .

    文件〜/ gitwrap.sh

    #!/bin/bash
    ssh -i ~/.ssh/gitkey_rsa "$@"
    

    然后,在我的.bashrc中,添加以下内容:

    export GIT_SSH=~/gitwrap.sh
    
  • 270

    如上所述:https://superuser.com/a/912281/607049

    您可以按per-repo配置它:

    git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
    git pull
    git push
    
  • 3

    您可以使用GIT_SSH环境变量 . 但是您需要将ssh和options包装到shell脚本中 .

    请参阅命令shell中的git manual: man git .

  • 29

    从Git 2.3.0开始,我们也有简单的命令(不需要配置文件):

    GIT_SSH_COMMAND='ssh -i private_key_file' git clone user@host:repo.git
    

    您可能需要重新启动计算机上的ssh服务 .

  • 124

    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
    
  • 0

    如果SSH端口号不是22(默认),请在 ~/.ssh/config 中添加 Port xx

    在我的情况下(synology),

    Host my_synology
        Hostname xxxx.synology.me
        IdentityFile ~/.ssh/id_rsa_xxxx
        User myname
        Port xx
    

    然后使用config中的主机 Headers 进行克隆 . (“my_synology” . 避免@chopstik的“*”)

    git clone my_synology:path/to/repo.git
    
  • 0

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

    相反,我详细阐述了@Martinv.Löwis提到为SSH设置 config 文件 .

    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
    

    NOTES

    • IdentitiesOnly yes 需要prevent the SSH default behavior发送与每个协议的默认文件名匹配的标识文件 . 如果您有一个名为 ~/.ssh/id_rsa 的文件,将在没有此选项的 ~/.ssh/id_rsa.github 之前尝试 .

    References

  • 7

    使用git 2.10(2016年第3季度:2016年9月2日发布),您可以为 GIT_SSH_COMMAND 设置配置(而不仅仅是Rober Jack Willanswer中描述的环境变量)

    commit 3c8ede3(2016年6月26日)Nguyễn Thái Ngọc Duy (pclouds) .
    (由Junio C Hamano合并 - gitster - 在提交dc21164,2016年7月19日)

    添加了一个新的配置变量core.sshCommand,用于指定每个存储库使用GIT_SSH_COMMAND的值 .

    core.sshCommand:
    

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

    这意味着 git clone 可以是:

    cd /path/to/my/repo
    git config core.sshCommand 'ssh -i private_key_file' 
    # later on
    git clone host:repo.git
    
  • 0

    当您需要使用正常请求( git pull origin master )连接到github时,在 ~/.ssh/config 中将主机设置为 * 对我来说,任何其他主机(例如,"github"或"gb")都无法正常工作 .

    Host *
        User git
        Hostname github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_xxx
    
  • 2

    其中许多解决方案看起来很诱人 . 但是,我找到了通用的git-wrapping-script在以下链接中的方法是最有用的:

    How to Specify an ssh Key File with the git command

    关键是没有 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 的位置是:

    #!/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 remote updategit pullgit clone 进行远程bitbucket repo的用户/密钥识别问题 . 所有这些现在在一个 cron 作业脚本中工作正常,否则导航有限shell时遇到问题 . 我也能从R中调用这个脚本,仍然可以解决完全相同的 cron 执行问题(例如 system("bash git.sh -i ~/.ssh/thatuserkey.pem pull") ) .

    不是R和Ruby一样,但如果R可以做到...... O :-)

  • 28

    如果这里没有其他解决方案适合你,并且你已经创建了多个ssh-key,但仍然不能做简单的事情

    git pull
    

    然后假设您有两个ssh密钥文件,如

    id_rsa
    id_rsa_other_key
    

    然后在你正在努力的git repo里面(cd进入那里),尝试:

    ssh-add ~/.ssh/id_rsa
    ssh-add ~/.ssh/id_rsa_other_key
    

    并确保您的github默认用户名和用户ID是正确的:

    git config user.name "Mona Lisa"
    git config user.email "mona.lisa@email.com"
    

    有关更多信息,请参见https://gist.github.com/jexchan/2351996 .

  • 0

    其他人关于 ~/.ssh/config 的建议比较复杂 . 它可以很简单:

    Host github.com
      IdentityFile ~/.ssh/github_rsa
    
  • 0

    更好的想法是将主机或ip添加到 .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)
    
  • 7

    我的诀窍是使用git @ hostname而不是http://hostname

  • 3

    这样的东西应该工作(由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,它将通过环境变量找到它的代理;反过来,这将加载密钥 .

    或者,如果您愿意将仅包含 .ssh 目录的目录设置为 HOME ,则设置 HOME 也可以执行此操作;这可能包含identity.pub或config file设置IdentityFile .

相关问题