Set up SSH Authentication using Git Bash – Login for Linux Server.

Last updated on May 26th, 2021 at 07:34 pm.

Setup SSH Authentication using Git bash – Setup SSH key login for Linux Server.

In this post let’s see how you can setup SSH Authentication using Git Bash on a Windows PC. By the end of this post you will have generated a key pair, added the public key on your Linux server and tested your login.

The aim of this post is to enable you log into any Linux server using SSH keys (public and private key pair). This post is for anyone using Windows. That is, for the Windows user who would like to use Git Bash for this process. Here is a video for setting up SSH authentication using putty and Puttygen.

This post is adapted from this Linux / Mac SSH key setup.

Steps of what we’ll do to setup SSH Key Login using Git bash.

Step 0 : Install Git . Download git from the official site here. Click on Windows. The download begins automatically. Install it. Git will come with Git CMD, Git Bash and Git Gui. W’ll use git Bash.

Step 1: First we’ll create the ssh key folder storage location.

Step 2: Second, we’ll generate the key pairs .

Step 3: Third, we’ll add the public key onto the Linux server.

Step 4: Finally, we’ll test our SSH Authentication login

The way that I’m going to show you here is a nicer way whereby you can organize your keys in different folders and log in as you may need to.

Step 1 : Create the ssh key folder

Since we are on windows we can just cd into the users home directory. By default gitbash’es home directory is your Username home directory on Windows. To go to it, go to Local disk C, then Users, then your username. That is your Home directory.

In your home directory, check if .ssh folder is available. If it is not, create it; create a folder called, .ssh .

As I said, we’ll group our keys in different folders. This is a nice way to organize different keys for different servers. For this reason, inside of the .ssh folder, create another folder. I will call my folder , Debian10Server . A Name withNo spaces ,

For my first key pair, I will store them in .ssh/Debian10Server .

For the next server keys, I may store them in another folder.

Let us generate the keys.

Step 2: Generate SSH key using Git Bash

Open up Git Bash on Windows.

I want to avoid typing paths, so let’s cd into the folder we created for our ssh keys. Remember to create the folders before you can cd into them.

   cd   ~/.ssh/Debian10Server
cd into git bash folder

Then once inside that folder, generate the SSH key pair as follows.

ssh-keygen -t rsa -b 4096 -C "[email protected]" -f firstserverkey_rsa

Our keys will be called firstserverkey_rsa, that is firstserverkey_rsa.pub for the public key and firstserverkey_rsa for the private key. You can name them whatever you want.

-b specifies the bits to be generated, in this case 4096 bits

-C is just a comment for the key. Add a comment for the key. Read more about SSH options here.

Once you run the command above, Enter a passphrase for your key. A passphrase is a password for your key. You must add one to secure it (your private key) even further.

You key pair has successfully generated. Please remember that I’m currently in the folder where my files are, I did cd into that folder earlier. That is why once my files are generated, the path is as follows:

git bash SSH key Authentication key generated

Now that we’ve generated the key, next , let’s add the public key to our Linux server. Please note that this step will work on any Linux server: Debian, Ubuntu, CentOS etc.

Step 3: Add the public SSH key onto the Linux server.

Since we did not put our key in the default location which is in the root of .ssh folder, we have to give the path identity of the key to upload. This is done as follows:

ssh-copy-id -i ~/path-to-public-key [email protected]

Whereby -i is used to specify the path / identity of the public key.

[email protected]: user is the server username for whom the public key is being setup. Host is the server ip or accessible hostname url.

The public key to be added, has a .pub extension.

For example we can add our public ssh key above as follows:

ssh-copy-id -i firstserverkey_rsa.pub [email protected]

Note: The above will work if you are still in the directory where the keys are. If not, cd into it.

An alternative would be to give the full path of the public key as follows:

ssh-copy-id -i ~/.ssh/firstserverkey_rsa.pub [email protected]

The tilde (~) represents the path to your home directory.

Step 4: Finally, we’ll test our SSH Authentication login

Logging in via SSH key.

To log in via SSH, this time round you have to add the path of your key:

ssh [email protected]_ip -i ~/.ssh/path-to-private-key

eg

ssh [email protected] -i  firstserverkey_rsa 

The -i option , enables you to choose an identity file to be used to log in.

The above will work if you cd into the folder where your keys are located.

Change permissions for .ssh and authorized_keys

The .ssh folder is on your Linux server. Inside it there is an authorized_keys file. Inside of this authorized-key file is where your public keys are.

We will change the permissions for .ssh folder and the default authorized_keys file using the following command:

sudo chmod 700 -R ~/.ssh && sudo chmod 600 ~/.ssh/authorized_keys

Then change ownership to your new user’s Folder. Do this if you are adding ssh keys for another user who isn’t root. In the following command, change all instances of joe with the username you are adding the key for.

sudo chown -R joe:joe /home/joe

Disable Root login and Password Authentication after Setting up SSH Authentication using Git bash

After changing the permissions above, try to login again. If you log in successfully, disable root login and password authentication.

Once you confirm you can log in successfully, disable Root Login and Password Authentication as is directed below.

Open up the SSH config file:

sudo nano /etc/ssh/sshd_config

Look for the PermitRootLogin line, uncomment it (remove the #) and set the value to no.

PermitRootLogin     no

Do the same for the PasswordAuthentication line:

PasswordAuthentication      no

Save and close the file. (CTRL X to exit and then Y to confirm changes . And then enter) To apply the new settings, reload SSH.

On Centos:

sudo systemctl reload sshd

Debian / ubuntu:

sudo service ssh restart
systemctl restart ssh

That is how to Setup SSH Authentication using Git bash on Windows.

If you experience any errors feel free to leave a comment. I will help you get it right.

This post is adapted from : How to Setup SSH Keys for Logging Into your Linux Server- Bizanosa .

Comment Here

We use cookies on Bizanosa.com. Continuing to use this website, implies consent of our Privacy Policy Privacy Policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close