MacOS – `sudoers` file resets with every macOS update

macos

I had updated my sudoers file to make some command run with root privileges by default without the need to use sudo and entering my password. I used the command sudo visudo and added this line to it:

<my_username> ALL=(ALL) NOPASSWD: <path/to/command --arguments>

But whenever I update macOS, my sudoers file is reset to its defaults. A folder named Relocated Items appears on my Desktop and it always includes the following file: /Users/Shared/Relocated Items/Configuration/private/etc/sudoers. This happens on macOS Catalina, and also on macOS Big Sur.

Screen shot from Relocated Items folder

How can I prevent the sudoers file resetting to its defaults with every macOS update?

Best Answer

Modifying most of the files in /etc leaves you open to them being replaced during OS updates. That's why most of the ones you usually need to change have the facility to add files to a directory instead.

If you look in /etc you will see a directory called sudoers.d and the last line of the sudoers file is #includedir /private/etc/sudoers.d so any file of commands you put into the directory will be read. Files in the directory will not be overwritten when you update.

You may notice directories with similar names in /etc that fill the same function for other parts of the system.