How to follow this guide
- Don't type or run anything until I told you so.
- This guide uses linux commands. For windows users, you can use
I have one computer and two different github accounts. One is for work, the other is for my personal stuff. I can't use the same ssh key twice, so I have to use different ssh key for each of my accounts. How do I do that? How do I switch between these ssh keys?
Generate SSH keys
Open your terminal / CMD PROMPT and type the following command:
ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org"
The command will ask ssh to generate a key for you. After running the command, you will see the following feedback:
Generating public/private rsa key pair.
The next line you will see would be:
Enter file in which to save the key (/Users/sprlwrks/.ssh/id_rsa):
Here, you can specify a directory and filename for the ssh key that will be generated. The default is
/Users/sprlwrks/.ssh/id_rsa. It will be saved in
/Users/sprlwrks/.ssh/ with the file name
It will generate two files.
id_rsa.pub contains your public key which you will use, you can give this to your team leader or to other people that you want. The
id_rsa is the private key, don't want to give this key to anyone.
In this case, since we are going to generate two ssh keys, we don't want to keep the default file name, set it to whatever name you want by giving it
/Users/sprlwrks/.ssh/file_name. I named mine
The next line you will see would be:
Enter passphrase (empty for no passphrase):
If you type a passphrase here, you will have to remember that and type the same passphrase again everytime you use this key. I'll leave it up to you to decide. For me, I did not add any passphrase so I simply pressed enter. The next line would ask you to retype the passphrase again, of course if you left it empty then just press enter.
With all that you should have gotten something that looks like this:
aprilmintacpineda-MacBook-Pro:PWA-W88 aprilmintacpineda$ ssh-keygen -t rsa -b 4096 -C "email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/sprlwrks/.ssh/id_rsa): /Users/sprlwrks/.ssh/id_rsa_personal Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/sprlwrks/.ssh/id_rsa_personal. Your public key has been saved in /Users/sprlwrks/.ssh/id_rsa_personal.pub. The key fingerprint is: 0b:0a:9f:58:2d:c8:a3:87:f7:44:17:f6:2c:d8:b7:3a firstname.lastname@example.org The key's randomart image is: +--[ RSA 4096]----+ | | | | | o | | . . = + | | = = * S | | o B = + o | |o + = o | | o o E. | | . .. | +-----------------+
That's your first key. Now run
cd ~/.ssh and then run
ls would list all files in the directory. You should see the keys that have been generated.
Now try to generate another one by following the same procedure again. I named my second key
id_rsa_work. Once you're done you should have the following files (respective to the filename you gave it):
id_rsa_personal id_rsa_personal.pub id_rsa_work id_rsa_work.pub
Adding ssh keys to github accounts (or whatever you use)
First, I'll add my
id_rsa_personal ssh key.
Go to your github account then go to
settings -> SSH and GPG keys. Click on
New SSH key button.
Go to your terminal again and run this command:
cat ~/.ssh/<yourfilename>.pub replacing
<yourfilename> with whatever file name you gave it, in this case mine is
After running the commands above, you'll see something like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCha2J5mW3i3BgtZ25/FOsxywpLVkx1RgmZunIACBxV5V1lUm9I6J8uP8sP4xst/WwTWzjUY8svey1FRSNghOwtZvYZyD7lEy4FCdTn3InbRq4xXHNSVEdpG0Bbr1MEr/QWin/Q87oabQZo3wyRRJ3KjsO9xCDcwH8xcxUM+I4f3b5zSulpArjBsjkkkMsXih7L35FtzrW241mKjoA5g9/cfbqF7F6Gqwi23eUMCxwbEjgsUdFAKNbSmf9b4b7dAmfwjljM23m6FYGN75r72RH5bSuSoPdNfRwbqtHmvY0dPjcsnBRAkVokNPnvUXx4FMe7ra7T2vFOjfuyrHNVNi82TiOKRbtSxzReyNUOtpAukw833iy0hLyDy/Oo4/9aFXQEg4QSFdb/cZcTUdKE2XWIlstGApXgoy19VfJHJLjFZ7QRQQbd+8l53bZ0vUqpWINpf1UwpgKLLuCKWMqXUlaTfWSTLS+7LAM9PhsSD8FmzwFMJiSlC6ZYmT5W8c74SxRrw0EB8u/UA2UdWaOB7bj66aydOt8i+tu5HhB5eNQGDUh/19seW+48f6Qf5kcwcO/KlEn9hujJbCZA8owbzyT0KFjo5slnvEtH46EKGlaWOrRk4nuDoi3nF87TkLB8yedVxki2dvh8dmTLgGU0Dzb8NpZ+6gq3X+xnXqo92989tQ== email@example.com
Copy paste that to the key on your
new ssh key form then give it whatever title you like. I gave mine
Done! Now you have added your first ssh key to your github account, now add the other one to another github account or to the same github account giving it a different title so you can differentiate between the two.
On your terminal / CMD PROMPT, run this command:
touch ~/.ssh/config this will create a file with the file name of
config on the
~/.ssh folder. Now Go to that folder and open that file with your text editor of choice.
Copy paste the following on it:
Host gh_work HostName github.com IdentityFile ~/.ssh/id_rsa_personal
Host is how you would referrence this credentials on your terminal. The
HostName is whatever platform you use, in this case
IdentityFile is the
ssh key to be used for this credential.
Now add another one. By the end you should have something like these:
Host gh_work HostName github.com IdentityFile ~/.ssh/id_rsa_work Host gh_personal HostName github.com IdentityFile ~/.ssh/id_rsa_personal
On your terminal / CMD PROMPT, run
ssh -T git@<myHost> replacing
myHost with the host you wrote on the
config file. Mine is
ssh -T git@id_rsa_personal.
After running that command, you should see something like this:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.. Now you know it's working. Go ahead and test the other key you added.
Cloning repository using a specific ssh key
For this example I will clone this repository: https://github.com/aprilmintacpineda/chat-with-people-backend. To test if yours keys work, you should your own test repository.
git clone git@<myHost>:aprilmintacpineda/chat-with-people-backend.git. Replacing myHost with the
Host you want that was specified on your
config. Ones it's done, you can run
git remote -v and you'll see something like this:
origin git@gh_personal:aprilmintacpineda/chat-with-people-backend.git (fetch) origin git@gh_personal:aprilmintacpineda/chat-with-people-backend.git (push)
You see now that it would use my
gh_personal keys everytime. Now make changes and commit and push the changes. If all worked well you should get no errors.
Note that if you already have an existing clone in your machine, you can do this:
git remote add origin git@<myHost>:aprilmintacpineda/chat-with-people-backend.git
then simply push to that remote.
Now you're all set, you can test the other key too!
Help me improve this guide by adding comments
Edits and corrections
- Point out mistakes (even grammatical mistakes).
- Suggest edits.
Tell me about it
- Did I missed something?
- Experiencing an error?
Created with <3 by April Mintac Pineda
ssh -t git@[host] works however when I try to fetch from my remote url git@[host]:[remote username]/[repo name].git it says
Unable to open connection:
Host does not existfatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Any idea why this is happening?
edit: I have my ssh config file setup, added the keys using ssh-add and ssh -T git@[host] works however any git command with the correct remote url does not