DEV Community

MD RAHIM IQBAL
MD RAHIM IQBAL

Posted on

Managing Multiple GitHub Accounts with SSH Keys on Your Device

Introduction

Managing different GitHub accounts is a common scenario for many developers. Perhaps you maintain a personal GitHub account to showcase your school projects and a work account for your professional work. To switch between these accounts easily and securely on the same computer, you can use SSH keys. These keys serve as a secure method of authentication, much like a physical key can be used to lock or unlock a specific door.

Secure Shell (SSH) is a cryptographic protocol enabling secure remote access to servers. Widely used by developers and system administrators, SSH authenticates two parties (client and server) and encrypts data that passes between them using a pair of cryptographic keys: a public key for distribution and a private one kept secret.
You can generate your SSH keys using the ssh-keygen command:

ssh-keygen -t ed25519

This command creates a new set of keys using the ED25519 algorithm. You can specify where to save the keys and whether to use a passphrase for added security.
After generating your keys, distribute your public key to any system you need to access. When initiating an SSH session, the server uses the public key to encrypt messages in a way that can only be decrypted with the private key. The client uses the private key to authenticate its identity to the server.

Step 1: Generating SSH Keys

Generating SSH keys for each GitHub account involves creating a pair of keys – one private and one public – for each account. The private key is kept secret on your computer, and the public key is added to your GitHub account.

Open Terminal on your device and use the following commands to generate SSH keys for your personal and work GitHub accounts:

For your personal account:

ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/github-personal
Enter fullscreen mode Exit fullscreen mode

And for your work account:

ssh-keygen -t ed25519 -C "your_work_email@example.com" -f ~/.ssh/github-work
Enter fullscreen mode Exit fullscreen mode

The -t flag specifies the type of key to create, ed25519 in this case. The -C flag lets you label the key with a comment, typically the associated email address. Finally, the -f flag lets you specify the filename of the key.

Step 2: Add SSH keys to SSH Agent

Now we have the keys but it cannot be used until we add them to the SSH Agent.

ssh-add -K ~/.ssh/github-personal
ssh-add -K ~/.ssh/github-work
Enter fullscreen mode Exit fullscreen mode

Step 3: Adding SSH Keys to GitHub Accounts

The public keys (not the private ones!) generated in Step 1 need to be added to the respective GitHub accounts. This process is a way to introducing GitHub to your magic box and telling it that messages from these keys should go to your respective accounts.

To display the contents of your public key, run:

nano ~/.ssh/id_rsa_personal.pub
Enter fullscreen mode Exit fullscreen mode
  • Copy the content and exit using ctrl+X

  • Go to personal GitHub account, click on your avatar > Settings > SSH and GPG keys > New SSH key.

  • Paste the key, give it a name and save it.

Repeat the process for your work account using the id_rsa_work.pub key.

Step 4: Configuring SSH for Different Accounts

With the SSH keys added to the respective GitHub accounts, we now need to create an SSH configuration file to map each key to the correct account.

Create and open a new config file in .ssh diectory by running:

touch ~/.ssh/config
nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

And then add the following configurations:

# Work account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

# Personal account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode

Save and exit the file (ctrl + X, then Y, then enter). SSH now knows which key to use for which GitHub account.

Step 5: Using the Configurations in Practice

With everything set up, you can now use these configurations to clone repositories, make commits, and push changes using each GitHub account.

For instance, if you wanted to clone a repository from your personal account, you would use:

git clone git@github.com-personal:username/repo.git
Enter fullscreen mode Exit fullscreen mode

And to clone a repository from your work account:

git clone git@github.com-work:username/repo.git
Enter fullscreen mode Exit fullscreen mode

Replace username and repo with your GitHub username and repository name, respectively.

Step 6: Git Local & Global Configurations

The next step involves telling Git who we are when we commit changes using these accounts. We do this by setting our name and email for each repositories. If you want to set this config for a specific repository only, navigate to that repository in your terminal and execute the below commands.

For the repositories have access with personal account:

git config user.name "Your Name"
git config user.email "your_personal_email@example.com"
Enter fullscreen mode Exit fullscreen mode

And for the repositories have access with work account:

git config user.name "Your Work Name"
git config user.email "your_work_email@example.com"
Enter fullscreen mode Exit fullscreen mode

These commands set your Git username and email respectively for a specific repository only. If you want same config globally(for all repositories) then the --global flag applies these settings across all repositories.

git config --global user.name "Your Name"
git config --global user.email "your_personal_email@example.com"
Enter fullscreen mode Exit fullscreen mode

To push or pull to the correct account from the existing cloned repositories we need to add the remote origin to the project

//for repositories associated with personal account
git remote add origin git@github.com-personal:username/repo.git

//for repositories associated with work account    
git remote add origin git@github.com-work:username/repo.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

Managing multiple GitHub accounts on a single machine might seem complicated, but with the help of SSH keys and some simple Git configurations, you can easily switch between your accounts. This is a powerful and secure way to maintain separate identities for personal and professional projects. So go forth and code, knowing that you have a robust and flexible system for managing your GitHub accounts!

If you'd like, let's connect on LinkedIn and grow our community bigger.

Top comments (0)