Wednesday, January 24, 2024

Using multiple SSH keys for multiple GitHub accounts

Why do you need multiple SSH Keys?

So you just started setting up your new work laptop. You figured out that your company uses GitHub for hosting its source code. Great!. But you also contribute to open-source that is also hosted on GitHub. A lot of organisations recommend creating a new GitHub account to access the repos hosted by the company.

Now GitHub doesn't allow you to use two different private keys on the same laptop. By default, the Git config is stored in .gitconfig directory. Every time you try to clone a repo which is private, Git client on your laptop uses the configuration defined in this directory to authenticate and authorise with GitHub. For security purposes, you should always keep the private keys separate for work related code and anything that you contribute on the open-source side. 

This blog aims to help you setup multiple SSH keys for different GitHub accounts on a single laptop


Setting up multiple SSH Keys

ssh-keygen -t rsa -b 4096 -C "personalemail@gmail.com"

Save the file as sanusatyadarshi


ssh-keygen -t rsa -b 4096 -C "work-email@work.com"

Save the file as sanu-work


Add both the public keys to respective GitHub accounts

vi /Users/sanu/.ssh/config


Add the following lines:

# Personal account

Host github.com-sanusatyadarshi

    HostName github.com

    User git

    IdentityFile ~/.ssh/sanusatyadarshi

    IdentitiesOnly yes

    

 # Work account # This will be the default account

 Host github.com

    HostName github.com

    User git

    IdentityFile ~/.ssh/sanu-work

    IdentitiesOnly yes


vi ~/.gitconfig

Add the following lines

[user]

        name = Sanu Satyadarshi

        email = work-email@work.com


For personal Repos

Change the hostName from github.com to github.com-sanusatyadarshi(whatever your put in config file)

git clone git@github.com-sanusatyadarshi:sanusatyadarshi/xyz.git


cd into the repo and run the following command

git config --local -e


Add this  line

[user]

    name = sanusatyadarshi

    email = personalemail@gmail.com

 



No comments:

Post a Comment

Making YouTube usable again

 YouTube has evolved from a platform for educational and entertaining content into a space filled with ads, distractions, and the ever-growi...