git 我没有 ~/.ssh/config 文件但多个 GitHub 帐户有效,如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49587932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:05:12  来源:igfitidea点击:

I don't have ~/.ssh/config file but multiple GitHub accounts works, how?

gitgithubssh-keys

提问by Marian Pa?dzioch

I'm trying to configure another GitHub account to be able work from my machine. I started to looking around and I found:

我正在尝试配置另一个 GitHub 帐户以便能够在我的机器上工作。我开始环顾四周,发现

Set up multiple ssh profiles by creating/modifying ~/.ssh/config. Note the slightly differing 'Host' values:

通过创建/修改 ~/.ssh/config 来设置多个 ssh 配置文件。请注意略有不同的“主机”值:

So as I (already) have multiple accounts working (I don't remember how and when I did that) I wanted to just add another entry in ~/.ssh/config. But it appears that I don't have such file. Maybe it is located somewhere else? Or maybe my machine is configured in some other way? Where to start searching?

因此,由于我(已经)有多个帐户在工作(我不记得我是如何以及何时这样做的),因此我只想在~/.ssh/config. 但似乎我没有这样的文件。也许它位于其他地方?或者也许我的机器以其他方式配置?从哪里开始搜索?

回答by Timothy Truckle

This is behavior of sshrather than git.

这是ssh而不是 git 的行为。

In your ~/.sshfolder you have your private key. The name of the file for your private key is id_rsaby default. When ever your ssh client has to login to a ssh server it reads the key from that file.

在您的~/.ssh文件夹中,您有您的私钥。id_rsa默认情况下,您的私钥文件的名称是。当您的 ssh 客户端必须登录到 ssh 服务器时,它会从该文件中读取密钥。

But in some cases you may want to use a different private key for authentication. In that case you create the file ~/.ssh/configand you add a section with an alias (the host name you write at the command line) the real host name and the path to he alternative key file on your local system:

但在某些情况下,您可能希望使用不同的私钥进行身份验证。在这种情况下,您创建文件~/.ssh/config并添加一个部分,其中包含别名(您在命令行中写入的主机名)、真实主机名和本地系统上备用密钥文件的路径:

# ~/.ssh/config
Host alternative-github
    HostName github.com
    User MyOtherGithubUser
    IdentityFile /media/me/MyUsbThumbDrive/.ssh/MyOtherGithubUsers-id_rsa

Whit this configuration sshlooks on my UBS thumb drive for the private key when I clone the repo like this:

当我像这样克隆 repo 时,这个配置ssh在我的 UBS 拇指驱动器上查找私钥:

git clone git@alternative-github:/MyOtherGithubUser/someRepositoty.git

conclusion

结论

As long as you use the same public/private key pair with your git projects and the private key is available as ~/.ssh/id_rsayou don't need the file ~/.ssh/config.

只要您在 git 项目中使用相同的公钥/私钥对,并且私钥可用,因为~/.ssh/id_rsa您不需要文件~/.ssh/config.

回答by VonC

I wanted to just add another entry in ~/.ssh/config

我只想添加另一个条目 ~/.ssh/config

You can create it, but:

您可以创建它,但是:

  • you need to have generate public/private keys with a different name

    ssh-keygen -q -P "" -t rsa -f ~/.ssh/key2
    
  • you need to have registered ~/.ssh/key2.pubon your remote server second account

  • you need to have a config file with:

    Host github2
    HostName github.com
    User git
    IdentityFile /home/me/.ssh/key2
    
  • 您需要生成具有不同名称的公钥/私钥

    ssh-keygen -q -P "" -t rsa -f ~/.ssh/key2
    
  • 您需要~/.ssh/key2.pub在远程服务器上注册第二个帐户

  • 你需要有一个配置文件:

    Host github2
    HostName github.com
    User git
    IdentityFile /home/me/.ssh/key2
    

Note the User here: 'git', not'another GitHub account'

请注意此处的用户:“ git”,而不是another GitHub account

  • you need to change the origin remote URL to use that entry:

    git remote set-url origin github2:MySecondAccount/MyRepo.git
    
  • 您需要更改源远程 URL 以使用该条目:

    git remote set-url origin github2:MySecondAccount/MyRepo.git