SSH Public Key Based Authentication

The SSH protocol is recommended for remote login and remote file transfer which provides confidentiality and security for data exchanged between two computer systems, through the use of public key cryptography. The OpenSSH server provides this kind of setup under Linux. It is installed by default. This how-to covers generating and using ssh keys for automated usage such as:

  1. Automated Login using the shell scripts.
  2. Making backups.
  3. Run commands from the shell prompt etc.

Now to start we have two servers, the local server where we will create the public and private keys, and the remote server where we need to send our public key

 

Generating SSH Keys

First, log on to your local server from where you will start an ssh connection to the remote server, then generate the keys by running:
ssh-keygen -t rsa

Assign the pass phrase (press [enter] key twice if you don’t want a passphrase). It will create 2 files in ~/.ssh directory as follows:

~/.ssh/id_rsa : identification (private) key
~/.ssh/id_rsa.pub : public key

Now we need to copy id_rsa.pub (public key) to the remote server in the file authorized_keys, this is know as Installing the public key to server.
We can use scp to do that:
scp .ssh/id_rsa.pub tassaf@remote.com:.ssh/authorized_keys

Or we can just copy and paste the content of id_rsa.pub in authorized_keys manually

 

Now to test run ssh connection from local server:

ssh remote.com

It should login automatically without asking about username and password..

Leave a Comment.