gpt4 book ai didi

linux - 使用 sudo 登录后运行 bash whiptail 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:22 26 4
gpt4 key购买 nike

我正在使用 whiptail 创建一个虚拟机配置脚本,我希望它在用户登录后自动启动。问题是,我不希望用户成为 root,但是因为用户会需要能够更改主机名、IP 地址、添加目录等内容。他们需要 sudo 权限才能让脚本真正完成它的工作。

到目前为止我所做的是创建一个使用我的 whiptail shell 脚本的用户:

adduser -M -s /scripts/whiptail_config.sh user1

我还使用 visudouser1 添加到 sudoers:

user1 ALL=(root) NOPASSWD: /scripts/whiptail3_config.sh

我已经更改了 /etc/init/tty1.conf 以自动让该用户登录:

exec /sbin/getty -8 38400 tty1 -a user1 

到目前为止,我的 whiptail_config.sh shell 脚本在启动和强制登录后加载正常。但是,脚本中任何实际需要 sudo 访问权限的内容都会出错,并显示 Permission denied... 消息。

我想做的事情可行吗?是否有我没有考虑过但应该考虑的替代方案?谢谢!

最佳答案

如果 /scripts/whiptail3_config.sh 不执行 sudo,那么它将没有权限。您可以让脚本检查其运行所在的 uid,并对自身执行 sudo。

有点像

#!/bin/bash
[ $UID != 0 ] && exec sudo $0 "$@"
# the rest of the script...

关于linux - 使用 sudo 登录后运行 bash whiptail 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38858782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com