Setting Up Multiple SSH Keys for GitHub on Linux
Table of contents
- Check for existing ssh keys
- Generate a new ssh key for a project
- Add the new ssh key to the ssh agent
- Setting up ssh config file
- Basic config structure
- Understanding ssh config options
- Copy the public key and add it to your github account.
- Navigate to SSH Keys Section
- Add Your SSH Key - This step is crucial , make sure no mistakes were made while copy pasting the keys.
- Test Your SSH Connection
- Clone the Repository
- References:
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:
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
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
User
Specifies the username for the SSH connection
For GitHub: Always use
User git
regardless of your GitHub usernameGitHub determines your identity through your SSH key, not the username
Example:
User git
IdentityFile
Full path to your SSH private key
Typically stored in the
~/.ssh/
directoryExample:
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:
Using the clipboard utility (xclip):
cat ~/.ssh/id_rsa_
ikafle.pub
| xclip -sel clip
Manually: Open the file
~/.ssh/id_rsa_
ikafle.pub
in any text editor and copy its contents.Copy from terminal: Copy the output
cat ~/.ssh/id_rsa_
ikafle.pub
Choose whichever method you prefer.
Open GitHub in Your Browser
Click on your profile picture in the top-right corner and select Settings
Navigate to SSH Keys Section
In the Settings sidebar, find SSH and GPG keys under "Access".
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.
Title: Give your key a descriptive title (e.g., “Project Name“, "Work Laptop" or "Home PC").
Key: Paste the SSH key you copied into the large text box.
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