coffeequant coffeequant - 1 month ago 17
Git Question

How do I use multiple SSH keys on GitHub?

The SSH configuration on GitHub seems to be a nightmare. I have multiple GitHub accounts, but for which I can have multiple SSH keys. In the GitHub SSH configuration section they mention this:


============

ssh-keygen -t rsa -C "your_email@example.com"


Creates a new ssh key, using the provided email as a label



Generating public/private rsa key pair.



We strongly suggest keeping the default settings as they are, so when you're prompted to "Enter a file in which to save the key", just press Enter to continue.

# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]




Why should I always use an id_rsa file? It will overwrite my existing keys. Anyway, I give a new name here and generate the key. I do all the other steps of adding it to agent, updating in the GitHub SSH keys section.

After completing all those steps I come to the final step which is:

ssh -vT git@github.com
Hi animesh11! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3128, received 1976 bytes, in 0.5 seconds
Bytes per second: sent 6077.0, received 3838.9
debug1: Exit status 1


Everything is hunky dory, but somehow
git clone
still complains:

MacBook-Pro:Documents animeshsaxena$ git clone git@github.com:regentmarkets/devbox.git
Cloning into 'devbox'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I am up to my wits end why the ssh -vT works and simple cloning doesn't. It doesn't seem logical, why would github put this step in the manual if they know it's freaking useless.


Any ideas?

Answer

I'd use .ssh config to set up different configurations for each particular user.

For example, edit (or create) the config file in the .ssh folder under your users root, and add something similar to this:

Host user1-github
    HostName github.com
    User git
    IdentityFile ~/.ssh/user1_rsa
Host user2-github
    HostName github.com
    User git
    IdentityFile ~/.ssh/user2_rsa

Where user1_rsa and user2_rsa are the outputs of ssh-keygen -t rsa -C "user1@example.com" and ssh-keygen -t rsa -C "user2@example.com"

Then, when testing simply use ssh -vT user1-github and ssh -vT user2-github.

Also, when cloning repos use git clone user1-github:username1/project.git or git clone user2-github:username2/project.git.