运行 bash 使用 sudo 登录后的 whiptail 脚本
Run bash whiptail script after login with sudo
我正在使用 whiptail 创建一个虚拟机配置脚本,我希望它在用户登录后自动启动。问题是,我不希望用户成为 root,但是因为用户将需要能够更改主机名、IP 地址、添加目录等内容。他们需要 sudo 权限才能让脚本真正完成它的工作。
到目前为止我所做的是创建一个使用我的 whiptail shell 脚本的用户:
adduser -M -s /scripts/whiptail_config.sh user1
我还使用 visudo
:
添加了 user1
到 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...
消息。
我想做的事情可行吗?是否有我没有考虑过但应该考虑的替代方案?谢谢!
你在哪里使用 sudo?无处。
运行 直接来自 inittab 的脚本。
如果 /scripts/whiptail3_config.sh
不执行 sudo,那么它将没有权限。您可以让脚本检查它所在的 uid 运行 并对其自身进行 sudo。
类似
#!/bin/bash
[ $UID != 0 ] && exec sudo [=10=] "$@"
# the rest of the script...
我正在使用 whiptail 创建一个虚拟机配置脚本,我希望它在用户登录后自动启动。问题是,我不希望用户成为 root,但是因为用户将需要能够更改主机名、IP 地址、添加目录等内容。他们需要 sudo 权限才能让脚本真正完成它的工作。
到目前为止我所做的是创建一个使用我的 whiptail shell 脚本的用户:
adduser -M -s /scripts/whiptail_config.sh user1
我还使用 visudo
:
user1
到 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...
消息。
我想做的事情可行吗?是否有我没有考虑过但应该考虑的替代方案?谢谢!
你在哪里使用 sudo?无处。 运行 直接来自 inittab 的脚本。
如果 /scripts/whiptail3_config.sh
不执行 sudo,那么它将没有权限。您可以让脚本检查它所在的 uid 运行 并对其自身进行 sudo。
类似
#!/bin/bash
[ $UID != 0 ] && exec sudo [=10=] "$@"
# the rest of the script...