DEV Community

Cover image for Simplifying GitHub Authentication: Adding and Managing Multiple SSH Keys 🤯
Haru Blank
Haru Blank

Posted on

Simplifying GitHub Authentication: Adding and Managing Multiple SSH Keys 🤯

Github provides us options to access private repo through HTTPS and SSH. I don’t know much detail and differences but basically, with HTTPS we have to use our Github username and personal access tokens while with SSH we use SSH key stored somewhere in you system for authentication.

This log is regarding SSH authentication and how we can keep multiple SSH key.

We will see how we can generate SSH and connect it with our Github. We will Also see how we can add multiple SSH key for multiple Github accounts.

Generating SSH Keys and Connecting to GitHub:

1. Generate SSH Keys:

- Let’s Open our terminal
- We can Generate SSH keys using the following command:
Enter fullscreen mode Exit fullscreen mode
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

ssh-keygen: This is the command-line utility used to generate SSH keys.

-t rsa: This option specifies the type of key to create. In this case, it specifies that we want to generate an RSA key. RSA is a widely used algorithm for public-key cryptography. Default file name file be id_ALGORITHM

-b 4096: This option specifies the number of bits in the key. In this case, it specifies that we want to generate a 4096-bit key. Generally, larger key sizes provide stronger security but may take longer to generate.

-C "your-email@example.com": This option allows US to add a comment to the key. The comment is typically used to identify the key and can be our email address or any other identifier we choose. Adding a comment is optional, but it can be helpful for managing multiple keys.

This will create a new SSH key, using the email as a label for SSH file.

> Generating public/private ALGORITHM key pair.
Enter fullscreen mode Exit fullscreen mode

We will be prompted to enter a file where we want to save our key.
By default the file name will be id_ALGORITHM , we can use any custom name and in fact we should use a custom name so we can use multiple SSH conveniently.

Enter file in which to save the key (/home/blank/.ssh/id_rsa):[Press enter]
Enter fullscreen mode Exit fullscreen mode

We can name our files like id_rsa_personal , id_rsa_opensource or any other name.
Once we enter our file name, it will create two files for us.
One with private key and another with .pub extension with same name which will be our public key.
These files will be generated inside ssh folder.
I am in Ubuntu WSL so mine is ~/.ssh. Your might be different so find yours.
These are just text files with SSK key inside them.
We add the public key with .pub extension to our Github for authentication.
And keep the private key private.
After creating a file, we have to type a secure passphrase at the next prompt.

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
Enter fullscreen mode Exit fullscreen mode

This will be used for authenticating our Github actions during push/pull and so on.

2. Adding our SSH key to the SSH-agent

Before adding SSH key to SSH-agent let’s make sure our agent is up and running.

blank@phoenix: ~$ eval "$(ssh-agent -s)”
Agent pid {processId}
Enter fullscreen mode Exit fullscreen mode

We will get the process id of the agent on success. Now we can add your SSH key to SSH-agent

We can add SSH file with the following command:

blank@phoenix: ~$ ssh-add path_to_private_ssh_key_file
Enter fullscreen mode Exit fullscreen mode
blank@phoenix: ~$ ssh-add ~/.ssh/id_rsa_personal
blank@phoenix: ~$ ssh-add ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

We will get a prompt to enter our passphrase. Let’s enter the passphrase and we’re good to go.
We can add multiple SSH files, we just have to make sure the file name are unique and don’t overwrite your SSH key and we add correct public key to our Github account.
Since we have multiple SSH key, we have to map our SSH keys with our Github account.
If we don’t map them then git will look for the default file while accessing our repo so it won’t be easy to use multiple Github account from SSH.

3. Let’s create a config to map our SSH key with Github

Now let’s create a config file inside our .ssh folder with our private and public keys.

# Personal account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

# Work account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

Host github.com-personal This is just an alias for our host. This alias name will be used in place of the actual hostname github.com when connecting with Github repo.

HostName github.com: This is the actual host name of the remote host which is (github.com )in our case. SSH looks for this host to connect.

User git: This line specifies the username that SSH should use when connecting to the remote host. In most cases with GitHub, the username is git. Github expects git to be there sorta thing.

IdentityFile ~/.ssh/our_ssh_key: This line specifies the path to the SSH private key file (id_rsa_personal) that should be used when connecting to the remote host using the defined alias (github.com-personal). This allows us to use a specific SSH key for specific Github account. We can map different SSH key to different account.

4. Add public SSH to Github

We have created a public and private SSH and added them to our SSH-agent. We also created a config file where we config which Host (alias for our Github account) will use which IdentifyFile. Now we need to add our public SSH key to our Github account.

To add public SSH key to our Github go to Github → Settings → under Access you’ll find SSH and GPG Keys.

Click New SSH Key button and add our SSH key from dentity-file.pub file.

We can read more on this here.

Adding a new SSH key to your GitHub account - GitHub Docs

We just have to make sure we add our personal SSH key to our personal Github account and work SSH key to our work account and so on.

  1. Now let’s update Remote URL in our git repositories By default the Github repositories’ SSH origin will be in the format

git@github.com:github-username/repository-name.git | (git@github.com/haruBlank00/baka-commerce.git)
We have to update our repositories URL so that git understands which SSH key to use for which Github repo.

  • For existing repositories, we have to update the remote URLs to use the configured aliases: as git@Host:username/repository.git Host is the config identifier we have on our config file (github.com-personal, github.com-work and username/repository.git is the actual git repo.
blank@phoenix: ~$ git remote set-url origin git@github.com-personal:username/repository.git
Enter fullscreen mode Exit fullscreen mode

We have to replace username/repository with our actual GitHub username and repository name.

For our previous URL , it will be git@github.com-personal:haruBlank00/baka-commerce.git

  • If we want to clone this repo then we can just change the default URL to this format and clone.
blank@phoenix: ~$ git clone git@Host/username/repository.git
blank@phoenix: ~$ git clone git@github.com-personal:haruBlank00/baka-commerce.git
Enter fullscreen mode Exit fullscreen mode

This way we can generate and add manage multiple SSH key in our system 😀
If you have any queries or want add something. Please leave a comment 💭.
Have a great day. 💞

Top comments (0)