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?)
$ 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:
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.
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.
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.