Published on
· 2 min read

Multiple ssh-key on your computer

Authors
  • avatar
    Name
    Nguyễn Tạ Minh Trung
    Twitter

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

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:
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:

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!