Let suppose I have two github accounts, https://github.com/rahul-office and https://github.com/rahul-personal. Now i want to setup my mac to easily talk to both the github accounts.
The setup can be done in 5 easy steps:
First make sure your current directory is your .ssh folder.
$ cd ~/.ssh
Syntax for generating unique ssh key for ann account is:
ssh-keygen -t rsa -C "your-email-address" -f "github-username"
here,
-C stands for comment to help identify your ssh key
-f stands for the file name where your ssh key get saved
ssh-keygen -t rsa -C "my_office_email@gmail.com" -f "github-rahul-office"
ssh-keygen -t rsa -C "my_personal_email@gmail.com" -f "github-rahul-personal"
Notice here rahul-office and rahul-work are the username of my github accounts corresponding to my_office_email@gmail.com and my_personal_email@gmail.com email ids respectively.
After entering the command the terminal will ask for passphrase, leave it empty and proceed.
Now after adding keys , in your .ssh folder, a public key and a private will get generated.
The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case github-rahul-office and github-rahu-personal)
Now we have the keys but it cannot be used until we add them to the SSH Agent.
ssh-add -K ~/.ssh/github-rahul-office
ssh-add -K ~/.ssh/github-rahul-personal
You can read more about adding keys to SSH Agent here.
For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.
For doing this we need to:
1. Copy the public key
We can copy the public key either by opening the github-rahul-office.pub file in vim and then copying the content of it.
vim ~/.ssh/github-rahul-office.pub
vim ~/.ssh/github-rahul-personal.pub
OR
We can directly copy the content of the public key file in the clipboard.
pbcopy < ~/.ssh/github-rahul-office.pub
pbcopy < ~/.ssh/github-rahul-personal.pub
2. Paste the public key on Github
- Sign in to Github Account
- Goto Settings > SSH and GPG keys > New SSH Key
- Paste your copied public key and give it a Title of your choice.
OR
- Sign in to Github
- Paste this link in your browser (https://github.com/settings/keys) or click here
- Click on New SSH Key and paste your copied key.
Following the instructions at https://dev.to/equiman/how-to-use-multiple-users-with-git-2e9l makes our life much easier as opposed to messing around with ssh configs.