HowTo: Grant Root Access to User – Root Privileges – Linux

From this article you’ll learn how to create a user in Linux and grant root access to him or how to grant root privileges to already existent user.

This can be easily done by changing UID (user id) and GID (group id) in /etc/passwd file.

Also you will learn how to just add user to root group and i will explain how to delete user with root privileges.

Actually it is not a good idea to give all the privileges of root to a non-root user and outside the test environment i would not recommend to have multiply superusers.

Warning: Giving a non-root user all the permissions of root is very dangerous, because the non-root user will be able to do literally anything that could cause a big trouble if account is hijacked.

Check SSH Server Settings: If you have disabled root access in SSH server settings, by setting PermitRootLogin no in /etc/ssh/sshd_config – you won’t be able to login if your user has UID 0.

Grant Root Privileges To New User

Let’s say you need to create a new user and grant him root access to the server.

To create a user with exactly the same privileges as root user, we have to assign him the same user ID as the root user has (UID 0) and the same group ID ( GID 0).

Use the following commands to create a user john, grand him the same privileges as root and set him a password:

$ sudo useradd -ou 0 -g 0 john
$ sudo passwd john

Grant Root Privileges To Existent User

Cool Tip: Dot the i’s and cross the t’s on file and folder permissions in Linux! Make it more clear! Read more →

Perhaps you already have a user john and you want to grant him root privileges (make him a second root user):

$ grep john /etc/passwd
john:x:1001:1001::/home/alice:/bin/sh

For this, it is required to edit the file /etc/passwd and just change UID and GID to 0:

$ grep john /etc/passwd
john:x:0:0::/home/john:/bin/sh

Add User To Root Group

If you just want to add john to root group, without granting him all root privileges, run the following command:

$ sudo usermod -a -G root john

Delete User With Root Privileges

Cool Tip: Log in to a remote Linux server without entering password! Set up password-less SSH login! Read more →

You won’t be able to delete a user with UID 0 using userdel command:

$ sudo userdel john
userdel: user john is currently used by process 1

To delete him, firstly open the /etc/passwd file and change his UID.

For example, change the line:

john:x:0:0::/home/john:/bin/sh

to something like:

john:x:1111:0::/home/john:/bin/sh

After this, you’ll be able to delete user john with userdel command:

$ sudo userdel john

19 Replies to “HowTo: Grant Root Access to User – Root Privileges – Linux”

  1. “For this, it is required to edit the file /etc/passwd and just change UID and GID to 0”
    WRONG.
    Baaaad idea. Much regret. Undo, undo?!!

      1. ntfs@brix:~$ ssh 192.168.88.16 -l pi
        pi@192.168.88.16‘s password:
        Permission denied, please try again.
        pi@192.168.88.16‘s password:
        Permission denied, please try again.
        – – –
        Nice explanation?)

        1. Since noone elaborate how to undo this broken way of adding users, I will. The only way is to successful login to the linux machine, if you login via ssh or tty.
          If you add a user with uid 0 to group uid 0, and you set in sshd_config AllowRootLogin NO, you`re access will be denied. So you will have to login to the linux machine via tty (this is the only way if you don`t have a correctly added backup user on the linux machine). Via tty you can login with any of the users, even root.
          The correct way to add a user with root privileges is adding the user the normal way, useradd -m user, and then add privileges with visudo to the user.
          So if you have a backup user that haves root privileges in visudo. you will be able to login to the linux machine via ssh, and you will be able to change the uid and group to the “broken” user.
          I think this article explains more likely how to add a user with root privileges “the hacker way (which is a broken way)”, and repair the broken way after.
          Also if you don`t have access to a tty console, and you want to test the commands of this article and be sure you did not broke something, STAY LOGGED in current session and open another one and test if all things are OK. If you can`t login in the new session, you still have opened the previous session and you can repair what have you broken.
          Hope it helps to repair further mistakes.

  2. It is prohibited to have to users with ID 0 in passwd file. Strange things then happens since linux messes users with the same ID.

    1. Please post an example?

  3. After lots of searching finally found what i was looking for. Thankyou for sharing the post.

  4. That is awesome article! you mentioned all required points not only how to create user. don’t stop blogging man!

  5. Do not use that F*ING decision, after i do that, my server did not accept any accesses

    1. I know this is old, but for other’s coming across this comment and have the same issue or to avoid having this issue..

      In the servers ssh_config, switch AllowRootLogin to Yes. However, do so with caution. Do everything involved with sudo and root access with caution. Do your research. Figure out what you want and need.

  6. Спасибо. Приятный сайт

  7. if using ssh authentication not working

    1. I know this is old blog, but for other’s coming across this comment and have the same issue or to avoid having this issue..

      In the servers ssh_config, switch AllowRootLogin to Yes. However, do so with caution. Do everything involved with sudo and root access with caution. Do your research. Figure out what you want and need.

      I posted this also as a reply to an older comment.

  8. «For this, it is required to edit the file /etc/passwd and just change UID and GID to 0»
    It’s a bad idea because after restart linux system you will not see user with UID 0 and GID 0 in log-in menu of linux. In this case you can log-in into linux just through the command-line. To get there from a log-in menu of linux is to push buttom combination Ctrl + Alt + F1.

    1. you are saying the correct but can u please share the way by using that i can delete the root user by using the second root that i had created as root.

  9. Just changed uid and guid to 0 in passwd, now i’m fucked up, thkx u… :\

  10. For those having issues with this make sure you have root login enabled prior to editing anything so your root accounts can log in and try running this when done so changes you made take affect.
    sudo service ssh restart

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.