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

<p>Assuming I have a command that would work on a normal file, how could I apply it to <code>/etc/sudoers</code>?</p>

<p>Can I copy and modify it, then have <code>visudo</code> replace the original with the modified copy? By providing my own script in <code>$EDITOR</code>?</p>

<p>Or can I just use the same locks and <code>cp</code>?</p>

<p>The question is more about potential issues than about just finding something that works.</p>

You should make your edits to a temporary file, then use visudo -c -f sudoers.temp to confirm that the changes are valid and then copy it over the top of /etc/sudoers
```
```
#!/bin/sh
if [ -f "/etc/sudoers.tmp" ]; then
    exit 1
fi
touch /etc/sudoers.tmp
edit_sudoers /tmp/sudoers.new
visudo -c -f /tmp/sudoers.new
if [ "$?" -eq "0" ]; then
    cp /tmp/sudoers.new /etc/sudoers
fi
rm /etc/sudoers.tmp
```
```