How to establish SSH Connection in Ubuntu4 min read


Table of Contents
Introduction
SSH stands for Secure Shell protocol. It is generally used to connect 2 or more machines. With the help of SSH, multiple machines can communicate seamlessly with each other. In this tutorial, we will learn how to establish SSH Connection in Ubuntu.
Pre-requisites
- OpenSSH server should be installed in both Master and Slave
- Both the machines should be able to ping each other
There are 2 ways in which you can establish the connection,
- By creating a user (recommended)
- From root user (not recommended but can be followed for learning purpose)
Establish SSH connection by creating a user
In this method, we will create a user on all our nodes and then establish the SSH connection between them. Follow the below steps,
1. Execute the below commands on all machines to create a user
sudo su useradd hiberstack -m -d /home/hiberstack -s /bin/bash
2. Add the user to the sudoers file for root previliges on all machines
echo -e 'hiberstack ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/hiberstack
3. Generate ssh keys for the above created user on the master (controller) machine
su - hiberstack ssh-keygen
4. The SSH key has been created. Copy and paste this key to the slave machine. Execute the below commands in the master machine,
cd /home/hiberstack/.ssh cat id_rsa.pub
The key will be displayed. Copy the complete content (key).
5. Now we need to add this key to the slave machine. Execute the below commands in the slave machine,
su - hiberstack mkdir -p /home/hiberstack/.ssh vi /home/hiberstack/.ssh/authorized_keys #paste the key you copied from the master machine in this file, save, and exit chmod -R 700 /home/hiberstack/.ssh
6. Done. To check the connection, execute the below command from the master machine,
ssh [email protected]_ip_of_slave
Change the public_ip_of_slave accordingly. If the connection is successful, you will see that you are switched to the slave in the terminal. Execute exit command to switch back to the master.
Establish SSH connection in Ubuntu from root user
Follow the below steps to establish SSH connection from the root user.
1. In the slave machine, execute the below command,
sudo nano /etc/ssh/sshd_config
2. Search and change the below lines as below,
PermitRootLogin yes PasswordAuthentication yes
For the first parameter (PermitRootLogin), by default this line will be commented, you need to uncomment it and also change the value to yes. For the second parameter, the default value will be no and you need to change it to yes.




Save and exit with ctrl+x -> y -> enter
3. Execute the below commands now,
sudo systemctl restart sshd sudo su passwd
Provide a password.
4. Now in the master machine, execute the below commands,
sudo su ssh-keygen
5. Press enter whenever asked without making any changes. A new public key will be generated. You need to copy this key to the slave machine. Execute the below command for the same,
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]_ip_of_slave
In the above command, change the public_ip_of_slave accordingly.


6. You will be asked to enter a password. Enter the password you set in the slave machine.


7. The ssh connection has been established. Now you can execute the below command to connect to the slave machine from the master,
ssh [email protected]_ip_of_slave


8. Execute the exit command to switch back to master instance.
Done. We have successfully established SSH Connection.
Reading recommendations