如何对父目录在 $PATH 中的 bash 脚本执行 sudo?
How to sudo a bash script whose parent dir is in the $PATH?
例如
~/Desktop/scripts
在 $PATH
cat ~/Desktop/scripts/hi
#!/bin/bash
echo hi
我试过的(当前目录是~):
hi # CLI said "hi"
sudo -E hi # sudo: hi: command not found
se hi # sudo: hi: command not found # alias se="sudo -E "
如何sudo脚本?
尝试以下操作:
sudo PATH="${PATH}" bash -c "hi"
解释请看man sudoers(5):
By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The new environment contains the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables in addition to variables from the invoking process permitted by the env_check and env_keep options. This is effectively a whitelist for environment variables.
例如
~/Desktop/scripts
在 $PATH
cat ~/Desktop/scripts/hi
#!/bin/bash
echo hi
我试过的(当前目录是~):
hi # CLI said "hi"
sudo -E hi # sudo: hi: command not found
se hi # sudo: hi: command not found # alias se="sudo -E "
如何sudo脚本?
尝试以下操作:
sudo PATH="${PATH}" bash -c "hi"
解释请看man sudoers(5):
By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The new environment contains the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables in addition to variables from the invoking process permitted by the env_check and env_keep options. This is effectively a whitelist for environment variables.