I'm searching for a way to prevent my system users to sudo into the root account in any way.

I don't mean logging to the root user by SSH, but specifically by sudoing ( sudo -s ) ( sudo su ) etc.

Any suggestions?

(Expect white listing specific commands by sudoers file )


Thats what i want to prevent. when user uses "sudo -s" he will not be able to use the "root" user

  • Do you still want users to be able to use sudo to act as other non-root users?
    – Kusalananda
    Commented Oct 2, 2019 at 13:11
  • Yes, The users have sudo access to run privileged commands. the idea behind disabling sudo into root user is to find suspicous activities.when you implement your employees the idea of using sudo for every command, suspicious activity can be easier to find :)
    – ayali
    Commented Oct 2, 2019 at 13:24
  • 1
    Ah, so you're talking about being able to start an interactive root shell, only.
    – Kusalananda
    Commented Oct 2, 2019 at 13:26
  • I found this one : makeitcompliant.blogspot.com/2012/06/… that is actually my question "Users often drop to a root shell to avoid typing sudo before any command. Dropping to a root shell is usually done doing su -, sudo –i, sudo –s, sudo bash etc." what i want is to prevent my users from dropping into the "root" shell. the example on this website is not working. any suggetions?
    – ayali
    Commented Oct 3, 2019 at 11:08
  • What if I run sudo arbitrary-command and that command happens to be a shell itself, or happens to drop me into a shell? Also, having sudo access, I can simply disable your sudoers thing to circumvent your lock. It's obvious it is absolutely impossible to prevent. You either have to allow dropping into a root shell, or you must complete a white list of commands permitted with sudo. There could be no "middle ground". If you want to be secure and "compliant" (whatever that means), take the second path. That's a lot of work, I know, but it's you who wanted this strange. Commented May 7 at 4:45

4 Answers 4


Once you define the sudoers specification with the 'ALL' keyword in the commands field, there is no way to effectively prevent the execution of a specific command or set of commands. The sudoers manual explains this in a straightforward manner:

Limitations of the ‘!’ operator

It is generally not effective to “subtract” commands from ALL using the ‘!’ operator. A user can trivially circumvent this by copying the desired command to a different name and then executing that. For example:

bill ALL = ALL, !SU, !SHELLS

Doesn't really prevent bill from running the commands listed in SU or SHELLS since he can simply copy those commands to a different name, or use a shell escape from an editor or other program. Therefore, these kind of restrictions should be considered advisory at best (and reinforced by policy).

In general, if a user has sudo ALL there is nothing to prevent them from creating their own program that gives them a root shell (or making their own copy of a shell) regardless of any ‘!’ elements in the user specification.

The only way to avoid the scenario described above is to have a list of allowed commands that the user can execute, i.e. an allow-list.


That is not possible, because there is always a way to act as root as long as you do not white list specific commands in the sudoers file.

If you don't mind disabling the root login shell completely, you could disable the user shell.

It would still be possible for any sudo user to undo this of course.

  • 1
    using this method disables SSH access but no preventing users from using " sudo -s" to su into the root user
    – ayali
    Commented Oct 3, 2019 at 10:07

Remove the users from the sudo group and create a new group, say, restricted.

Then, in /etc/sudoers (or better yet, in a file under /etc/sudoers.d) you add something like this:

%restricted ALL=NOPASSWD: /usr/bin/command1,/etc/init.d/command2 restart

The users in that group will only be able to run those commands.

  • Hi, That was not my question :)
    – ayali
    Commented Oct 2, 2019 at 14:09
  • You're right. Sorry. Commented Oct 2, 2019 at 14:27
  • This new answer might solve it. Commented Oct 2, 2019 at 14:36
  • /usr/bin/rsync? Hopefully you won't be surprised when someone you gave the ability to run rsync as root to uses that to get full root privileges. "Gee, I have root rsync!!!! I can copy /etc/shadow out, change or remove the root password, and copy it back!" Commented Oct 2, 2019 at 14:47
  • @AndrewHenle that is not an actual entry. But I'll edit it so as to not distract people from the actual solution. Commented Oct 2, 2019 at 14:50

I was looking for the same so this is what I tried and worked

Created a group called admin and then added admin group to etc/sudoers

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

# usermod -aG admin u3
# usermod -aG wheel u3

# su - u3

[u3@localhost ~]$ yum install something
Loaded plugins: product-id, search-disabled-repos, subscription-manager
You need to be root to perform this command.

[u3@localhost ~]$ sudo yum install something
Loaded plugins: product-id, search-disabled-repos, subscription-manager

So the user u3 has the root power since added to the wheel group but it will not get root shell so sysadmin will know what u3 has been done with the sudo/root privilege.

  • Any user in your admin group can get a shell. If you read some of the other answers to this question they explain why your suggestion is flawed - as does the man page itself Commented Mar 19, 2022 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .