How do I edit an invalid sudoers file? It throws the below error and it's not allowing me to edit again to fix it.
Here is what happens:
$ sudo visudo
>>> /etc/sudoers: syntax error near line 28 <<<
sudo: parse error in /etc/sudoers near line 28
sudo: no valid sudoers sources found, quitting
On a modern Ubuntu system (and many other GNU/Linux distributions), fixing a corrupted
sudoers
file is actually quite easy, and doesn't require rebooting, using a live CD, or physical access to the machine.To do this via SSH, log in to the machine and run the command
pkexec visudo
. If you have physical access to the machine, SSH is unnecessary; just open a Terminal window and run thatpkexec
command.Assuming you (or some other user) are authorized to run programs as
root
with PolicyKit, you can enter your password, and then it will runvisudo
asroot
, and you can fix your/etc/sudoers
.If you need to edit one of the configuration files in
/etc/sudoers.d
(which is uncommon in this situation, but possible), usepkexec visudo -f /etc/sudoers.d/filename
.If you have a related situation where you have to perform additional system administration commands as root to fix the problem (also uncommon in this circumstance, but common in others), you can start an interactive root shell with
pkexec bash
. Generally speaking, any non-graphical command you'd run withsudo
can be run withpkexec
instead.(If there is more than one user account on the system authorized to run programs as
root
with PolicyKit, then for any of those actions, you'll be asked to select which one you want to use, before being asked for your password.)If that doesn't work--for example, if there are no users authorized to run programs as root via PolicyKit--then boot from an Ubuntu live CD (like the CD you probably used to install Ubuntu) and mount the filesystem for the installed system. You can do this by running
sudo parted -l
to view your partitions--there is probably just one ext4 partition, and that's the root filesystem.Suppose the installed Ubuntu system's root filesystem is on
/dev/sda1
. Then you could mount it withsudo mount /dev/sda1 /mnt
. Then you can edit the installed system's sudoers file withsudo nano -w /mnt/etc/sudoers
. Or, even better, you can edit it with(which will prevent you from saving a sudoers file with incorrect syntax).
Always use
visudo
to edit your sudoers file, never edit it directly yourself. It will prevent you saving it to disk unless it validates.Type in:
Then change last line
To:
It should solve your problem.
When this happens to a non-GUI system (your production server, maybe) the
pkexec
fails with this error message:In this situation, using
pkttyagent
can be helpful. If you want to remove a corrupted file insudoers.d
directory, use this:If you want to recover the default
/etc/sudoers
, you can use this gist to copy the default configurations, putting it in a non-root accessed place (e.g. your$HOME
). Then, you can overwrite your sudoers file:NOTE: Using this approach, after running your command, probably your access to the shell will be gone. But I'm sure losing one shell session is much better than losing your server! (According to the manpage, this is the normal behavior: When its services are no longer needed, the process can be killed.)
if anyone else like me didn't have pkexec installed, or was not able to run vi, visudo, nano or any other editor to change sudoers file you can be sure with this process.. I was saved with this:
remount boot device for rw, and apply exec right for user, and edit file
fix that mistake and be happy :)
If you messed up your
sudoers
file, you'll need to:visudo
, fix your filesource :- http://mario.net.au/content/recover-etcsudoers-ubuntu-1204
There is nothing wrong #include sudoer.d removing #include sudoer.d won't make any difference.
But please make sure you don't have any syntax errors. I had same issue but and spent hours to fix and just figured out they are syntax errors. Refer to manual and make them right.
For example Say your username is : dolly I used following which is wrong
correct syntax is
or
hope this helps
For WSL users, accessing a bad
sudoers
is much more straightforward:If you cannot recover the file manually this way, you can reset it to the default installed version (adapted from this answer) with:
Important: This will reset all configuration files associated with
sudo
, including other customizations done in/etc/sudoers.d
.run recovery mode then type this
only the group and user root should have read privelege
then revert your mistakes