How to SSH Automatically without Password using Public Key

Posted on

This is an article related on the administration of Linux operating system specifically on how to SSH automatically without password using a public key. Sometimes, network administrator or any professions or roles related which need a remote access to a specific host, server or workstation must have an automatic access without having to entry the password.

For an instance, if there is a certain file which is generated daily as a backup mechanism executed by a cron task scheduler and it need to be moved or it need to be duplicate as soon as possible without having to be interfered or done by someone manually, the process of the file duplication where the destination exist in a remote host, server or workstation or any other storage media, it is important to connect or to copy remotely in a non-interactive mode. In other words, no password is needed to do the task described above.

The protocol utilized to accomplish the tasks is a remote access protocol known as SSH (Secure Shell). It is a network protocol for permitting remote access securely so that the means for accessing remote host, server or workstation is provided securely. Below are the steps taken to accomplish the task :

  1. Generate public key from the server, host or workstation which is going to remote the destination server, host or workstation as shown below :

[user@hostname ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx user@hostname
The key's randomart image is:
+--[ RSA 2048]----+
| xxxxxx          |
| x x             |
| x x x           |
| x x x           |
| x x             |
| x x x           |
| x xxx           |
| xxxxx           |
| xxxx            |
+-----------------+
[user@hostname ~]#

2. Copy the public key generated above by running the following command :

[user@hostname ~]$ ssh-copy-id [email protected]
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[user@hostname ~]#

3. Test the already copied public key by trying to remote or to copy remotely from the current place to the destination’s host, server or workstation address which is represented by an IP Address of xxx.xxx.xxx.xxx as shown in the above command execution. It is shown as follows :

[user@hostname ~]# ssh [email protected]
Last login: Mon Sep 4 xx:xx:xx 2017 from xxx.xxx.xxx.xxx
[user@remote ~]$

One thought on “How to SSH Automatically without Password using Public Key

Leave a Reply