How to disable some commands on root user?

Solution 1:

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.

Solution 2:

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!'