Post

Configuring multiple github accounts in Windows using SSH

Configuring multiple github accounts in Windows using SSH

Introduction

More than often a developer with will have atleast two GitHub accounts, one personal and other work. Let’s see how we can make the two accounts work side by side in windows.

First navigate to the folder

1
C:\Users\<user-name>\.ssh

Let’s check if we have any SSH keys already there. If there are no keys, the folder looks like

1
2
3
.ssh
├── known_hosts
└── known_hosts.old

Now lets go through generating the SSH Keys. I am configuring two SSH keys in this example

Account TypeGithub UsernameEmail ID
Personalnaveennaveen@gmail.com
Workcodeandcloudnaveen@codeand.cloud

Please note that we are using Ed25519 Keys for creating the SSH keys.

Generating SSH Keys

Here’s how we generate the key. Open the git bash and key in

1
ssh-keygen -t ed25519 -C "naveen@gmail.com"

It will then further prompt to enter the

  1. file to save the key
  2. passphrase

We will proceed by naming the key “id_personal” and passphrase empty to keep it simple.

The output looks like this after entering

1
2
3
4
5
6
7
8
9
~/.ssh$ ssh-keygen -t ed25519 -C "naveen@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/____/.ssh/id_ed25519): id_personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_personal
Your public key has been saved in id_personal.pub
The key fingerprint is:
SHA256:_______________________________ naveen@gmail.com

Create another key for work like,

1
2
3
4
5
6
7
8
9
~/.ssh$ ssh-keygen -t ed25519 -C "naveen@codeand.cloud"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/____/.ssh/id_ed25519): id_work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_work
Your public key has been saved in id_work.pub
The key fingerprint is:
SHA256:_______________________________ naveen@codeand.cloud

Adding SSH Keys to the Agent

We do that by keying in,

1
eval "$(ssh-agent -s)"

Breaking it down:

  1. ssh-agent -s

Starts the SSH agent (a background program that holds your private keys in memory so you don’t need to type the passphrase every time).

The -s option tells it to output environment variable setup commands in a format suitable for sh/bash (like export SSH_AUTH_SOCK=…; export SSH_AGENT_PID=…;).

  1. eval “…”

The eval command takes that output and executes it in your current shell.

This means the environment variables (SSH_AUTH_SOCK and SSH_AGENT_PID) get set up for your shell session.

Without eval, the agent would run, but your shell wouldn’t know where to find it.

Now add the identities to ssh like this

1
ssh-add id_personal

The terminal will be like this after adding both

1
2
3
4
5
6
7
8
9
~/.ssh$ eval "$(ssh-agent -s)"
Agent pid 3350

~/.ssh$ ssh-add id_personal
Identity added: id_personal (naveen@gmail.com)

~/.ssh$ ssh-add id_work
Identity added: id_work (naveen@codeand.cloud)

Adding the SSH Keys to Github

Copy the SSH keys and add them to GitHub using the cat command.

1
2
cat id_personal.pub
ssh-ed25519 ---------------------Pz naveen@gmail.com

Now login to github, go the settings and select SSH and GPG Keys and add the New SSH key

Save the key.

Configuring SSH for Multiple Accounts

create a config file in the .ssh folder and save it.

1
2
3
4
5
6
7
8
9
10
# Personal GitHub
Host github-personal
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_personal
# BetTech GitHub
Host github-work
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_work

Testing the SSH keys

We can test the SSH key using,

1
ssh -T git@github-personal

If successful, the terminal will show,

1
2
3
4
5
~/.ssh$ ssh -T git@github-personal
Hi naveen! You've successfully authenticated, but GitHub does not provide shell access.

~/.ssh$ ssh -T git@github-work
Hi codeandcloud! You've successfully authenticated, but GitHub does not provide shell access.

Using different accounts for different repositories

Now we can clone using the SSH way

Suppose we have a repository hello-world in the github account codeandcloud, we clone it as

1
2
# git clone git@github.com:codeandcloud/hello-world.git
git clone git@github-work:codeandcloud/hello-world.git

Please note that we replaced the git@github.com to git@github-work

That’s all to it. Happy coding.

This post is licensed under CC BY 4.0 by the author.