How to restrict direct root access in Linux

We can do it just in two steps.

Step One:

At first we will create new root user as follows (for example, newroot)

  1. Login as root using SSH.
  2. Create new user e.g., newroot and add user to wheel group
    useradd -G wheel newroot
  3. Set password for newroot
    passwd newroot
  4. You will get option to enter password. Use letter and number only.
    Save the newroot and password somewhere of you edge.
  5. Su to newroot
    su - newroot
  6. Test sudo permission (permission should be root)
    sudo whoami
  7. Enter password of newroot and it will show result “root”.
  8. Open sshd config file and check whether it has the “newroot” in AllowUsers. If not, add the user manually. (usually at the end).
    vi /etc/ssh/sshd_config

    [Permission Denied!] Quit :q

    AllowUsers newroot
  9. Restart sshd.service
    sudo systemctl restart sshd.service

Step Two:

Now we are going to disable the root user. Before doing this, you have to logout from “root” and then login with “newroot”.

You can only disable the  “root”, if you can successfully login with “newroot”.

  1. After successfully login with “newroot”, su to “root” using root password.
    su - root
  2. Then open sshd_config.
    vi /etc/ssh/sshd_config
  3. Search and change permitRootLogin “yes” to “no” as follows.
    permitRootLogin no
  4. Restart sshd.service to implement the change
    sudo systemctl restart sshd.service
  5. Now logout from “newroot” and try to login “root”
  6. Login with root now not allowed. Access Deny.
  7. So login with “newroot” and then su to root with root password
    su - root
 
  • unix, linux, centos
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How to extract .tar.gz files in Linux/UNIX OS

A tarball is a group of files that are bundled together using the tar command. Use the...

How to add welcome message when SSH start?

You need to change the contents of /etc/motd. Unfortunately, by default, /etc/motd is a link to...

How to change root password when SSH logged in

Run the following command: passwd Now type your new passwordOnce done, retype new passwordDone!...

How to install Pinguzo on any Linux/UNIX OS

Login to Pinguzo panel using Softaculous account or create an account of Pinguzo To add new...

How to use Postfix as Relay for Mailgun SMTP Relay

Mailgun is a popular SMTP Relay/API service, one of my favorites. For transactional emails, I...