使用 sudo 登录后运行 bash Whittail 脚本

使用 sudo 登录后运行 bash Whittail 脚本

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

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

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

我还添加了user1使用sudoersvisudo

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

我已更改/etc/init/tty1.conf为自动登录该用户:

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

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

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

答案1

如果/scripts/whiptail3_config.sh没有a sudo,那么它就没有权限。您可以让脚本检查uid它在其下运行并对其自身进行 sudo 。

就像是

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

相关内容