Use sudo without password INSIDE a script

前端 未结 8 1959
礼貌的吻别
礼貌的吻别 2020-11-29 03:29

For some reason I need, as user, to run without sudo a script script.sh which needs root privileges to work.
I saw as the only solution to put sudo INSIDE script.sh. Let

8条回答
  •  心在旅途
    2020-11-29 03:47

    As you noted, the file that must appear in the sudoers configuration is the one that is launched by sudo, and not the one that runs sudo.

    That being said, what we often do, is having something like

    user ALL=(ALL:ALL) NOPASSWD:/path/to/script.sh
    

    in the sudo configuration, where script.sh has all the commands that the script has to do.

    Then we define either a Bash function or an alias so that script.sh is actually

    sudo /path/to/script.sh
    

    The only issue is if some commands must not be run as root, you need to insert some su - user -c "command" commands in the script.

提交回复
热议问题