Menu Close

How to Setup SSH Login Without Password in Linux

Automating routine tasks is important for linux administrators, this is to avoid overloading them when doing the same tasks every day. Tasks such as file transfer from server A to server b using scp or rsync commands can be automated by setting up the SSH login without password and integrate it with scp or rsync. This is the most basic skill that all linux administrators must know.

Setup SSH Login Without Password in Linux Guide

1) Setting up ssh-keygen & ssh-copy-id.

ssh-keygen is a Unix utility that is used to generate, create, manage the public and private keys for ssh authentication. Meanwhile the ssh-copy-id is a script that copies the local-host’s public key to the remote-host’s authorized_keys file.

Create public and private keys using ssh-key-gen. You have option to protect the private key with a passphrase :

# ssh-keygen

Example :

[root@centos78 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0MAiahOBpa+5WyKn/IKdZabcdeC138QndefTghi+Ue8 root@centos78.example.com
The key's randomart image is:
+---[RSA 2048]----+
|.o.  .o.   +=    |
|... .o.+. .oo+ . |
|o.. o.o.. .+o.o .|
|.o.o   .. o o=  .|
|ooo     S. .  +. |
|oo.          .  E|
|o=o.             |
|=+B.             |
|oB=.             |
+----[SHA256]-----+

2. Copy the the public key to remote-host using ssh-copy-id. This will appends the keys to the remote-host’s .ssh/authorized_key.

a) With Default SSH port

# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.8

Example :

[root@centos78 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.8
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@192.168.0.8's password:

Number of key(s) added: 1

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

b) With Custom SSH Port “222” :

# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.8 -p 222

Example :

[root@centos78 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.8 -p 222
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@192.168.0.8's password:

Number of key(s) added: 1

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

You are done with the basic steps to setup SSH login without password in linux.

Leave a Reply

Your email address will not be published. Required fields are marked *