Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was editing my /etc/sudoers file and saved it but it has a syntax error in there. Now I can't 'sudo vim /etc/sudoers' because sudo thinks the sudoers file is not valid.

How can I fix this?

share|improve this question
See – belacqua May 22 '14 at 22:32
up vote 4 down vote accepted

Boot the system into single mode and then edit it.

share|improve this answer
Thanks, my Ubuntu is running under Virtualbox. How do I boot into single user mode? – fred basset Nov 27 '10 at 22:41
I don't get the grub options at boot time however, it boots straight into Ubuntu... – fred basset Nov 27 '10 at 23:04
Try holding <Ctrl> or something. – Ignacio Vazquez-Abrams Nov 27 '10 at 23:05
@red basset: As the vm starts press Esc several times that should drop you into grub then select the recovery mode option. – Iain Nov 27 '10 at 23:13

You should never edit your sudoers file directly. Use visudo - it will protect you from these syntax errors in the future.

To recover from this situation, either boot into single-user mode and edit the file or boot off of a livecd and do the same.

share|improve this answer
Thanks, I was wondering what visudo did differently. I need to work out now how to boot into single user mode. My Ubuntu is running under Virtualbox. – fred basset Nov 27 '10 at 22:43
visudo causes you to edit a copy of the sudoers file. It will only replace the sudoers file if it validates when you save it. It will use the editor specified by your EDITOR variable, so you can use your prefered editor. It can also be used in check mode if you edited the file and want to be sure it is still valid. – BillThor Nov 28 '10 at 0:13

As noted in this similar question on askubuntu, this may well work without having to reboot into single-user mode:

pkexec visudo
share|improve this answer
This is the by far the easiest way to do it, if it's available. – belacqua May 22 '14 at 22:33

By the way, if you have the root password, just login with it and set it back the way it was.

chmod 0440 /etc/sudoers

This will avoid you from booting into single-user mode or worst, booting from a livecd.

In my case I couldn't afford to bring the server down.

share|improve this answer

If you have Docker installed and your user is a member of the docker group you can easily get yourself out of this pickle (and some others) without rebooting or what-have-you:

  1. $ docker run -it --rm -v '/etc/:/root/host.etc/' debian bash
  2. apt-get update && apt-get install [vim|nano|whatever]
  3. cd ~/host.etc/
  4. Repair the the problem files
  5. exit
share|improve this answer
Docker? Have you seen Docker anywhere in the original question? – Deer Hunter Jul 22 '15 at 7:24
As the question doesn't specify anything about the asker's environment and it is now 2015, meaning quite a few people have Docker installed, I understand neither your attitude toward me nor your contempt for my contribution. It is not at all unfathomable that someone else in this situation would have software installed that, as of today, has been starred by 23.5k people on GitHub. If pkexec is also for some reason unavailable then this provides an alternate method, without rebooting, that is almost as quick and simple. – Adrian Günter Jul 23 '15 at 21:17

Your Answer


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.