Lost sudo somehow, here's how to fix it

I received the dreaded <username> is not in the sudoers file. This incident will be reported error. Somehow my user was removed from /etc/sudoers.

This is how I fixed it.

  • Reboot your computer and when you see the prompt to hit <esc> for the boot menu do it.
  • Press the number which represents your boot drive (usually 1)
  • You will be taken to a Grub menu, highlight the entry for PureOS and press e to edit.
  • Find the line that starts with linux and somewhere where it says ro quiet, add single in there so that it looks like ro single quiet and at the end of that line add init=/bin/bash (these changes will not persist)
  • Press Ctrl-X to boot into single user mode.
  • You will be at a bash prompt but the filesystem is read only so remount it read-write with: mount -o remount,rw /
  • Type visudo to edit /etc/sudoers and add this entry: <username> ALL=(ALL:ALL) ALL
  • Reboot your computer and sudo will have been restored.

I can’t speak specifically for PureOS or librem laptops but the generic approach that I would take for this is:

  • plug in LiveBoot media for any Linux distro that has one
  • after power on, press whatever Fx key brings up the boot disk menu
  • boot the LiveBoot media and choose to “try” (rather than choosing to “install”)
  • that most likely won’t have mounted the normal boot drive, so click it to mount

Then you can edit / repair / replace whatever file you need to do something with, and you have a fairly normal GUI environment in which to do it.

Things might be more complicated if the normal boot drive is doing full disk encryption.