How do I edit /etc/sudoers from a script?

后端 未结 12 812
花落未央
花落未央 2020-12-04 05:43

I need to edit /etc/sudoers from a script to add/remove stuff from white lists.

Assuming I have a command that would work on a normal file, how could I

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 06:42

    Just to add a further option to the answers above, if the race condition is not a major concern, then the following command can be used to avoid manually copying a modified file to /etc/sudoers

    sudo EDITOR="cp /tmp/sudoers.new" visudo
    

    This will ensure that the new file is validated and installed correctly with permissions update.

    Note that if there is an error in the /tmp/sudoers.new file then visudo will prompt for user input so it is advisable to check it with visudo -c -f /tmp/sudoers.new first.

提交回复
热议问题