Skip to content

Instantly share code, notes, and snippets.

@crecabar
Forked from bonnopc/multipleSSHkeysForUnix.md
Created January 26, 2023 13:14
Show Gist options
  • Save crecabar/e33614e18e116895e45badfd824ef0e1 to your computer and use it in GitHub Desktop.
Save crecabar/e33614e18e116895e45badfd824ef0e1 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.

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