18

Sample command:

drush cc all

works, but this:

sudo drush cc all

gives me:

sudo: drush: command not found

Why? How to fix this?

dr_
  • 28,763
  • 21
  • 89
  • 133
Codium
  • 313
  • 1
  • 2
  • 7
  • I've also had this issue sometimes, with such basic commands as `cd`. Using `dzdo cd` returning "command not found". Of course I later realized that `cd`ing into a directory that I don't have permissions in, won't help very much, so I either need a root shell or need to `dzdo ls`, `dzdo mv`, etc. the contents from outside that dir. – Wildcard Nov 27 '15 at 00:31
  • 2
    The problem with `sudo cd` is that `cd` is a built-in command, not a program. If you want to go into a directory that *you* don't have any access to, and (for example) rename a file there, you could do (for example) `sudo mv dir/oldfile dir/newfile` ***or*** `sudo sh -c "cd dir; mv oldfile newfile"`. – G-Man Says 'Reinstate Monica' Nov 27 '15 at 22:39

2 Answers2

18

When you sudo, you get a preconfigured $PATH, which is (supposed to be) something like the root user's default path. Your program is not in that list of directories identified by $PATH.

See for example

sudo tries to be safe when executing external commands.

There are two distinct ways to deal with environment variables. By default, the env_reset sudoers option is enabled. This causes commands to be executed with a minimal environment containing TERM, PATH, HOME, SHELL, LOGNAME, USER and USERNAME in addition to variables from the invoking process permitted by the env_check and env_keep sudoers options. There is effectively a whitelist for environment variables.

If you cannot configure sudo to preserve your $PATH, the usual workaround is to specify the complete pathname of the program. That may not work well with scripts that call other executables in the (not-accessed) directory.

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
1

You should specify the full path. It's also more secure; it you don't specify the path, it's conceivable that an attacker could create another program that will be run with root permissions.

Also, you need to put a line in /etc/sudoers to allow it. man sudoers for the syntax, it's way too much to put here.

Tom Zych
  • 923
  • 8
  • 17