nice sudo or sudo nice?

Solution 1:

There's a difference, a crucial one.

If you want to decrease the process' priority, the order does not matter. On the other hand, if you want to increase it, you must put sudo before nice.

Since you are running the command as a normal user (otherwise you would not bother with sudo at all), you can only decrease the priority of your command. But if you use sudo first, you can increase it if you want.

Solution 2:

If you run nice sudo then the prompt for your password will also be niced, but since you'll spend far more time typing it, it really doesn't matter much.

As ThoriumBR noted, if you are lowering the priority, then the order is irrelevant, but if you want to raise the priority, then (since this must be done as root) you must use sudo nice.

Otherwise I can't imagine any real difference.


Solution 3:

Using the 'principle of least privilege' you should only run a program with root privileges if it needs them, and then drop them again as soon as you don't need them anymore.

So yes, there is a difference, if there is an exploit for nice, an attacker could run code with the same privileges as the nice program.

Also, sudo resets your environment, so it migh thave side effects, try

$ echo 'echo $PATH' | sh
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/jens/.local/bin:/home/jens/bin:/home/jens/.local/bin
$ echo 'echo $PATH' | sudo sh
[sudo] password for jens: 
/sbin:/bin:/usr/sbin:/usr/bin

So the 'nice' command you run via sudo might actually end up being a different binary.

$ which ash
~/.local/bin/ash
$ sudo which ash
[sudo] password for jens: 
which: no ash in (/sbin:/bin:/usr/sbin:/usr/bin)

Solution 4:

Late answer:

If your sudo access is limited to certain programs (foo and bar, for example), then you won't have authority to run sudo nice foo, but will be allowed to run nice sudo foo.