How to do it...

First, we will see how to disable SSH root login and then we will also see how to enable it again:

  1. First, open the main configuration file of SSH, /etc/ssh/sshd_config, in any editor:
    sudo nano /etc/ssh/sshd_config
  1. Now look for the line that reads as follows:
    PermitRootLogin yes
  1. Change the value yes to no. Then save and close the file:
    PermitRootLogin no

  1. Once done, restart the SSH daemon service using the following command:

  1. Now let's try to log in as root. We should get an error:
"Permission Denied" 

This is because the root login has been disabled:

  1. Now whenever we want to log in as root, first we will have to log in as a normal user. And after that, we can use the su command and switch to the root user. So, the user accounts that are not listed in the /etc/sudoers file will not be able to switch to root user and the system will be more secure:

  1. Now if we want to enable SSH root login again, we just need to edit the /etc/ssh/sshd_config file again and change the no option to yes:
    PermitRootLogin yes

  1. Then restart the service again by using the following command:

  1. Now if we try to log in as root again, it will work: