SSH login without password using SSH keys

secure sshSSH or Secure SHELL is the most popular and trusted UNIX-based cryptographic network protocol. It can be used for secure data communication, remote server logins, remote command execution, and many other secure network services between two networked servers.

Normally, password authentication is used to connect to a remote server via SSH. In this blog tutorial we will show you how to login to a remote Linux VPS without password, using SSH keys. This method is more secure than using a password.

First of all, we need to generate the public and private keys. We will use the RSA (Rivest-Shamir-Adleman) cryptosystem.

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): PRESS ENTER
Enter passphrase (empty for no passphrase): PRESS ENTER
Enter same passphrase again: PRESS ENTER
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/
The key fingerprint is:
4a:91:e0:19:89:37:41:ce:3e:ff:56:2c:02:eb:65:cb root@LOCALSERVER
The key's randomart image is:
+--[ RSA 2048]----+
|   o=o           |
|  .+++ .         |
|   .=.o          |
|   ..  .         |
|    oo. S.       |
|    .++.. o      |
|   . +oo o       |
|    . E..        |
|       ..        |

This will create 2048 bit public and private keys in the ‘/root/.ssh/’ directory on your server

ls /root/.ssh/

Create a new ‘.ssh’ directory on the remote server

ssh root@REMOTE_SERVER mkdir -p .ssh

Copy the newly generated public key to the remote server you want to SSH without password

cat /root/.ssh/ | ssh root@REMOTE_SERVER 'cat >> /root/.ssh/authorized_keys'

Change the permissions of the public key and the ‘.ssh’ directory

ssh root@REMOTE_SERVER "chmod 700 .ssh; chmod 600 .ssh/authorized_keys"

Now, login to the remote server and configure the SSH server to accept key authentication. Open the SSH configuration file

nano /etc/ssh/sshd_config

And make sure that the following three options are not commented and  ‘RSAAuthentication’ and ‘PubkeyAuthentication’ are set to ‘yes’.

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys

Save the changes and restart the SSH server

/etc/init.d/sshd restart

That’s all. You should be able to the remote server without password


Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to set up this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Setup a mailserver with Exim and Dovecot on a CentOS 7 VPS
How to install ownCloud 6 on a Linux Virtual Server
Set-up SSL encrypted connection in Postfix, Dovecot and Apache
  • Max


    Author Reply

    To copy key to remote machine beter use ssh-copy-id

    • admin


      Author Reply

      Hi Max,

      Yes, we completely agree that using ssh-copy-id user@hostname is another way of copying the key to the remote machine.


  • Mandar


    Author Reply

    It is:
    ssh-copy-id -i ~/.ssh/ REMOTE_SERVER

  • leon


    Author Reply

    Very helpful
    Thanks for sharing!

  • TimWatley


    Author Reply

    Might as well create a 4096 bit RSA key.

    (ssh-keygen -t rsa -b 4096)

  • Rae Arvene Salud

    I tried this “ssh: Could not resolve hostname remote_server: Name or service not known” but it says here that “ssh: Could not resolve hostname remote_server: Name or service not known” i am new to linux please help

    • admin


      Author Reply

      Hi Rae,

      Please use your actual hostname or IP address instead of ‘remote_server’.