Sometimes you just need to have multiple Github accounts setup on your computer. In my case I needed 2 accounts to differentiate my personal Github account from my freelance business’s Github account. Here’s what I had to run in order to setup another account with it’s own keys. So in this example, I want to setup my computer with another Github account, sarn1.
So let’s fire-up the terminal!
Goto where the SSH keys are located.
>> cd ~/.ssh
Time to generate a new key, replace [email protected] with your Github email. However the C flag stands for comments and you can really just fill this flag with anything since its only used for identification when you open up the .pub key file. But best practice is to use the your email associated to the Github account you want to use this key for.
>> ssh-keygen -t rsa -C "[email protected]"
A prompt will ask you to enter a in which to save the key. For generating Github keys I typically will do id_rsa_xxx where xxx is the Github username the key will be used for.
>> id_rsa_sarn1
In your SSH folder, you should see 2 new files created. Which would be your public and private keys. So id_rsa_sarn1 and id_rsa_sarn1,pub is what I saw. Also see if you have a file called config.
>> ls
Now you need to open the public key file and copy the entire key that is generated from you so that you can put that in Github. The public key would be in the file ending with .pub. So for me I would run:
>> vim config
This would open up the pub file in vim so that you can copy the entire hash key into your clipboard. Then go to Github and navigate towards your settings area. There should then be an area where you can add an SSH key. Once you find that place, paste the public key in your clipboard there. Save it and go back to your terminal where vim is waiting. To exit vim, hold down shift + q. And then press q.
Next you’ll want to create or append to a configuration file. If you have never done this before, meaning you didn’t see any config file in the directory after running lists (ls), then run the following line which will create an empty config file.
>> touch config
Once the config file is created or you have a config file, time to make edits to it. Fire up vim by running:
>> vim id_rsa_sarn1.pub
Once you’re in vim, you’ll want to go into insert mode, so to this, hold down shift + i. Assuming your config file was empty, by the time your done with it you should have something that looks like this:
#Github – default – user1
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user1
#Github – sarn1
Host github-csrf
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_sarn1
A little info on what you’ll seeing above. There’s two configuration settings. The top half is the “default” Github setting mainly because when you’re cloning the repo you’re most likely be something like:
>> git@github.com:sarn1/urp.git
Where the user is git and the hostname is github and when I run something like that above, Github would use the id_rsa_user1 SSH file. That’s because it will look at this config file and see that the identify file id_rsa_user1 is tied to user git and host github.
The second configuration, what I’m doing is for user git and host github-csrf uses the id_rsa_sarn1 identify file.
Once you’re finishing making changes in the config file its time to get into exit mode of vim, save our changes and quit. To do that, hold down shift + q. Then type in wq.
You should then be navigated back to your SSH folder.
Run the following line to add an identity to the key.
>> ssh-add ~/.ssh/id_rsa_sarn1
Now its time to navigate to the folder you want your new project to be. In this example I was working on a CSRF library, so I created a new project folder and navigated to that directory. Here, either pull down or create a new Git repo. When you’re ready to add Github as your origin you would run something like:
>> git remote add origin git@github-csrf:sarn1/csrf.git
Troubleshooting:
Notice that this would trigger the id_rsa_sarn1 ssh keys. Then when you’re ready to push it should work. However, it didn’t work for me and took me awhile to figure this step out. The following may be optional if you have no issues pushing. When I was pushing, it was still pushing under my default account. I get something like this:
ERROR: Permission to sarn1/csrf.git denied to user1.
fatal: Could not read from remote repository.
When I ran:
>> git config user.name
It was still showing my other Github account handle, user1. So I ran:
>> git config user.name "sarn1"
And then ran:
>> git config user.email "[email protected]"
Where the email is the email associated to the sarn1 Github account. And then when I pushed, everything worked. I was able to push under the sarn1 user and not my other account. This part is a bit hazy for me and I’m trying to figure out what this is and why its working, but I didn’t find much information about this. It seems like for most people, you don’t have to do this.
Another thing to try is to rerun the identity.
>> cd ~/.ssh
>> ssh-add ~/.ssh/id_rsa_sarn1
In addition, it’s possible that things may resolve after rebooting your computer. And if all else fails, try removing all your keys and try the process again. I suspect some weird Git key caching issues may occur. You may want to explore what’s in the global default git config settings.
>> vim ~/.gitconfig
Some other highly recommended resources that help me during this journey…
- https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts–net-22574
- https://www.youtube.com/watch?v=fnSRBRiQIU8
- http://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer
- https://www.keybits.net/post/automatically-use-correct-ssh-key-for-remote-git-repo/