6

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?

karel
  • 114,770
defossez
  • 161
  • 1
    can you do an echo $PATH with and without sudo and check if the path has the library needed to run the program? – blvdeer Dec 28 '18 at 16:43
  • 1
    If gtkwave works normal user then why are you trying to run it as root user? Again was it designed to work with sudo? – George Udosen Dec 28 '18 at 16:43
  • @blvdeer I'm having the same problem with OP. I tried the two commands and the outputs are the same. Actually I can't even run vi with root account, but I have to install with a root account. – Nicholas Humphrey Apr 26 '19 at 23:41

2 Answers2

7

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/prog

  • modify the sudoers secure_path to include the locations - if you decide to do this, please use sudo visudo to catch any syntax errors (else you risk locking yourself out of sudo altogether).

However, you should probably read Why should users never use normal sudo to start graphical applications? before proceeding.

steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • Or you could add yourself to the exempt_groupas it states in the man_page excerpt that you posted (or set exempt_group to your user group id a.k.a. user id. – Nate T Sep 07 '21 at 10:13
0

Instead of running

sudo myCommand

you can either do

sudo /full/path/to/myCommand

or

sudo "$(which myCommand)"

Toldry
  • 101