gitssh keys

How to Configure Multiple SSH Keys on macOS

In practice, you may use multiple Git accounts for different repositories. This guide explains how to configure multiple SSH keys on macOS and select the correct key for each GitHub, GitLab or Bitbucket account.

Jul 10, 20262 min readUpdated Jul 17, 2026
How to Configure Multiple SSH Keys on macOS

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!

Comments

0/2000

Loading comments…

Trung Nguyen

Trung Nguyen

Software engineer building simple, reliable systems. I write about architecture, Java, and things I learn along the way.

More about me