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.
visudo -fsafe? – aob Jan 25 '15 at 06:41visudo -fedits in a safe fashion just like normalvisudo. That is why we havevisudoand why it provides the-foption. – John1024 Jan 25 '15 at 06:44-fas this could byte someone who then copies it into /etc/sudoers.d/ without double checking them. – dragon788 Dec 08 '16 at 22:02/etc/sudoers.d/mysudorules.sudoand couldn't for the life of me figure out why the rules inside weren't applied before reading your answer. – Zaroth Apr 26 '18 at 10:11/sudoers.d/mysite.co.ukwasn't working and after renaming tomysiteit works! :) – J86 Oct 26 '18 at 22:39visudo -f /etc/sudoers.d/.../etc/sudoersis not in any way taken into account. Cansudostop working, even thoughvisudofinished successfully,? I mean, on their own the files are okay, but together... I should point out here that I have a very basic understanding of what is possible with the files. – x-yuri Aug 11 '20 at 16:32#includedir /etc/sudoers.din this , why#is not treated ascommented line– Arun Apr 17 '21 at 23:40man sudoers, however, the rule is that#starts a comment unless the#is immediately followed by eitherincludeorincludedir. The authors ofsudoprobably chose this approach because they were familiar withCand the C preprocessor also has a#includedirective. – John1024 Apr 19 '21 at 20:13mukesh-sudo.confbecause I saw another.conffile in the/etc/sudoers.dfolder. Thanks for the exception block - I removed the.confto get it working. – mukesh.kumar Apr 05 '23 at 10:13.txtextension on my file thats why it was not working lol – tbrodbeck Jan 21 '24 at 17:21