Sudo not finding scripts in $PATH

I have scripts in ~/bin. They work fine from user, but when using sudo to run them, the system can not find the scripts via $PATH. (works fine with absolute or relative path)

sudo echo $PATH
/home/bedna/.local/bin:/home/bedna/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

$ ls -l bin
total 280
-rwxr-xr-x. 1 bedna bedna   1537 Nov  8 04:26 backup
-rwxr-xr-x. 1 bedna bedna    629 Oct 27 12:23 mount-backup
-rwxr-xr-x. 1 bedna bedna 141870 Nov  8 02:09 shrink-backup
-rw-r--r--. 1 root  root  128926 Nov  8 01:59 shrink-backup.log

$ sudo backup
sudo: backup: command not found

$ whereis backup
backup: /home/bedna/bin/backup

$ sudo whereis backup
backup:

sudo autocompletes to the stuff in ~/bin just fine when pressing tab, but does not find them when running.

I have both complete -cf sudo & alias sudo='sudo ' in ~/.bashrc.d/bash_aliases but that does not do the trick either. (do I need to add that to /root/.bashrc or smthn? Or secure path in sudoers?)

Thank you in advance.

1 Like

Would be great if someone could delete the comment from the bot above (and the exact same comments he made on a bunch of other threads as-well).

You (mod) can then delete this post too. Thank you (and hope I get a response on my question in OP)

Sudo drops various options, because of security reasons.

Either use the full path or take a look into the manpages of sudo.

And maybe sudo changes to “root” before running your commands, thus $PATH would be from “root”.

Never looked that deep into the mechanisms of sudo.

$ env > a
$ sudo env > b
$ grep ^PATH a b
a:PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/jlehtone/.local/bin:/home/jlehtone/bin
b:PATH=/sbin:/bin:/usr/sbin:/usr/bin

The man sudo and particularly man 5 sudoers do tell what is done for the environment. For example:

secure_path If set, sudo will use this value in place of the user’s PATH environment variable. There are two basic use cases for secure_path:

  1. To make it possible for sudo to find system administrator commands located in directories that may not be in the default user path, such as /usr/sbin.
  2. To help protect scripts and programs that execute other commands without first setting PATH to a safe value. Otherwise, a user with limited privileges may be able to run arbitrary commands by manipulating the PATH if the command being run executes other commands without using a fully-qualified path name.

Users in the group specified by the exempt_group option are not affected by secure_path.

and secure_path is set in Alma {8,9,10}:

# grep -i path /etc/sudoers
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

You run this command in bash. The shell replaces the variable with value before command is executed. You did effectively type:

sudo echo /home/bedna/.local/bin:/home/bedna/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

and therefore the root user did run command:

echo /home/bedna/.local/bin:/home/bedna/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Hence the output is not from the PATH that the sudoed environment has.

1 Like

Ah, so it is secure_path that stops it.
Didn’t think about using env | grep PATH instead of sudo echo $PATH, thanks.

My only remaining questions then is:

  • Does secure_path override Defaults env_keep += "PATH" or Defaults:%sudo env_keep "PATH"?
  • What would be better, add /home/bedna/bin to PATH in /root/.bashrc or edit secure_path with a dropin file?

There was also that exempt_group option for sudo.

We did already see that /root/.bashrc does not help when secure_path overrides.

One could also create an RPM package that provides /usr/bin/backup (and other files).


How about a ~/bin/subackup script that runs sudo /home/bedna/bin/backup?

For sudo yes, but it was not stated if it was for root as-well.
But I tried changing to root and run without sudo and can confirm /etc/sudoers do override $PATH set in /root/.bashrc as-well.
I guess that is the reason env_keep does not work either.

That would be a script, that runs a script, that runs scripts.
I guess I could symlink but have to do that on every single file I throw in there. The point is I want to be able to throw scripts in ~/bin and if I need to use sudo to run I just do. (I know, I could just use relative path too, but I’m lazy xD)

I’m going to go with creating /etc/sudoers.d/10-secure_path and throw Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/bedna/bin" in there. chmod 440 and it works as I want it to. This is only a rpi I use locally and don’t have to worry too much about security.

Side note, there are strange spaces in /root/.bashrc (and maybe in ~/.bashrc, I don’t remember since I have edited it and might have removed it without thinking) and in /etc/sudoers.
For .bashrc I don’t think it matters, but not so sure about sudoers.

root    ALL=(ALL)       ALL
    ^^^^tab      ^a space followed by tab

Just FYI.

And IMHO,
sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
should be changed to
sudo: [ "ALL=(ALL) ALL" ]
in documentation (cloud-init: /etc/sudoers.d/90-cloud-init-users)
Or at least WARN in the documentation that password checks will be removed if creating that config.
I don’t know/remember what the default config is if not configuring cloud-init manually, I have not tried due to me wanting to change username from almalinux to bedna (and my own password) and saw the documentation recommends removing sudo passwd with NOPASSWD.
I know rpi-os does the same thing for the default pi user, and I’m not a fan of that either, but at least almalinux does not leave a leftover sudoers.d dropin file for the almalinux user if changing name like rpi-os does.

Thank you all for your input.

1 Like