Keep Work and Home Separate with Multiple SSH Keys

Sometimes you are blessed with a work laptop that is also your personal laptop. It is also common to have a work-related Github account and a personal account. So how do you manage to alternate between multiple accounts so that work and personal projects are separated? It will involve credentials called SSH keys. But, first, we need to explore the whys, and then the hows.

Why multiple separate accounts?

If you are creating a SaaS product (software as a product) as a side gig or are developing a separate business, it’s important to ensure you are not using anything company-related or you could run into uncomfortable legalities in the future. As another example, you could be working on a personal project with other friends outside of your company. If you are sharing access to company-subscribed products or sharing a repository with your work account, that could also lead to a breach of security or an NDA you might have signed. Overall, if company policy is very strict, then it’s best to keep personal projects in your personal account. So how do you manage separate accounts then?

Step 1: Check your SSH keys.

Though there are other hosting platforms, I will use Github as an example. First, check your GitHub account settings and see if you’ve already generated SSH keys for both your work and personal GitHub accounts. If not, follow the steps in GitHub’s documentation.

Just a reminder, make sure to:

  1. Generate SSH Key
  2. Add to .ssh/config
  3. Run ssh-add --apple-use-keychain ~/.ssh/id_ed25519 in your terminal
  4. Test your SSH key terminal connection with your Github accounts by running ssh -T [email protected] or ssh -T git@<hostname>
  5. Make sure your public key is stored in your Github account

NOTE: You can find your hostname in your SSH Config file.

Step 2: Modify the hostnames.

If you haven’t already, open your config file so you can modify the hostname for each key to your preference.

open ~/.ssh/config

Inside this file, you will probably see something like this:


Host bitbucket.org AddKeysToAgent yes IdentityFile ~/.ssh/id_ed25519 Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed30000 Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed20000

You will want to update your hostname to a new name so that your personal GitHub, Bitbucket, or any other hosting platform will clone under a specific SSH key. This is important because if you kept your Hostname as “github.com” for all your GitHub accounts, you wouldn’t be able to tell which key you were cloning a repository with. Finally, for better clarity, you can also rename your each public and private key in your .ssh directory so that each key is clearly distinguishable.

Modified Config File (Sample Setup)


Host bitbucket.org AddKeysToAgent yes IdentityFile ~/.ssh/bitbucketWork Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/gitWork IdentitiesOnly yes Host gitHub.com-personal HostName gitHub.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/gitPersonal IdentitiesOnly yes

There are two things you should notice.

  1. I originally hadIdentityFile ~/.ssh/id_ed25519 , but now I’ve renamed my SSH public and private key in .ssh directory to bitBucketWork. So now I have IdentityFile ~/.ssh/bitbucketWork and I have done the same for the other keys.
  2. Host gitHub.com-personal – the Hostname for my personal GitHub SSH key is now gitHub.com-personal, whereas my work GitHub SSH key is still github.com

Result!

When I clone a repository, it will now look like this:

This will be a bit inconvenient since many developers are used to just clicking copy to clone with an SSH in a repository. But, after a few times, this can become habitual! Of course, if your company is not as strict, then all of this is not necessary. If it is, though, a slight modification before you clone will definitely be worth avoiding a potential headache.

 
Conversation

Join the conversation

Your email address will not be published. Required fields are marked *