How to Automatically Login SSH via Command Line

Posted on

As shown in the title of this article, the main purpose is to show how to automatically login SSH via command line. As we already knew, SSH or Secure Shell which is considered as a protocol for remote communication must be done by providing username and password. But there are an occurrence or a situation where the process or logging using SSH can be done automatically without providing any additional information such as password.

It is actually can be seen as a process to login to a remote server using SSH remote connection protocol interactively without having to provide any additional information such as password. It can actually be done in several steps. Those steps are explained sequentially as follows :

1. Generate Public Key

This step is basically generating a key which is called a public key because it will be given to the other which in the context of this article, it is the server which is going to be logged. In order to do that, the following is the command for generating the public key :


The following is the example of the command execution stated above :

user@hostname:~$ ssh-keygen 
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/
The key fingerprint is:
SHA256:U/Ld9KwnJcrbeO3rg8i+lJ3Jw5OzTcQCwqFxUwWswew user@hostname
The key's randomart image is:
+---[RSA 2048]----+
|       .o+ooo.   |
|        =+o.     |
|       .oo+.  .  |
|         E...o.o |
|        S . ..oo+|
|         . .=.*+ |                                                                                                                                                                                                               
|          .o+@+o.|                                                                                                                                                                                                               
|          .o =O+.|                                                                                                                                                                                                               
|          .o+oo=+|                                                                                                                                                                                                               

2. After the public key has already been successfully generated as shown in the first step, copy the public key using the following command :


The above command can be executed as the following example :

user@hostname:~$ ssh-copy-id
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh ''"
and check to make sure that only the key(s) you wanted were added.


The above step needs a certain user which has already created and it is exist in the remote server which is represented by an IP Address of in the context of this article. Sure, IP Address which is being discussed is the IP Address owned by the remote server.

3. Login to the remote server using the following command :


The command which is stated in the above can be execute as the example shown below :

                                                                                                                                                                                                               user@hostname:~$ ssh
Last login: Thu Mar  1 12:59:35 2018 from
[user@remote ~]# 

So, based on the above output, the remote server is represented by an IP Address of It is using a user named ‘user’ to login to the server. And based on the output above, it is done interactively without supplying any password at all. That is because in the second step, the public key which is generated in the first step has already been copied. So, using the public key, further password supply doesn’t event required in the process of SSH remote login.

Leave a Reply