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?
Boot the system into single mode and then edit it.
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.
As noted in this similar question on askubuntu, this may well work without having to reboot into single-user mode:
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.
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:
$ docker run -it --rm -v '/etc/:/root/host.etc/' debian bash
apt-get update && apt-get install [vim|nano|whatever]