Passwordless SSH (Secure Shell) between two machines is required by a lot of distributed frameworks. It creates a secure shell connection from the host machine to the remote machine without password prompt.

Follow the steps below to configure Passwordless SSH between two linux machines.

Prerequisites

1. Install Open SSH Server package on your machines. This will also start sshd service on port 22 on your machines.

Ubuntu:

sudo apt-get install openssh-server

Centos:

sudo yum -y install openssh-server openssh-clients

[NOTE: Now by default all operating system comes with pre-installed “Open SSH Server”, so you won’t need to install it.]

2. Check the status of sshd service and start the service if the status is not running.

Ubuntu: Command Output will look like  “* sshd is running” OR “* sshd is not running” 

/etc/init.d/ssh status

Centos: Command Output will look like “openssh-daemon (pid  985) is running…” OR “openssh-daemon is stopped”

sudo service sshd status

Steps for Passwordless ssh

1. Install ssh keys on your machine. The command will create an ~/.ssh/ folder for the current user and the folder will have the public (id_rsa.pub) and private (id_rsa) key pair. Execute the command mentioned below:

ssh-keygen

Note: If you are getting Permission Denied error while generating the Public and Private keys, then make sure that the SELinux is disabled on your machines. To disable SELinux, you can read here

2. To enable Passwordless SSH to a remote machine, you need to copy the contents of your public key file (id_rsa.pub) to the ~/.ssh/authorized_keys file of the remote machine. ssh-copy-id command is the easiest way to copy the key to the remote machine. You need to specify the user and the hostname of the remote machine. If the user is not specified, it’ll take the user executing the command as the default user. Execute the following command on the host machine.

ssh-copy-id <username>@<hostname>

Replace the <username> and <hostname> by the actual username and hostname of the remote system. This command will prompt for the password for <username>@<hostname>.

For Example, on node1 run below commands: (This will make Passwordless SSH from node1 to node2)

ssh-copy-id localhost
ssh-copy-id hadoop@node2

The output of the above command will look like:

Now try logging into the machine, with "ssh 'hadoop@node2'", and check in:

 ~/.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

Similarly, on node2 run below commands: (This will make Passwordless SSH from node2 to node1)

ssh-copy-id localhost
ssh-copy-id hadoop@node1

3. Passwordless SSH configured successfully. You can now use the ssh command to verify the same.

ssh <username>@<hostname>
Share this:

One thought on “Passwordless SSH between linux machines

Leave a Reply

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