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.)
Generate a new ssh-key for your companyAccount.
cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"ssh-add -DThen add your keys as following -
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_companyYou can always check your keys by entering following command
ssh-add -lYou may have no config file right now. to create a config enter
touch configOr 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_companyAs you can see here, you'll be using two hosts as personalAccount.github.com & companyAccount.github.com.
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.gitNote: the host name (companyAccount.github.com) should match with the Host described as in the config file.