Another benefit of using visudo -f
as mentioned in some answers is there is a corresponding option -c
or --check
that verifies you don't have invalid information in a sudoers.d file or any other file you might want to put in sudoers.d. This is REALLY handy in the cases mentioned with automated tools, as you can run e.g.
sudo visudo -c -q -f /home/myuser/mysudoers && \
sudo chmod 600 /home/myuser/mysudoers && \
sudo cp /home/myuser/mysudoers /etc/sudoers.d/mysudoers
This silently checks the file (no output due to -q) and only if that does NOT return an error (exit 1 means an invalid sudoers file) then it will copy the file into sudoers.d, this way you can create the file and work on it without having to make it right the first time (using sudo visudo -f /etc/sudoers.d/myfile
has to succeed or it discards the content if you don't fix it).
Also, a word of caution regarding these statements from the other answers.
It has been my experience that the rules on files in this directory are looser than for /etc/sudoers. This has included:
Mistakes in the file did not cause sudo to fail. However, the file was ignored.
Permission rules appear less strict. I allow the applicable group to read the file. I don't believe that is possible with /etc/sudo.
Files in /etc/sudoers.d are required to adhere to the same syntax as /etc/sudoers, because under the covers the system simply concatenates together all the files with the last one in "winning" if there are multiple entries for the same singular setting.
If the permissions are horribly incorrect (world writable) on files in /etc/sudoers.d/ then they are ignored, this may be what caused the invalid files to get overlooked, otherwise you can seriously break the sudo
command by having an invalid sudoers.d file with correct permissions.
You can allow the sudoers files to be world readable, if you accidentally allow 'other' the write permission you need to sudo as another user or from a root terminal run this command. This can also break if the file is owned by someone other than root:root.
sudo chmod 644 /etc/sudoers.d/mysudoers
sudo chown root:root /etc/sudoers.d/mysudoers
I just confirmed that if I run chmod o+w /etc/sudoers.d/vagrant
I can no longer sudo as the vagrant user, it prompts me for my password and then fails.
When I ran sudo -l
to view the applied command permissions as another valid sudo user, I also received a warning about the file permissions. This is the same command I used to confirm that the 'vagrant' user lost sudo when I applied o+w
permissions to the file giving that user sudo permissions.