如何配置 visudo 以便非 root 用户以 root 权限运行脚本?

如何配置 visudo 以便非 root 用户以 root 权限运行脚本?

我需要部署者(部署组成员)能够以 root 权限运行一些脚本。但我不希望部署者可以运行任何东西。我尝试了这个。

使用 root 用户执行以下操作:

~$ echo "touch /opt/expe/\$file_name.txt" > create_file.sh
~$ echo "chown \$USER /opt/expe/\$file_name.txt" >> create_file.sh
~$ cat create_file.sh
touch /opt/expe/$file_name.txt
chown $USER /opt/expe/$file_name.txt

~$ sudo mv create_file.sh /opt/expe/create_file.sh
~$ sudo chgrp deploy /opt/expe/create_file.sh
~$ sudo chmod g+x /opt/expe/create_file.sh
~$ sudo visudo 
%deploy ALL=(root) NOPASSWD:/opt/expe/create_file.sh

使用非 root 用户部署我执行以下操作:

~$ groups
deploy
~$ /opt/expe/create_file.sh
touch: cannot touch '/opt/expe/my_text.txt': Permission denied
chown: cannot access '/opt/expe/my_text.txt': No such file or directory

为什么 touch 命令没有以 root 身份执行?

如何编写一个脚本,让非 root 用户以 root 身份执行命令?

参考文献:

相关内容