I'd purge and then reinstall sudo. There's a couple of ways to do this. One is to boot into a live USB and recover the sudoers file from there. The other is to boot into recovery mode and manually reconstruct the file. The live USB option is much easier, but the other option will work if you don't have access to a live USB (which may be the case if you got rid of your live USB and this system is the only one you have).
First, the live USB method. Insert a live USB drive with a matching version of Ubuntu as the one on your computer. For instance, if you're on Ubuntu 20.04, boot an Ubuntu 20.04 live USB. Next, open a terminal with Ctrl+Alt+T.
Next, figure out which drive and partition Ubuntu is installed to. To do this, type lsblk
in the terminal. This will show you a list of drives and partitions in your system. You should be able to tell which drive and partition holds Ubuntu based off of disk size. Find the corresponding partition code (it will look something like sda1
or nvme0n1p1
).
Now that you know the partition code of your Ubuntu system, it's time to mount it. To do that, run sudo mount /dev/<partition code here> /mnt
, replacing <partition code here>
with the partition code you found with lsblk
. This will give you access to your Ubuntu system.
Next, run the following command sequence to get a root shell with Internet access within your main Ubuntu partition:
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo mount --bind /dev/pts /mnt/dev/pts
sudo cp /etc/resolv.conf /mnt/etc/resolv.conf
sudo chroot /mnt
Boom, root shell. Now you should be able to do the following command sequence:
export SUDO_FORCE_REMOVE=yes
apt purge sudo
apt install sudo
Once that's done, shut down, remove the live USB, and boot into your system again. If everything went right, you should be back up and running.
If you don't have access to a live USB, you can use recovery mode to let you get root access so you can reinstall sudo and thus recreate the file. To do that, shut the computer all the way down, then turn it back on. The early boot screen should display, and then the screen should go solid black (or you should see it change in some noticeable way). The moment that happens, hit Esc. This should get you into a boot menu. (If Esc isn't working no matter how hard you try, try pressing and holding Shift instead.)
Once you get into the boot menu, select Advanced Options for Ubuntu
, then select the first entry in the list that says (recovery mode)
in it. (If you can't see the words "recovery mode", just try the second option in the list, that should do it.) This should pull up a rather scary-looking menu. From there, select the network
option to enable Internet access, then select the root
option with the arrow keys, press Enter, then drop to a root shell.
Finally, do the following commands:
export SUDO_FORCE_REMOVE=yes
apt purge sudo
apt install sudo
reboot
That should fix the problem.
Final note: Virtual machines are a great way to experiment with your system without possibly damaging your system. If you want to try something that's possibly dangerous, try it in a VM first. Just because everything works in the VM doesn't guarantee that it will work fine on your physical system, but it will help you to catch stuff like this more easily.