gitssh keys

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?

Jul 10, 20262 min readUpdated Jul 10, 2026
Multiple ssh-key on your computer

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 the ssh-key for user trungntm
  • I also create a new one named developer1 - which will store the ssh-key for user developer1
  • Next step, you need to manual move your id_rsa and id_rsa.pub into 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_profile and 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

code
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-ssh command:
code
use-ssh trungntm

The command will be called and update the id_rsa and id_rsa.pub to main files in .ssh folder will similar to this:

code
trung.nguyen@trungtmnguyen ~ % use-ssh trungntm
πŸš€ πŸš€ πŸš€ Switching to ssh profile trungntm
Completed πŸ”₯ βœ…

Let's try to switch to developer1

code
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!