GitHub – Setup account using SSH keys

GitHub provides support of SSH keys and configure an authentication agent so that you won’t have to re-enter your GitHub username and password. Many developers must have face problems with authenticating their self to GitHub using standard username and password mechanism specially when working with multiple GitHub accounts.

  • Developers need to remember and enter username and password of GitHub account on each and every interaction with GitHub through Git.This is tedious task for many lazy developers. 😛
  • Every Developers must be working with multiple repositories. It can be personal/private repositories or it can be multiple project repositories of same client or it can be multiple repositories of multiple clients. Developer needs to remember username and passwords of each different accounts. There are many chances of Author conflicts among repositories .For example,  you are working with two repositories personal Repo1 having username User1 & professional Repo2 having username User2. When you are working with Repo1 then chances are high that when you switch to Repo2 and commit anything then it still display User1 as Author and vice versa. It even push code with User1 in Repo2 if proper access is not configure for Repositories.

You don’t need to worry about all those problems which I have faced and lately used SSH keys which had made my life username/password free. You need to configure SSH keys for every different GitHub accounts only once and then you are all set.

You need to configure it by following steps for every GitHub account. This guide is for Windows user but more or less it is same for other environments.

1. Generate SSH public and private key for GitHub account

Let say, you are working with repository https://github.com/User1/Repo1 and email is User1@domain.com. Generate public and private key by running following command.

ssh-keygen -t rsa -b 4096 -C “User1@domain.com”

If you get error ‘ssh-keygen’ is not recognized as an internal or external command,operable program or batch file just google it out and make it run.

This command will prompt you to enter file name and passphrase. Enter user1 as filename and any passphrase to add security layer to SSH keys. It will generate two files user1(private key) and user1.pub(public key).

2. Create SSH config file

You need to first create SSH config file config at USER_HOME/.ssh/config if this file does not exist. Then, add entry of private key generated in Step1

Host github-User1
HostName github.com
User git
IdentityFile USER_HOME\.ssh\user1
IdentitiesOnly yes

Host : You can configure any name to host. This needs to be used when connecting GITHUB using GIT over SSH.

HostName : github.com

User : git

IdentityFile : Location of private file generated.

3. Setup public key to GitHub account

This step will add public keys generated to User1‘s GitHub account. Login to GitHub using User1@domain.com for which you have generated SSH keys.

Go to Setting > SSH and GPG keys section and Add new SSH key by providing any title and copy the content of user1.pub which is generated in Step1. Public key content may look like following. Copy full text without leaving anything.

ssh-rsa AAAAB3NzaC1yc2EAAAAlX66kDOAl2kQUAAcKrhycnrYuXuHnYXRNGlmglVtkkP2KqHxi3puXexRNjuf/99AdTKen6sUklpElgWUaWYgFdL9qDIY4Zg83ggIcOxsK/Dw2qsUepRyMponSIhUOWnkmOhk1rhHqWemeR3BoMxEJKNzdvUVSrLYiOOnbfjgriROZ407bNUHmNvIKicoSdL2fnlEH6zImfrVU22XRT5WNPx7QPQfS1Y03MycVsTfh Unknown+User@DESKTOP-JV41U9T

4. Configure local git repository

Once you are done with setting up public and private key then you need to setup locally cloned repository to use the SSH keys so that Git knows that it needs to use SSH key to authenticate user.

Browse to path to where local repository(Repo1 in our case) is cloned and open .git/config file. Leave the content of this file as it is and edit following two sections

[user]

name = User1

email = User1@domain.com

[remote “origin”]

url = git@ github-User1:Repo1.git

fetch = +refs/heads/*:refs/remotes/origin/*

[user] Add this section with email and username using which SSH key is generated.

url Copy it from GitHub repository under clone or download with Use SSH. In our case it will display like below under your repository in GitHub

git@github.com:Repo1.git

5. Verify SSH keys setup

Hey you are done with SSH keys setup. Let’s verify it by committing any source file of Repo1 and push commit. When you push it first time you might be ask to enter passphrase. Enter the passphrase given in Step1 and then Git will push it without prompting you for username or password. Even next time if you push it will not prompt for passphrase.

Git has internal security layer to setup master password to all SSH keys for which you get prompt to setup and use while pushing code. This password is common to all SSH keys generated as it is master password to all SSH keys.

Reference links :

https://help.github.com/articles/generating-an-ssh-key/

http://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you!

Advertisements

2 thoughts on “GitHub – Setup account using SSH keys

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s