Running programs as sudo doesn't work
Installing tools and other using command line in Ubuntu 18.04 with sudo works perfectly.
example: sudo apt install ... works perfectly.
Running tools and programs via the command line as sudo doesn't work at all.
Running the same programs and tools as normal user works perfectly.
Example: start gtkwave as normal user makes the gtkwave GUI pop up. Start gtkwave as sudo, asks for password and then ends in: sudo: gtkwave: command not found.
I must say that some tools invoked on the command line as sudo work flawlessly, for example I can start Atom from the command line as a normal user and as sudo.
Is this maybe because the tools/programs not running as sudo are not installed in the common Linux folders like /bin or /usr/bin but as programs under /opt (these are added to the path and have necessary environments set)?
Does anybody have any idea why this happens?
31 Answer
By default, sudo searches for programs using its own secure_path that is defined in the /etc/sudoers file. From man sudoers:
secure_path Path used for every command run from sudo. If you don't trust the people running sudo to have a sane PATH environ‐ ment variable you may want to use this. Another use is if you want to have the “root path” be separate from the “user path”. Users in the group specified by the exempt_group option are not affected by secure_path. This option is not set by default.
Note that in spite of the last line there, the default Ubuntu /etc/sudoers does set it:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"so neither the invoking user's PATH nor root's PATH will effect whether programs are located when using sudo.
If you really want programs in locations such as /opt to be executable via sudo, you will need to either
use the full path e.g.
sudo /opt/somepath/bin/progmodify the sudoers
secure_pathto include the locations - if you decide to do this, please usesudo visudoto catch any syntax errors (else you risk locking yourself out ofsudoaltogether).
However, you should probably read Why should users never use normal sudo to start graphical applications? before proceeding.
1