Shell 脚本 - Sudo 权限随着时间的推移而丢失
我制作了一个简单的 bash 脚本,需要在整个脚本中保留其超级用户权限。不幸的是,但可以理解的是,当 sleep
发生时,脚本会失去其 sudo
提升的权限。对我来说不好:
sudo echo "I am sudo!" # Asks for passwords
sleep(60)
sudo echo "I am sudo!" # Need to enter password again.
我考虑过用 while 循环替换 sleep
来保持 sudo 的活动,但我很确定有更好的选项可以使 sudo
- 权限在整个脚本中保留?
谢谢
I've made a simple bash script that need to keep it's super-user privileges throughout the script. Unfortunately, but understandable the script looses its sudo
-eleveted permissions when the sleep
occurs. Not good for me:
sudo echo "I am sudo!" # Asks for passwords
sleep(60)
sudo echo "I am sudo!" # Need to enter password again.
I thought about replacing the sleep
with a while-loop that keeps the sudo alive, but I am pretty sure that there's better options available to make the sudo
-permissions stay throughout the script?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
sudo 的灵活性被广泛低估。这会导致非常糟糕的实践(例如 sudo su - 炮弹手术方法)。
更好的方法是专门允许您想要允许的命令,不使用密码:
您可以选择对以特定管理员用户身份运行的特定主机中的特定用户执行此操作。您甚至可以阻止用户将 shell 转义符作为参数传递。您可以使 sudo 阻止启动的程序动态执行更多应用程序等。您将需要阅读 sudoers 的手册页(以及请务必阅读编辑此特殊文件的步骤!)。
这里是一些小东西,(从这里):
The flexibility of sudo is widely under-estimated. This leads to very poor practices (like the
sudo su -
canon-ball surgery method).A much better method is to specificly allow the commands you intend to allow without use of a password:
You can optionally do this for specific users from specific hosts running as specific admin users. You can even prevent users from passing shell escapes as parameters. You can make sudo prevent the launched program to execute further applications dynamically etc. etc. You will want to read the man-page for sudoers (and be sure to read the procedures for editing this special file!).
Here is a small taste of things, (from here):
严格在脚本内工作(而不是编辑 sudoers 文件或通过 sudo ./script.sh 调用脚本),这就是我认为最干净的方法。
基本上,这定义了一对用于启用和禁用 sudo 模式的函数。在运行使用 sudo 的代码之前调用
startsudo
可以使用 sudo 进行身份验证、分叉后台 sudo 刷新循环、保存循环的 PID,并设置信号陷阱以在按下 Ctrl+C 时停止 sudo 模式。调用 stopsudo 会终止循环,清除信号陷阱,并使之前使用 sudo 进行的身份验证无效。将这些函数复制到脚本中后,像这样使用它们。
我要感谢 @karl 内联 sudo 刷新循环的简单性,并感谢 @sehe 指出如果循环没有正常终止,则应使用信号陷阱来终止循环。这两个想法都改进了我的 btrfs 备份脚本 ,它使用 sudo 刷新循环来避免重新提示用户在子卷的备份花费的时间超过 sudo 的超时时间后。
Working strictly within a script (and not editing the sudoers file or calling the script via
sudo ./script.sh
), here's what I think the cleanest method is.Basically, this defines a pair of functions for enabling and disabling sudo mode. Calling
startsudo
before running your sudo-using code authenticates with sudo, forks a background sudo-refreshing loop, saves the loop's PID, and sets a signal trap to stop sudo mode when Ctrl+C is pressed. Callingstopsudo
kills the loop, clears the signal trap, and invalidates the earlier authentication with sudo.After copying these functions into your script, use them like this.
I would like to thank @karl for the simplicity of inlining the sudo-refreshing loop and @sehe for pointing out that a signal trap should be used to kill the loop if it isn't killed normally. Both of these ideas improved my btrfs backup script, which uses a sudo-refreshing loop to avoid re-prompting the user after a subvolume's backup takes longer than sudo's timeout.
您可以通过添加到 /etc/sudoers 来调整此超时
,但运行起来更容易
You can adjust this timeout by adding to /etc/sudoers
But it is much easier to run
这是一个解决方法:
Here's a workaround:
这是我的方式:
This my way:
一次性获得root权限:
Get root privileges once for all: