Sample command:
drush cc all
works, but this:
sudo drush cc all
gives me:
sudo: drush: command not found
Why? How to fix this?
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
preserve $PATH?PATH
)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 containingTERM
,PATH
,HOME
,SHELL
,LOGNAME
,USER
andUSERNAME
in addition to variables from the invoking process permitted by theenv_check
andenv_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.
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.
cd
. Usingdzdo cd
returning "command not found". Of course I later realized thatcd
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 todzdo ls
,dzdo mv
, etc. the contents from outside that dir. – Wildcard Nov 27 '15 at 00:31sudo cd
is thatcd
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