1

Problem:

Running a script as sudo returns

sudo: mount-remove-hiberfile: command not found

Info:

  • Scripts folder: '/scripts' (all my custom scripts are kept here)

  • My User: 'cybex'

cybex@cybex-W55xEU:~$ echo $PATH //and 'sudo echo $PATH' return the same path

/home/cybex/.rbenv/plugins/ruby-build/bin:/home/cybex/.rbenv/shims:/home/cybex/.rbenv/bin:/home/cybex/.rbenv/plugins/ruby-build/bin:/home/cybex/.rbenv/shims:/home/cybex/.rbenv/bin:/home/cybex/bin:/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/scripts:/snap/bin

root@cybex-W55xEU:/home/cybex# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/scripts

The script 'mount-remove-hiberfile' cannot be run as a normal user, thus I would need to run sudo mount-remove-hiberfile.

Why does sudo not detect this script?

note: I have found a few questions regarding this but none which answered my question

UPDATE

ls -l /scripts/
total 20
-rwxr-xr-x 1 root root   95 Apr 26 22:45 apt-manage
-rwxr-xr-x 1 root root   40 Apr 26 22:45 apt-update
-rwxr-xr-x 1 root root  769 Apr 28 20:47 create-desktop-file
-rwxr-xr-x 1 root root  370 May 16 22:45 extractgst.sh
-rwxr-xr-x 1 root root 1085 May 16 21:25 mount-remove-hiberfile

UPDATE (include a paste that includes the command and the error and directory )

cybex@cybex-W55xEU:~$ pwd
/home/cybex
cybex@cybex-W55xEU:~$ ls -l /scripts/
total 52
-rwxr-xr-x 1 root root   95 Apr 26 22:45 apt-manage
-rwxr-xr-x 1 root root 2283 May 21 16:59 backup
-rwxr-xr-x 1 root root  109 May 21 16:59 check-port
-rwxr-xr-x 1 root root 2185 May 21 16:59 connected
-rwxr-xr-x 1 root root  769 Apr 28 20:47 create-desktop-file
-rwxr-xr-x 1 root root  164 May 21 16:59 download
-rwxr-xr-x 1 root root  370 May 16 22:45 extractgst.sh
-rwxr-xr-x 1 root root 5142 May 21 16:59 firewall
-rwxr-xr-x 1 root root 3881 May 21 16:59 firewall.save
-rwxr-xr-x 1 root root 1085 May 16 21:25 mount-remove-hiberfile
-rwxr-xr-x 1 root root   80 May 21 16:59 portquiz
-rwxr-xr-x 1 root root   84 May 21 16:59 space-used
drwxr-xr-x 1 root root    0 May 21 17:00 tmp
cybex@cybex-W55xEU:~$ sudo mount-remove-hiberfile 
sudo: mount-remove-hiberfile: command not found
8
  • i'm not that familiar with linux, but try doing ls -l on the script might help. it will show you the permissions. And put a line at the top of the script to echo something to the screen so you know whether or not it is running it. And try to make your own script with the same permissions and see if sudo can run it or not. Idea is to troubleshoot whether it's permissions or something in the script.. though a linux tech might see straight away.
    – barlop
    Commented May 21, 2016 at 9:58
  • Are you sure the file is executable? chmod +x <SCRIPT> Commented May 21, 2016 at 10:07
  • @joejoe31b it's sad how many times people have been asked this, check update, but yes, it is
    – CybeX
    Commented May 21, 2016 at 10:31
  • @barlop see update
    – CybeX
    Commented May 21, 2016 at 10:31
  • 1
    can you include a paste that includes the command and the error and directory (rather than stating the error separately from the command). Copy/paste the output and comment on it. Also try from within the directory sudo ./yourscript
    – barlop
    Commented May 21, 2016 at 16:59

2 Answers 2

3

The sudo configuration by default will reset the path regardless of how sudo is called. Here are two common ways to work around this, the first is the permanent change, the second is a command line argument to sudo (which you could add as an alias).

permanent & global change

In /etc/sudoers you need to modify two settings:

sudo visudo

In the sudoers file there is by default a setting called Default env_reset which clears the environmental settings from the sudo callers shell, even if sudo is called with -E (keep environment).

In the file look for a setting called env_keep. This determines what enviromental variables are allowed to be passed into the root environment that is created with sudo. There are typically a series of env_keep="xxx" and env_keep+="xxx" statements in the default sudoers file. By default PATH is not one of the env vars allowed to pass through.

Add PATH to one of these entries or comment out the env_reset line. If you do not have env_keep entries, look for Defaults env_reset and add a new env_keep = "PATH" entry after it.

Secondly, even if you allow the PATH variable to pass into the sudo env, the path is limited in sudoers by the setting Defaults secure_path='...' You can either comment out this setting or add the path that your script is in to the entry.

After that, sudo will maintain the PATH variable and execute your script.

command line workaround

To avoid modifying the sudoers file, you can call sudo with this syntax:

sudo env "PATH=$PATH" <script name>
2
  • 1
    thanks, this worked. I saw it /etc/sudoers had a directory path specified, but wasn't sure if editing it was a good idea. I did it anyway, solved my problem :p
    – CybeX
    Commented May 23, 2016 at 8:55
  • This is an excellent answer. Could use a bit of placeholders in it for various problems that users face, though.
    – cst1992
    Commented Nov 15, 2017 at 6:24
3

sudo does not, by default, use your path, and does not run root's bashrc or equivalent. See https://askubuntu.com/q/128413 for a full description.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .