Sunday, August 6, 2017

Command not found when using sudo

Give sudo your current PATH with:
sudo env "PATH=$PATH" your_command
 
 
Elaborating on @opyate's answer, I am using the following shell script (which may be named mysudo, for example):
#!/bin/bash
sudo -E env "PATH=$PATH" "$@"
  • -E tells sudo to preserve the environment.
  • env "PATH=$PATH" is expanded outside the sudo call, making the external PATH available inside the sudo too (this is required in addition to the -E as the PATH usually receives special treatment in addition to the treatment the entire environment receives).
  • "$@" passes the arguments our script receives to the sudo line.
Save the script in a file in a directory in the PATH, give it +x permissions, et voilà.
 
Since the current answers are a little vague, the specific setting in /etc/sudoers changing your path is secure_path:
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
You can modify it it with sudo visudo, or better yet, add the directories you need:
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
shareimprove this answer
 
 

No comments:

Post a Comment