Open In App

How to Login to SSH Without A Password Using Private Key?

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

SSH or Secure Shell or Secure Socket Shell is a cryptographic network protocol. Two computers can share sensitive data over an insecure network using SSH. It can be used for strong password authentication and encrypted communication using public-private key pair. SSH is commonly used for remotely managing systems in a secured way over the internet.

Login to SSH using a Private key is an easy, secure, and convenient way of authentication than passwords.

We will discuss a 3 step process, for password-less authentication using a Private key on SSH.

Stepwise Implementation

Step 1: Public and Private key Generation

In the beginning, we will create a public and private key with ssh-keygen on the local machine using the following command –

ssh-keygen

After execution of the above command, it will ask for a location to store the private key. If we don’t specify any location, then it will be the default location. If a key already exists, it will ask you to overwrite it.  Next, you have to provide a passphrase for the key. It is optional and we can leave it empty. Now, your Public and Private keys will be generated and saved in the respective locations. In case you haven’t specified the location, then the default location will be –

Private key

/home/user/.ssh/id_rsa

Public key

/home/user/.ssh/id_rsa.pub

Below is the terminal shell pictorial representation after executing the following command –

 

Step 2:  Change Permissions

The file permission of the private key should be 600. So, we will use chmod command to change its permission. 

To change the permissions, we will first change the directory to the directory of the private key and then execute the following command –

cd ~/.ssh
chmod 600 id_rsa

Note: We have changed to the default directory here, it will be different if you have specified a different directory in the earlier step.

Below is the terminal shell pictorial representation after executing the following command –

 

Step 3:  Configure Server

Now we have to configure the server, to use our private key for login.

To do so, we need to run the following command –

ssh-copy-id user@IP

Note:  This command will be executed on the local machine and not on the remote server. To make it work, the password authentication between the local machine and server should be configured and present at that time.

Below is the terminal shell pictorial representation after executing the following command –

 

Now we are ready to connect with a remote server using SSH without a password.

To do so, use the following command –

ssh user@IP

Below is the terminal shell pictorial representation after executing the following command –

 

If you have specified a passphrase then it will ask you for the same.

In case, the key pair is not in its default location then use the following command –

ssh -i "private key path" user@IP

Now we have successfully performed passwordless login to SSH using a Private key.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads