Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adrian-taralunga/8666361fd73c2e53c4481004b36be17c to your computer and use it in GitHub Desktop.
Save adrian-taralunga/8666361fd73c2e53c4481004b36be17c to your computer and use it in GitHub Desktop.
Multiple SSH Keys settings for different github account

Managing Multiple SSH Keys for Different GitHub Accounts

This gist outlines how to configure multiple SSH keys for different GitHub accounts on a single machine.

1. Generate SSH Keys

Generate two SSH key pairs, one for each GitHub account, ensuring you specify a unique file path for each.  Replace the example emails with your actual emails.

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -C "[email protected]"  
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "[email protected]"  

 

This will create two key pairs:

  • ~/.ssh/id_rsa_work (and its public key ~/.ssh/id_rsa_work.pub) for your work account.
  • ~/.ssh/id_rsa (and its public key ~/.ssh/id_rsa.pub) for your personal account.

2. Add SSH Keys to the SSH Agent

First, clear any existing keys from the agent:

$ ssh-add -D

Then, add your new keys:

$ ssh-add ~/.ssh/id_rsa_work

$ ssh-add ~/.ssh/id_rsa

Verify the keys are added:

$ ssh-add -l

3. Configure SSH Config File

Edit (or create) your SSH config file:

$ nano ~/.ssh/config

Add the following configuration, replacing placeholders with your actual GitHub username:

#Work GitHub account

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#Work GitHub account

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

4. Clone Repositories and Configure Git

Personal Repository:

Clone your personal repositories as usual:

$ git clone [email protected]:your_personal_username/repo_name.git

Work Repository:

Use the github.com-work hostname defined in your SSH config:

$ git clone [email protected]:your_work_username/repo_name.git

5. Configure Git User Settings

Global Config (for personal account):

$ git config --global user.name "Your Personal Name"

$ git config --global user.email "[email protected]"

Work Repository Specific Config:

Navigate to the work repository's directory:

$ cd project-name

Then set the user settings:

$ git config user.name "Your Work Name"

$ git config user.email "[email protected]"

6. Push and Pull

You should now be able to push and pull to both your personal and work repositories without any SSH key conflicts. Remember to use the correct remote URL (github.com or github.com-work) depending on the repository.

7. Add Public Keys to GitHub

Finally, add the contents of the public key files (~/.ssh/id_rsa.pub and ~/.ssh/id_rsa_work.pub) to your respective GitHub accounts under "Settings" -> "SSH and GPG keys". You can copy the contents of the public keys using 

$ cat ~/.ssh/id_rsa.pub 

$ cat ~/.ssh/id_rsa_work.pub.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment