ssh in Linux with examples

Hello, everyone In this Article we are going to learn what is ssh in Linux  in a very simple and easy step-by-step approach

ssh in linux

SSH in linux

The SSH is used to take remote for the different server. The SSH stands for secure shell. When we use SSH and when the connection between servers is created that connection is in an encrypted format.

In SSH the First machine will take the fingerprint of the other machine in this the remote machine Mac address etc. is stored in the future if someone changed the machine and assign the new IP then the MAC address will change and a new fingerprint will be generated.

The basic requirement will be machine must be ping SSH package ie. Open SSH -server and Open SSH- Client must be installed on both of the machines.

Suppose that we have two servers as follows

  • Server 1 IP Information – 172.31.240.113/16
Screenshot 8 12
  • Server 2 IP Information – 172.31.9.73/16
Screenshot 9 12

The server-1 is able to ping to server-2

Screenshot 10 11

The server-2 is able to ping to server-1

Screenshot 11 12

How to take SSH for the remote machine

To take the remote for the machine we must know the password for the user that we are taking the remote connection.

We are defining the password for server-1 machine for the root user

Screenshot 12 12

We are defining the password for server-2 machine for the root user.

Screenshot 13 10

To take the ssh connection for the remote machine we need to run the ssh command with the IP address for the remote machine with the username for the remote machine where we want to set the connection

# ssh root@172.31.9.73

Here we put the remote Ip address this will generates the figure print of the server-2 machine in our server-1 machine.

Screenshot 14 7

Then this will ask for a password for the server-2 machine root user after that connection will successfully created.

To disconnect the connection just run # exit command.

Now in companies they deny the root login In the origination they don’t want someone to Take SSH using root user .

To deny root login we need to edit the vi /etc/ssh/sshd_config file. And we need to put no in Permit root login.

Screenshot 16 6
Screenshot 15 7

Then restart the ssh service using # systemctl restart sshd command

Screenshot 17 7

Now we have disabled the root permit login to the server-2

Now we are making user name as commonuser in server-2 and assigning the password to them So that we can connect to server-2 with the commonusername

Screenshot 19 5

Now we are taking connection using ssh command and the user that we created just now. And this works perfectly

Screenshot 20 3

How to deny the user in the remote machine

Now we want that the user that we created above we want to deny that user for the remote login

Just go to Server-2 Machine and again edit vi /etc/ssh/sshd_config file and add the Parameter as Denyusers commonuser .( Name of the user that we want to deny )

Screenshot 22 2
Screenshot 21 3

Again restart the sshd service

Screenshot 23 1

Now the commonuser is deny for the remote login permit.

This means that only commonuser is deny rest all other user are allow

The best practice is that we need to define only the allowed users in the # vi /etc/ssh/sshd_config file

Screenshot 22 3
Screenshot 24 2

Again restart the sshd service

Screenshot 23 1

Now only commonuser is allowed rest all the users are deny.

How to run commands using SSH to remote machine

We can also run the commands to the remote machine using ssh concept. When we execute this command from the machine-1 then we can see that the Directory name as NewDir is created in Machine-2 common user home directory

# ssh commonuser@172.31.9.73 mkdir/home/commonuser/NewDir

How to take SSH passswordless

Now we want to take the SSH to the remote machine but passwordless.

The need for this is that suppose that we have a script that is placed in server-1 that we want to run in machine-2 at night 12 if we do not make server password-less then at night 12 this will ask for the password for the machine-2 in order to run the script.

To avoid this situation we need an SSH as passwordless.

How to generate the Key. To generate the key we can use the ssh-keygen command from the Machine-1

# ssh-keygen
Screenshot 25 2

How to transfer the key to remote server

First, we need to do to # cd .ssh/ Directory here the key is generated.

Then we need to run this command with the Machine-2 Ip address in order to send the Key to the remote location server. This will ask remote machine password once

# ssh-copy-id -i root@172.31.9.73
Screenshot 26 2

Now when we execute # ssh root@172.31.9.73 command this will not prompt for the password. This is the IP address for the server-2 machine

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

This is the complete knowledge of SSH in Linux,if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment