Configure multiple Github accounts on the same machine using SSH

Make your life a bit easier :)

·

6 min read

Hello there, everyone!

I'm sure you've run across a circumstance where you needed to use numerous GitHub accounts to get into a different project workflow environment.

It's possible that you have two GitHub accounts, one for work and the other for personal use. You just have one machine to work with, and you don't want to have to switch accounts every time you push your commit.

In this blog, I'll show you how to set up as many GitHub accounts as you like on your PC, or even move work to a separate machine.

Let's get started without further ado.

Prerequisites

  • Git basic commands (add, commit, push, pull, clone)
  • Github account access

Setup

To begin, go to your machine's user account folder and look for the ".ssh" folder. You can navigate to the home folder if you're a Linux user. If you're using Windows, navigate to SystemDrive C:/Users/username

You may need to toggle the option to show hidden files and folders to true, or you may need to create a folder called ".ssh".

ssh folder.png

You might come across anything similar to the image above.

Let's build a Public-Private Key pair now.

This is a relatively simple step. All you have to do now is run commands.

To get started, open the.ssh folder's terminal and type the following command, changing the email with your GitHub email -

$ ssh-keygen -t ed25519 -C "your_email@example.com"

It will then either prompt you to provide the key name or use a default name. I prefer to use a name that is significant and easy to understand, such as an account or project name.

After that, it will ask you to generate and confirm a pass. Having a pass is typically a good idea, but it shouldn't be too complicated, as it will be required for every action with the remote repository while executing push and pull.

In order to remain flexible with git operations, I personally skip passes by leaving them blank.

It will then create two files.

  • private key (the one without any extension)
  • public key (the one with .pub extension)

ssh-keygen.png

You'll see something similar to this.

Now open the public key in any text editor and copy its content.

Go to account settings after logging into your GitHub account. On the left-hand side, look for SSH and GPG keys.

SSH-GPG key page.png

As you can see, I've already added a few keys, which I normally name after the machine I'm using.

By choosing the " New SSH key " option, you can add your public key.

Note: You can only delete your key from this list; nothing else can be changed or edited.

Now we must create an ssh config file in order to specify our private key while connecting to GitHub via ssh.

Create a file with no extension called "config" inside the ".ssh" folder, and copy and paste this snippet into it.

Host github.com-{Whatever you feel feasible}
HostName github.com
User git
AddKeysToAgent yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/{Private key name}

Replace {Whatever you feel feasible} with a name that is easy to remember, like my GitHub account name, and replace {Private key name} with the private key name we created in the previous steps.

Host github.com-PrivateAccount
HostName github.com
User git
AddKeysToAgent yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/Project_GitHash

Exhausted? Drink a glass of water...

I understand that this is taking so long, but trust me and the process. It takes time for good things to happen. And we're about there.

I'm assuming you're familiar with the concept of a private repository and how to set one up.

Using ssh, look for a clone and copy the repository's path.

Take a look at the image below to see what I mean.

Screenshot from 2022-05-30 01-41-57.png

Now paste the following command into the terminal where you wish to clone the repository.

git clone git@github.com-PrivateAccount:PradyumnaGarg/Phonebook.git

Notice that after "github.com", I included the term I used in the config file; see the config section above for more information.

Then you're done.

Because the system doesn't recognise the new ssh key, it wants you to add it to the known host file, which you can do by simply putting "yes" into it.

Now, any push or pull actions you perform on this repository will be handled by the ssh configuration, which will push the changes to the remote repository.

If you want to connect a local git repository to a remote repository, all you have to do is add the remote repository URL to the specified host configured in ssh config.

git remote add origin git@github.com-PrivateAccount:PradyumnaGarg/Phonebook.git

It's as simple as that.

You'll get used to it as you repeat these steps more often. At first, I was puzzled as well.

Error Resolutions

At this point, I observed that some people received an error from GitHub stating that they did not have the proper access to the repository.

You can double-check that you followed all of the instructions exactly and that all of the file names were correct.

Finally, you can build an ssh-agent and attach your private key to it, then repeat the git operations.

Here are some commands to try out to assist you with this. These commands may need to be run in "Git bash" for Windows users. For Linux and Mac users, bash terminals will be enough.

eval "$(ssh-agent -s)"

Depending on your environment, you may need to use a different command. For example, you may need to use root access by running sudo -s -H before starting the ssh-agent, or you may need to use exec ssh-agent bash or exec ssh-agent zsh to run the ssh-agent.

ssh-add ~/.ssh/Project_GitHash

Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace Project_GitHash in the command with the name of your private key file.

Conclusion

As you've come this far, I'm assuming you know how to use ssh to set up numerous github accounts on the same machine and make your life a little easier.

On a daily basis, I communicate with 5-6 github accounts in order to work on numerous projects, and this has proven to be really beneficial.

It was also simple to set up a new machine, as I only needed to copy and paste my private files to the new system.

I also know of another ssh configuration use case, which is connecting to an EC2 instance and then using Visual Studio Code to connect to certain folders and code right into them.

It's really simple to set up; you can find countless articles on the subject online, or if you want, I can provide a basic guide like this one.

I hope you find this blog to be informative.

Have fun coding!

Did you find this article valuable?

Support Pradyumna Garg by becoming a sponsor. Any amount is appreciated!

Â