Skip to content

Instantly share code, notes, and snippets.

@bonnopc
Created November 23, 2021 17:06
Show Gist options
  • Save bonnopc/c78920431284ce3fc2a5270016205116 to your computer and use it in GitHub Desktop.
Save bonnopc/c78920431284ce3fc2a5270016205116 to your computer and use it in GitHub Desktop.
Enable Multiple SSH Keys for MacOS/ Ubuntu/ Debian etc.

Enable Multiple SSH Keys for UNIX Based OS

Follow these steps below to enable multiple SSH keys on your device with UNIX Based OS (MacOS/ Ubuntu/ Debian etc.). Suppose, you have two different users/ accounts, one is personalAccount and another is companyAccount. And you have already a default key configured with personalAccount. (If you haven't set up your default ssh-key yet, please follow this article before going ahead with these steps described below.)

1. Generate another ssh-key

Generate a new ssh-key for your companyAccount.

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"

2. Clear previous cached keys

ssh-add -D

3. Add Keys

Then add your keys as following -

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_company

4. Check added Keys

You can always check your keys by entering following command

ssh-add -l

5. Create or Update config

You may have no config file right now. to create a config enter

touch config

Or maybe, you already have a config file in your ~/.ssh directory. Now you have to edit & save this file and include these lines as described below. (You can use nano config or open and edit with your favorite editor.)

Host personalAccount.github.com
HostName github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa

Host companyAccount.github.com
HostName github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_company

As you can see here, you'll be using two hosts as personalAccount.github.com & companyAccount.github.com.

6. Cloning a (Private) Repository

You can always use git clone [email protected]:someUserName/example-repo.git by using your default or personalAccount credentials. But you have to change the url while cloning from the companyAccount repositories.

Suppose we have a repo url of something like [email protected]:companyAccount/some-private-repo.git, then you have to change this url and clone this repo as following -

git clone git@companyAccount.github.com:companyAccount/some-private-repo.git

Note: the host name (companyAccount.github.com) should match with the Host described as in the config file.

@Steravy
Copy link

Steravy commented Apr 4, 2024

Thanks. Helped a lot.

@FloRobart
Copy link

thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment