0

I have no backup.

I'm incredibly unknowledgeable, so if anyone could give a beginners guide to this issue that would be amazing

this is the error that is displayed when using any sudo commands :

sudo: unable to stat /etc/sudoers: No such file or directory
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

I've seen people suggest using this command to solve the issue :

pkexec apt-get install sudo

But when I do this I get the following response

 Error initializing authority: Could not connect: No such file or directory   
Nathan
  • 1
  • 1
    It may not be the most efficient way, but you could boot to a live usb, mount the device's system partition, and copy the file from the live file system to the real one. I don't know how that will affect your user being a sudoer, however. – Organic Marble May 27 '22 at 18:10

2 Answers2

4

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.

ArrayBolt3
  • 3,129
2

Another way go to ubuntu packages search look for package sudo (in your case) for your release and download it (with wget or in your browser) and save it in your $HOME. Boot up in recovery and gain a rootshell so it works, purge the old sudo package, and install the one you downloaded with dpkg.

When in recovery mode, you will have to remount your drive as read/write, since it will be mounted read-only by default. To do this, enter a rootshell from within recovery mode, then run mount -o remount,rw / to get read/write access.

ArrayBolt3
  • 3,129
nobody
  • 5,437