Multiple ssh-key on your computer
In real world, you may have a multiple Git account for multiple Git repo. So, how to setup multiple your ssh-keys in your computer and switch between them easily?

If you do not have any idea to generate the ssh-key, please refer to creating-ssh-key.
Introduction
In the case I have multiple ssh-key and would like to use them for specified Git repo. In stead of backup them and manual update .ssh directory, I prefer to have an automation shell script to help with that.
Get started
Take a look at your .ssh directory.
- I will create a new directory named
trungntm- which will store thessh-keyfor user trungntm - I also create a new one named
developer1- which will store thessh-keyfor user developer1 - Next step, you need to manual move your
id_rsaandid_rsa.pubinto new folders. Note: you should check correct files before moving them to ensure right files will be used - Okay, at this step, you've already have the enough files into your new folders. Let's open your
.bash_profileand update it as below:
cd ~
vi .bash_profile
Update the shell function into your .bash_profile. Don't forget to update your user directory on your machine
function use-ssh() {
echo "π π π Switching to ssh profile" $1
cp /Users/trung.nguyen/.ssh/$1/id_rsa.$1 /Users/trung.nguyen/.ssh/id_rsa
cp /Users/trung.nguyen/.ssh/$1/id_rsa.pub.$1 /Users/trung.nguyen/.ssh/id_rsa.pub
echo "Completed π₯ β
"
}- Saving them, and open new Terminal and trying new
use-sshcommand:
use-ssh trungntmThe command will be called and update the id_rsa and id_rsa.pub to main files in .ssh folder will similar to this:
trung.nguyen@trungtmnguyen ~ % use-ssh trungntm
π π π Switching to ssh profile trungntm
Completed π₯ β
Let's try to switch to developer1
trung.nguyen@trungtmnguyen ~ % use-ssh developer1
π π π Switching to ssh profile developer1
Completed π₯ β
You're done. Now, you can create a multiple profiles and switch between them easily via shell command.
Happy coding!