这是预期的行为吗?
是的,确实这是一种预期的行为 . 用户对sudo的缓存凭据负责 .
可配置吗?
是的,它是可配置的 .
我认为您的安全问题是有效的 . 如果脚本是使用显式 sudo 命令编写的,则在之前运行 sudo 命令的终端(在某个超时内)运行 script.sh 将为脚本提供超级用户权限 .
通过运行sudo,可以避免任何脚本在以sudo身份运行时不提示输入密码:
sudo -k script.sh
无论以前的sudo命令或会话如何,它都会要求输入密码 .
并且运行script.sh而不使用sudo,即只使用script.sh并仍然提示输入sudo命令的密码:
You can change the timeout value (the duration sudo maintains the session) permanently:
跑 sudo visudo
然后换行:
Defaults env_reset
至
Defaults env_reset,timestamp_timeout=0
保存并退出(ctrl X然后Y)
这将确保sudo每次运行时都要求输入密码 .
Or If you don't want to change it permanently and want your script to prompt for password at least once (while maintaining a session), then you can change your script like this:
sudo -k first-command-with-sudo
sudo second-command
sudo third
and so on
无论以前的sudo命令或会话如何,此脚本都将至少提示输入一次密码 .
In case you are unaware of (or don't have access to) the content of the script script.sh (it can have sudo commands inside it or not)
并且您希望确保任何sudo命令肯定会提示输入密码至少一次,然后在运行脚本之前运行 sudo -K (大写K) .
现在如果你运行 script.sh 并且它包含一个sudo命令,它肯定会提示输入密码 .