Cyberithub

How to Setup Passwordless Authentication for git push in GitHub

Advertisements

In this article, we will see how to setup passwordless authentication for git push in GitHub. If you are using GitHub hosting service to keep and manage your project source code then you might have noticed that during every git push, it will ask you to provide user name and password to authenticate. As a user, you might find this pretty inconvenient as for every small changes also you need to authenticate to push your code. Well, the best solution to get rid of this is by creating a passwordless authentication where it won't ask you to provide your credentials again and again. This is very easy setup and can be quickly done in few steps as discussed below.

 

How to Setup Passwordless Authentication for git push in GitHub

How to Setup Passwordless Authentication for git push in GitHub

Also Read: Solved: "error: failed to push some refs to remote"

Advertisements

Step 1: Prerequisites

a) You should have a valid GitHub Account.

b) You should have access to the repository.

Advertisements

c) You should have access to add new SSH Key.

 

Step 2: Login to GitHub

You need to first open the GitHub login page in your favorite browser and then provide the credentials to login to your account as shown below.

Advertisements

How to Setup Passwordless Authentication for git push in GitHub 2

 

Step 3: Create SSH Key Pair

For creating a passwordless authentication, you need to first generate a public-private key pair using ssh-keygen command as shown below.

NOTE:

Advertisements
Please note that you can use ssh-keygen command from any Linux based system to generate the key pairs. But if you are looking to push the changes to the same GitHub account repo using multiple systems then each system must have the private key to authenticate against the uploaded public key. Otherwise, passwordless authentication won't work in those systems.
cyberithub@ubuntu:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/cyberithub/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/cyberithub/.ssh/id_rsa
Your public key has been saved in /home/cyberithub/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:bKYwAK0zKiHZRnZj+t8WoVVkrNCFJyH0hkdY64rGlIc cyberithub@ubuntu
The key's randomart image is:
+---[RSA 3072]----+
|     o.oo=. .    |
|  . + +oo.. .    |
| ..= ...++.o .   |
|..*oo. *+oo +    |
|.B.o. ..S. E o   |
|. = . +. + o     |
| . o .o o        |
|  . . ...        |
|     ...         |
+----[SHA256]-----+

 

Step 4: Setup Passwordless Authentication

Once the key pair is generated, you need to go to Settings from top right corner as shown below.

How to Setup Passwordless Authentication for git push in GitHub 3

Then click on SSH and GPG Keys under Access section from the left side column.

How to Setup Passwordless Authentication for git push in GitHub 4

Here click on New SSH Key from SSH keys to add a new key.

How to Setup Passwordless Authentication for git push in GitHub 5

Provide the key title, type and the key contents. In the key content section, you need to paste the contents from ~/.ssh/id_rsa.pub location in the system where you have generated your key pairs. Then click on Add SSH Key.

How to Setup Passwordless Authentication for git push in GitHub 6

You can see that the key will be added as shown below.

How to Setup Passwordless Authentication for git push in GitHub 7

 

Step 5: Configure Git 

In local Git configuration, first you need to configure remote origin URL by using below git config command. So the command syntax would be git config remote.origin.url git+ssh://git@github.com/<user_id>/<repo_id>.git. You can do the necessary changes in this syntax and and then run accordingly.

NOTE:

Please make sure to run all the below git commands from the local repo directory only. Otherwise, you might end up getting either incomplete output or fatal: not in a git directory error.
cyberithub@ubuntu:~/atlantis-example$ git config remote.origin.url git+ssh://git@github.com/cyberithub/atlantis-example.git

Then verify the configuration using git config -l command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git config -l
user.email=user@cyberithub.com
user.name=CyberITHub User
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git+ssh://git@github.com/cyberithub/atlantis-example.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

In the next step, add your generated private key as IdentityFile in ~/.ssh/config as shown below.

cyberithub@ubuntu:~/atlantis-example$ nano ~/.ssh/config
IdentityFile ~/.ssh/id_rsa

After adding the file, test the configuration by using ssh -T git@github.com command as shown below.

cyberithub@ubuntu:~/atlantis-example$ ssh -T git@github.com
Hi cyberithub! You've successfully authenticated, but GitHub does not provide shell access.

Now you can try to push the changes after adding and committing the files and then see if the passwordless authentication is working.

cyberithub@ubuntu:~/atlantis-example$ git push -u origin master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 650 bytes | 216.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git+ssh://github.com/cyberithub/atlantis-example.git
eb904c2..4f32267 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

From the above output, we can see that passwordless authentication is working now as it is not asking for username and password.

1 thought on “How to Setup Passwordless Authentication for git push in GitHub”

Leave a Comment