If sudo was your only way to access the root account, and you don't have a running root shell somewhere, you'll need to regain access to the root account by external means, typically requiring physical access to the machine. The most common way is to select “single user mode” or “recovery mode” at a boot prompt (you may need to perform some action such as pressing and holding Shift to make the boot prompt appear). How to do this depends on the bootloader is set up, which different distributions do differently, so look for documentation or tutorials related to your distribution.
If you aren't able to reach a root prompt in single user mode, you can get into an even more stripped down mode by adding
init=/bin/sh to the kernel command line. You'll get to a root shell but you need to run a few commands to get a usable environment:
mount -t proc proc /proc
mount -o remount,rw /
You can now edit the problematic file.
If you want multiple terminals in single user mode, you can open a new shell on another virtual console with the command
openvt. Use Ctrl+Alt+F1, Ctrl+Alt+F1, etc. to switch between virtual consoles.
sudoers directly. Use
visudo. This command invokes an editor, then checks the syntax of the file, and aborts the modification if there's a syntax error. You can still lock yourself out, for example by removing the line that granted you access, but it does reduce the risk.
Ubuntu uses nano as the default editor. Many other distributions use vi by tradition. To pick a different editor, set the
VISUAL environment variable, e.g.
You can use the directory
/var/tmp as a temporary storage area if you want to make a copy of the file that you can read back after rebooting in normal mode. Make sure to make the file readable to your normal account.
cp /etc/sudoers /var/tmp
chmod a+r /var/tmp/sudoers
Tip: whenever you make changes that could affect your ability to access an account, keep a running shell in one window, and test your access in another window. If you aren't getting access, switch back to that running shell and fix the problem or revert the change immediately.