Setting Up Multiple SSH Keys for GitHub on Linux

Managing multiple SSH Keys becomes crucial when:

  • You work with multiple GitHub accounts: personal and work accounts require separate ssh keys to maintain boundary between the projects.

  • You contribute to multiple projects hosted under different git organizations or Git hosting platforms (e.g., GitHub, GitLab, Bitbucket).

  • Without proper setup, you might inadvertently commit to the wrong repository.

Below is a detailed guide with command snippets :

Check for existing ssh keys

Before you proceed and create a new key, verify if you already have SSH keys on your system.

command : ls -sail ~/.ssh

If files like id_rsa or id_rsa.pub exist, those are your existing keys and you do not want to override them.

Generate a new ssh key for a project

command : ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_project_name

  • -t rsa: Specifies the RSA encryption algorithm.

  • -b 4096: Sets the key size (4096 bits recommended for better security).

  • -C "email": Adds an optional label (usually your email address).

  • -f: Defines the file name for the key pair.

You’ll be prompted to set a passphrase. you can simply press enter for default setup or a passphrase to add an extra layer of security.

Once completed, verify the newly generated ssh keys :
command : ls -al ~/.ssh/

For example, i have generated a ssh keys in the file named ikafle located inside the .ssh folder (file-path : ~/.ssh/id_rsa_ikafle and ~/.ssh/id_rsa_ikafle.pub).
id_rsa_ikafle : private file - you will not share this with anyone
id_rsa_ikafle(.pub) : public file - you will share the contents of this file with your github repository.

Add the new ssh key to the ssh agent

command: ssh-add ~/.ssh/id_rsa_project_name
in my case : ssh-add ~/.ssh/id_rsa_ikafle

if the ssh agent isn’t running in the current shell session, start the ssh agent manually as:
command: eval "$(ssh-agent -s)"
This will output its process ID :

you can configure your shell to automatically start the ssh agent by adding the following script to your shell configuration file. If you use the bash shell like me, add the following to your .bashrc file and source it as : source ~/.bashrc

if ! pgrep -u "$USER" ssh-agent > /dev/null; then
    eval "$(ssh-agent -s)"
fi

Setting up ssh config file

The SSH config file allows you to define specific settings for different hosts, including which SSH key to use for each connection:

Open your SSH config file using your preferred text editor:

Basic config structure

Here's how to structure your SSH config file:

# Project 1
Host project1.github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_ikafle

# Project 2
Host project2.github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_project2

Understanding ssh config options

Let's break down the essential configuration options in detail:

  1. Host

    • Acts as a nickname or alias for your connection

    • Can be any name you choose (e.g., github-personal, github-work)

    • Used when cloning repositories via SSH

    • Example: Host github-work

  2. HostName

    • The actual server address you're connecting to

    • Can be a domain name or IP address

    • For GitHub, this will be github.com

    • Example: HostName github.com

  3. User

    • Specifies the username for the SSH connection

    • For GitHub: Always use User git regardless of your GitHub username

    • GitHub determines your identity through your SSH key, not the username

    • Example: User git

  4. IdentityFile

    • Full path to your SSH private key

    • Typically stored in the ~/.ssh/ directory

    • Example: IdentityFile ~/.ssh/id_rsa_work

With our configuration file ready, let's proceed to set up the keys in GitHub.

Copy the public key and add it to your github account.

To copy your SSH public key, you can either:

  1. Using the clipboard utility (xclip): cat ~/.ssh/id_rsa_ikafle.pub | xclip -sel clip

  2. Manually: Open the file ~/.ssh/id_rsa_ikafle.pub in any text editor and copy its contents.

  3. Copy from terminal: Copy the output cat ~/.ssh/id_rsa_ikafle.pub

Choose whichever method you prefer.

Open GitHub in Your Browser

  1. Log in to your GitHub account.

  2. Click on your profile picture in the top-right corner and select Settings

Navigate to SSH Keys Section

  1. In the Settings sidebar, find SSH and GPG keys under "Access".

  2. Click the green New SSH key button.

Add Your SSH Key - This step is crucial , make sure no mistakes were made while copy pasting the keys.

  1. Title: Give your key a descriptive title (e.g., “Project Name“, "Work Laptop" or "Home PC").

  2. Key: Paste the SSH key you copied into the large text box.

  3. Click Add SSH key.

Test Your SSH Connection

Use your configured SSH alias to test the connection: ssh -T project1.github.com
A successful connection will show a message like:

Clone the Repository

The standard Git clone command uses this format: git clone git@github.com:username/repository.git
However, since we've configured a custom SSH host alias, modify the command to use your alias instead of github.com: git clone git@project1.github.com:username/repository.git

This uses your SSH configuration to properly authenticate while cloning the repository.

You’re all set to clone your repository. Happy coding!

References:

Github - Adding a new SSH key to your GitHub account

Stackoverflow - Setting up multiple SSH key access to Github