DEV Community

Cover image for Utilizing GitHub accounts on Ubuntu with multiple SSH keys
emmanuel-nullpod for Nullpod

Posted on

Utilizing GitHub accounts on Ubuntu with multiple SSH keys

In the dynamic realm of software development, it's not uncommon for developers to find themselves juggling multiple GitHub accounts—one for work and another for personal projects. Navigating this dual identity on Ubuntu/Mac requires a thoughtful approach, particularly when it comes to managing SSH keys. This article serves as a practical guide to seamlessly use two or more GitHub accounts on Ubuntu/Mac by employing multiple SSH keys.

The Dilemma

Managing work and personal GitHub accounts simultaneously poses a unique challenge. A key aspect of addressing this challenge is the need for separate SSH keys, ensuring secure and efficient authentication for each account.

Understanding SSH Keys

SSH keys play a pivotal role in secure authentication. By utilizing distinct keys for different GitHub accounts, developers can maintain a clear separation between their professional and personal coding endeavours.

Generating SSH Keys for Each GitHub Account

Work Account(example):

Open the terminal and generate an SSH key using:

ssh-keygen -t rsa -b 4096 -C "your-email@work.com"

Save the key to an appropriate directory. By default it is in the “~/.ssh” directory you can keep it that way.

Image description
If you want to give a specific name to your key, it can be changed by renaming the file in which you want the key to be saved in. Renaming of keys can be used to properly identify different keys you saved.

Personal Account:

Repeat the process with a different email for your personal GitHub account.

Configuring SSH Config File

To manage multiple SSH keys effectively, edit your SSH config file. Add the following lines:
Once the Keys properly generated, they should be found in the “~/.ssh” directory.

Image description

If all the key generated are available, then head to your different GitHub accounts on GitHub.com

Head to the your profile settings and select “SSH and GPG keys” and click “New SSH key”

Image description

You need to add you generated public key that can be obtained from reading the public keys generated. With the command

cat ~/.ssh/<your_key_name>.pub
Enter fullscreen mode Exit fullscreen mode

Copy and paste the result from the public key in the Key text area provided on your GitHub and click “Add SSH key”.
Repeat the step for both GitHub accounts and any accounts you wish to add this way.

Bash Code

To write the bash code go to the “.shh” folder and create a file to create a “config” text file using vim to properly separate the different public keys.

  1. Open your terminal
  2. Go to the SSH folder using cd ~/.ssh.
  3. Create a config file using the command “vim config”.
  4. Insert the correct text below in the config file by replacing the data with your private data.
  5. Check that the content of the config file is properly inserted using cat ~/.ssh/config

Image description

Personal account

Host github.com-personal
            HostName github.com
            IdentityFile ~/.ssh/<Your private key>
Enter fullscreen mode Exit fullscreen mode

Work account

Host github.com-work
            HostName github.com
        IdentityFile ~/.ssh/<Your work key>
Enter fullscreen mode Exit fullscreen mode

Bash Testing

Testing the Configuration

Work account

ssh -T git@github.com-work
Enter fullscreen mode Exit fullscreen mode

Personal account

ssh -T git@github.com-personal
Enter fullscreen mode Exit fullscreen mode

While testing you might be prompted to enter a password to unlock the private key. Enter the password given to the key that you generate in the steps above to continue with the operation.

Image description
Here is what should be seen when the keys are properly distributed.

Image description

Adding SSH Keys to the Authentication Agent

After configuring multiple SSH keys and testing the setup, you can enhance the workflow by using the ssh-add command. This command adds private keys to the SSH authentication agent, eliminating the need to enter the passphrase every time you use the keys.

Open a terminal and run the following commands for each key:

Image description

Note: You can then list all the ssh-keys information to verify that your keys are well implemented. By doing the command

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Organizing Workspaces

Consider organizing your workspaces effectively, keeping work and personal projects in separate directories. This helps maintain a clear distinction between the two.

Updating Git Configurations

Update your Git configurations to align with the new SSH key setup. Ensure consistency across all relevant settings for a smooth experience.

Conclusion: A Seamless Workflow

By following these steps, you've successfully configured multiple SSH keys for your dual GitHub accounts on Ubuntu/Mac. This setup ensures a harmonious balance between your professional and personal coding pursuits, enhancing both security and workflow.

Top comments (1)

Collapse
 
bojangles profile image
Bojangles

this article falls short. on newish versions of git you can use includeif to select which key to use based on directory.

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig.work

[includeIf "gitdir:~/code/"]
    path = ~/.gitconfig.personal
Enter fullscreen mode Exit fullscreen mode

then make a .gitconfig.personal file

[core]
sshCommand = "ssh -i ~/ssh/keys/personal"

[user]
email = joe@website.com
name = Joe 
Enter fullscreen mode Exit fullscreen mode