How to setup SSH Key-Based Authentication (Private & Public keys)

What is SSH?

SSH – secure shell is a protocol that helps systems to in an secure & encrypted manner over an network

Using SSH, we can create a secure connection to the remote server using a specific user account & get the shell session on the remote server as that user & can perform tasks

 

NOTE: In order to login to the remote server, we need sshd service running in the remote server

 

How to setup SSH Key-Based Authentication ?

There are few advantages of using SSH Key-Based Authentication

• Getting access to the system by password guessing techniques is defeated

• With passphrase-protected private keys, Both the passphrase and a copy of

the private key is needed.

 

This is based on private-public key mechanism

A private key, a matching public key is needed for this

The private key acts like a password, which must be preserved securely, Public key is copied to the system the user wants to access

When try to access the remote server, public key & private key are matched and this permits the user to securely access the remote server without password

 

Generate SSH keys

  • 1.       Ssh-keygen – private key and matching public key is generated, private & public keys are saved in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub files

On executing this command, passphrase is prompted, it is optional to set it. You can continue with empty passphrase by just hitting ENTER

                Next step is copying the public key to the remote server

  • 2.       Ssh-copy-id copies public key to the remote server, if we do not specify the pub file when executing the command, it uses default public file

                Ssh-copy-id user@remotehost

 

NOTE:  if the private key is protected with passphrase, you have to enter the passphrase to use private key . you can use ssh-agent to cache passphrase in memory, so need not have to enter passphrase everytime

Some important things to be kept in mind:

·         Do not allow direct remote login to the root account, In order to avoid this we have a config settings  in /etc/ssh/sshd_config file PermitRootLogin yes (default) – This can be changed to No

·         Do not allow Password based authentication, In order to avoid this we have a config settings in /etc/ssh/sshd_config file PasswordAuthentication yes (default) – This can be changed to No

 

Comments

Popular posts from this blog

How services works in Linux

Permissions in Linux