Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I edit an invalid visudo file?

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
share|improve this question

10 Answers

up vote 40 down vote accepted
+50

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. Assuming you (or some other user) are authorized to run programs as root with PolicyKit, you can enter your password, and then it will run visudo as root, and you can fix your /etc/sudoers.

(If there is more than one user account on the system authorized to run programs as root with PolicyKit, then 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 with sudo mount /dev/sda1 /mnt. Then you can edit the installed system's sudoers file with sudo nano -w /mnt/etc/sudoers. Or, even better, you can edit it with sudo visudo -f /mnt/etc/sudoers (which will prevent you from saving a sudoers file with incorrect syntax).

share|improve this answer
No other way without LiveCD? Lets say my PC is in remote location. – YumYumYum Oct 30 '11 at 20:59
Its in another country. And i did mistake via SSH. How do you insert liveCD? – YumYumYum Oct 30 '11 at 20:59
@Google I think you might be able to elevate privileges to root with PolicyKit rather than sudo, to fix the problem remotely. I'll test this to see if it works, and if it does, I'll edit my answer to add detailed instructions for this. In the mean time, I recommend you edit your answer to add this information (about how you need to be able to fix the problem by SSHing in). – Eliah Kagan Nov 12 '11 at 0:38
@Google I've verified that you can use PolicyKit (via the pkexec command) for this, and I've edited my answer to explain how. – Eliah Kagan Nov 12 '11 at 1:00
Is PolicyKit a standard command? I tried it and it says it is not installed (I needed to apt-get it, which I can't because of a messed up sudoers file). – acme Jan 18 '12 at 11:51
show 4 more comments

Always use visudo to edit your sudoers file, never edit it directly yourself. It will prevent you saving it to disk unless it validates.

share|improve this answer

run recovery mode then type this

chown -R root:root /etc/sudoers.d
chmod u=rwx,g=rx,o=rx /etc/sudoers.d/
chmod u=r,g=r,o= /etc/sudoers.d/*

only the group and user root should have read privelege

share|improve this answer

I found the bug. It is in the last two lines of the default /etc/sudoers file. Remove the following entries from the end of the /etc/sudoers file with visudo.

#includedir /etc/sudoers.d
share|improve this answer
1  
That seems like a related, but different situation from this one. In this situation, the problem isn't that there are bad files (or wrong permissions) in /etc/sudoers.d, but that /etc/sudoers was itself corrupt. – Eliah Kagan May 29 '12 at 6:32

check permissions of root system /. it must be owned by root und use 755, than find command of sudo will find the config.

share|improve this answer

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

 dolly ALL = (ALL) ALL NO PASSWD: ALL

correct syntax is

dolly ALL = (ALL) ALL //give permission to everything, not good

or

dolly ALL=(ALL) NOPASSWD:/usr/bin/thurderbird //good, give specific permission

hope this helps

share|improve this answer
A better approach than making sure you don't have syntax errors is to always use visudo when editing these files, which makes sure you don't have syntax errors for you, before it modifies file. visudo is not just for editing /etc/sudoers--it will also create and edit files in /etc/sudoers.d. It will also work with whatever text editor you want. See the manpage for details. – Eliah Kagan Jul 3 '12 at 0:12
As for giving specific permission, please note that this is only useful for very simple commands/apps, because any sufficiently complex app (including thunderbird, which should never be run as root anyway) will effectively give the user full system access when run as root. Even seemingly simple functionality opens the door to full root access. For example, a user who can run a program that can save a file to an arbitrary location as root can gain full root access (they can install their own /etc/sudoers, or if syntax limitations prevent that, they can install their own /etc/crontab). – Eliah Kagan Jul 3 '12 at 0:16

If u messed up your sudoers file.You'll need to:

  • Reboot into recovery mode (hit escape during boot, choose the recovery mode option on the grub screen)
  • Choose the 'Enable networking' option (if you don't your filesystem will be mounted as read-only. who knew)
  • Chosee the 'Drop to root shell' option
  • run visudo, fix your file
  • Reboot with normal grub option

source :- http://mario.net.au/content/recover-etcsudoers-ubuntu-1204

share|improve this answer

You can edit your boot entry while in grub as well.

Simply reboot your pc, and wait for grub to show. Then press "e" on the "Ubuntu" entry to edit it.

Look for a line with "linux = " or "kernel = " and simply add an "single" to the end of that line.

Then press F10 to boot this temporarily modified boot entry. This will give you a shell (without GUI) with root rights and you can edit the sudoers file with s.th. like nano /etc/sudoers back to its previous state.

Then reboot and its done.

share|improve this answer

Type in:

pkexec visudo

Then change last line

includedir /etc/sudoers

To:

includedir /etc/sudoers.d

It should solve your problem.

share|improve this answer
I've noticed that removing the leading # from #includedir causes syntax errors, the # is part of the directive, at least on Ubuntu 12.10. – SAFX Apr 5 at 2:46

You can also login as root on a tty console with Ctrl+Fn (Fn from 1 to 6) and run visudo.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.