How to Manage Multiple SSH Keys
with Separate GitHub Accounts.

Time Square
0

In order to securely manage multiple SSH keys and authenticate them with GitHub, it's important to follow the correct steps. Having multiple SSH keys is useful when you're using different machines or need to access different GitHub accounts. Here's a step-by-step guide on managing multiple SSH keys and authenticating them with GitHub:

Video Tutorial for How to Add & Manage Multiple SSH Keys in GitHub
  1. Check for existing SSH Keys
    • Before you generate a new SSH key, you should check your local machine for existing keys, to list existing keys open Git Bash and run the following command line:
      ls -al ~/.ssh
    • This will list all files in the .ssh directory. Look for files named id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub). If you find these files, you already have an SSH key pair. Most of the time, the directory path will be: C:\Users\{username}\.ssh
      
                                          -rw-r--r-- 1 user 197121 419 Nov 29  2022 id_ed25519  // private key
                                          -rw-r--r-- 1 user 197121 106 Nov 29  2022 id_ed25519.pub  // public key
                                      
    • If your machine doesn't support Ed25519 algorithm, your keys might have been generated with RSA or ECDSA algorithm:
      • id_rsa.pub
      • id_ecdsa.pub
  2. Generate a new SSH Key Depending on which algorithm is supported by your local machine, you should generate a new key with your desired email, which can be different from the initial email used with the existing key.
    • In git bash, you need to navigate to where your .ssh directory is located.
      cd ~/.ssh
    • If Ed25519 is supported, run the command line below:
      ssh-keygen -t ed25519 -C "your_email@example.com"
    • If you are using a legacy system that doesn't support Ed25519 algorithm:
      ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    • You will be asked to save the key in a specific file; usually will be the same .ssh directory, think about giving the key a unique name to differentiate between various keys, workspaces, or GitHub accounts: $ Enter file in which to save the key(c/users/you/.ssh/id_ed25519: "unique-key-name")
    • The last step is to enter a passphrase of your choice, or press enter to leave it blank.
    • To ensure that the new key pair are generated successfully, run:
      ls -al ~/.ssh
    • You should be able to see the old & new keys as follows:
      
                                          -rw-r--r-- 1 user 197121 419 Nov 29  2022 id_ed25519  // OLD private key
                                          -rw-r--r-- 1 user 197121 106 Nov 29  2022 id_ed25519.pub  // OLD public key
                                          -rw-r--r-- 1 user 197121 419 Jul 23  2023 unique-key-name  // NEW private key
                                          -rw-r--r-- 1 user 197121 106 Jul 23  2023 unique-key-name.pub  // NEW public key
                                      
  3. Create a Config file If you have more than one pair of keys, you will have to create a config file in your .ssh directory to specify which key to use for each account, as the ssh-agent can't handle multiple keys:
    • Create a config file at .ssh directory path by running:
      notepad config
    • Copy, paste, customize, and save the file below in your config file.
    • Please note that thepersonal account is the previously existed account with id_ed25519 key, you can customize the account name to fit your needs.
    • While the work account the second account with newly generated key, you will need to distinguish Host by adding any word that stands out for you; I added work.
      
                                          #personal account
                                          Host github.com
                                              HostName github.com
                                              User git
                                              IdentityFile ~/.ssh/id_ed25519
                                              IdentitiesOnly yes
                                          #work account
                                          Host github.com-work
                                              HostName github.com
                                              User git
                                              IdentityFile ~/.ssh/unique-key-name
                                              IdentitiesOnly yes
                                      
    • Double-check the config is created properly by running:
      ls -al ~/.ssh
    • In case you find the config file saved as text file, run:
      mv config.txt config
  4. Add SSH keys to your GitHub account To authenticate your SSH keys, you need to add them to your GitHub account. Follow these steps:
    • Run this command below in git bash to display the public key, which you will then copy.
      cat 'unique-key-name'.pub
    • Go to GitHub and log in to the account associated with the SSH key you want to add.
    • Go to Settings > SSH and GPG keys.
    • Click on New SSH key.
    • Give your key a meaningful name.
    • Paste the contents into the key field on GitHub and click on Add SSH key to save.
  5. Time to test SSH keys Now that your SSH keys are set up and connected to your GitHub account, you can clone or update repositories without having to enter your credentials each time. Simply use the appropriate host alias and repository URL in your terminal. For example:
    • Clone a repository using the default SSH key
      git@github.com/username/repository.git
    • Clone a repository using the second SSH key
      git@github.com-work/username/repository.git
    • Note in the second cloned repository, we added the key word work to git@github.com-work which was customized in the config file to the Host.