-->

I don't have ~/.ssh/config file but multiple G

2020-07-08 07:16发布

问题:

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

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

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?

回答1:

This is behavior of ssh rather than git.

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

But in some cases you may want to use a different private key for authentication. In that case you create the file ~/.ssh/config and 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
Host alternative-github
    HostName github.com
    User MyOtherGithubUser
    IdentityFile /media/me/MyUsbThumbDrive/.ssh/MyOtherGithubUsers-id_rsa

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

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_rsa you don't need the file ~/.ssh/config.



回答2:

I wanted to just add another entry in ~/.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.pub on 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
    

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

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

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