0

Some commands like npm, composer, git, etc are not recommended run as root user. But sometimes I forget to run it when I log in as root, and my coworkers also sometimes do that too. That makes some files/dirs unable to be accessed by non-root users, which sometimes makes our website/apps errors. Is there any way to disable those commands when run as a root user? at least give warning/confirmation before running those commands.

2
  • Fix whatever problem with your systems or workflow that requires you and your co-workers to use (abuse) root so frequently.
    – user535733
    Sep 15, 2021 at 3:10
  • @user535733 thanks for your advice, but actually I am a very rare abused root user. And I can change my workflow, but I don't have a power to change my co-worker workflow. Sep 15, 2021 at 3:49

2 Answers 2

4

Ubuntu does not have a root user so I am assuming that you are using sudo su.

There are very few situations where this is needed. It's better that you learn to use sudo effectively only when superuser access is actually needed.

When you are using sudo su, everything you run is as if you were using the root user. By definition, the root user can do anything they want. You cannot restrict what can be done by sudoers when using sudo.

You cannot restrict the root user, nor can you restrict what someone with sudo privileges can do when using sudo, so what you are asking is impossible.

The better option is to correct your workflow so that you are not overusing sudo when it is not necessary.

3

You can use aliases to prevent those commands from execution. for example for preventing command git from execution you can append this line to the file /root/.bashrc:

alias git='echo You are root!'
3
  • Thanks. That's what I'm looking for Sep 15, 2021 at 9:16
  • 1
    @MuhammadDyasYaskur just a note that this won't apply if you do sudo git.
    – muru
    Sep 15, 2021 at 9:22
  • @muru yeah, I also tried it, and no problem, I and my co-workers never use sudo on commands that I mentioned. I just want to prevent the commands from sudo su or root user login. Sep 15, 2021 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.