6

I was using Ubuntu 14.04 only recently. And somehow I changed permission to /etc/sudoers file. So every time I run any command using sudo I get these errors:

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 was able to login as a user and change permissions back. So now when I run ls -l /etc/sudoers command I get:

-r--r----- 1 root root 755 жов 14  2016 /etc/sudoers

But I was not able to make root an owner. And I still get the same errors, when I try to user sudo.

I have read a lot of scary things about this problem. So could you help me to resolve this problem? I would be very grateful for advice.

  • You still get the "No such file" error although the file is there? The permissions and ownership is also correct as you show it in your ls -l output. – Byte Commander Apr 16 '17 at 20:32
  • Can you try to reinstall the sudo package by running pkexec apt-get install --reinstall sudo? – Byte Commander Apr 16 '17 at 20:35
  • @ByteCommander When I run exactly this command I get these data: /usr/bin/mandb: the setuid man user "man" does not exist Processing triggers for ureadahead (0.100.0-16) ... ureadahead will be reprofiled on next reboot Setting up javascript-common (11) ... dpkg: error processing package javascript-common (--configure): subprocess installed post-installation script returned error exit status 1 Setting up sudo (1.8.9p5-1ubuntu1.3) ... Errors were encountered while processing – Cassie Apr 16 '17 at 20:44
  • 1
    You could try that, but it will likely also remove a bunch of other packages depending on sudo with it, which you would have to reinstall later as well. I also doubt that it would help with the error you describe. Can you maybe give a bit more detail on how you broke your system/what happened before this all? – Byte Commander Apr 16 '17 at 21:08
  • @ByteCommander I was careless and I gave 777 rights to sudoers. – Cassie Apr 17 '17 at 7:40
6

Shortcut:

dpkg-reconfigure: If only something is wrong with the permission of /etc/sudoers file, with root access run: dpkg-reconfigure sudo. something like pkexec dpkg-reconfigure sudo should do the work.

visudo: If dpkg-reconfigure does not works for you, then just run pkexec visudo, the sudoers file will be opened in an editor, then just save and close the file, visudo will fix the permission for you.

After all, if you changed sudoers.d directory permissions too, then run:

pkexec chmod 755 /etc/sudoers.d

If files are there, and their permissions are as it should be but you still get this error message, the only possibility that come into my mind is that you have broken other permissions and sudo does not have access to /etc/sudoers. the most possible guess is permissions of /etc/.

So run:

pkexec chmod 755 /etc

We are done, however if you want a detailed instruction to restore broken or removed sudoers file with messed up permissions follow along, we are going to use another way to fix things.


Introduction

I messed around my /etc/sudoers file to get a similar error as you. I get really close, so I completely delete mine and it's became exactly as yours.

What I've did to fix the issue:

My root account was locked, and It didn't have any password, so it was impossible for me to do what I wanted to do. however if your root account is active and has a password, switch to root user with su - and skip this part. if not, follow the instructions.

Before going further I have to mention that You can always use a live disk to do a chroot into your system, there are a lot of instruction about chroot using a live disk, so I'm not going to bother with giving all those instruction again, instead I'm going to assume we don't have any live disk and we are going to use what we already have, to fix this issue. If you are happy with cheroot go that way, that would do the work too.


Getting root access

  • Reboot your system
  • When grub appears press e to edit it
  • At the end of the line which starts with linux, add init=/bin/bash
  • Press CTRL+X

Now you will be dropped into a bash shell with root access. first remount the file system with write access:

mount -o remount,rw /

Then unlock the root user and set a password on it:

passwd -u root
passwd root

Now If your /etc/sudoers still exists on /etc, move it somewhere else:

mv /etc/sudoers /etc/sudoers.broken

Because we are going to tell our package manager that this file is missing, and it's going to replace it for us with a correct one, so it shouldn't be there.

We are almost done, reboot your system. login as your normal user, open a terminal then try su - to switch into root user. I wasn't able to do this, so what I've done was using CTRL+ALT+F1, to go into tty1, then I logged in as root with the new password we just set.


Bring sudoers back

We simply should reinstall the sudo package, however reinstalling does not bring this file back, we should tell the dpkg to fix the missing config files too, like this:

apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall sudo

Remember that it will asks you about installing the /etc/sudoers file, by inserting y say yes.

If it did not work for you, do as following as root:

mkdir /root/tmp
cd /root/tmp
apt-get download sudo
dpkg-deb -x sudo*.deb .
cp etc/sudoers /etc/sudoers
cd /root
rm -r tmp

Now there may be some different between permissions, run dpkg-reconfigure sudo to fix it. if it did not work, run visudo as root user, the sudoers file will be opened in an editor, then just save and close the file, visudo will fix the permission for you.

If everything goes right, your sudoers file will be back and you will be able to use sudo as you were before.

  • I tried to bring sudoers back. But I get weird errors with Javascript-common: Setting up javascript-common (11) ... dpkg: error processing package javascript-common (--configure): subprocess installed post-installation script returned error exit status 1 Setting up sudo (1.8.9p5-1ubuntu1.3) ... Errors were encountered while processing: javascript-common E: Sub-process /usr/bin/dpkg returned an error code (1) – Cassie Apr 17 '17 at 7:40
  • @Cassie I added some new instruction for when apt does not works... ;) – Ravexina Apr 17 '17 at 7:52
  • I tried it, but my sudoers still does not work. Maybe, there is a problem with configuration? Because when I typed dpkg-reconfigure sudo I got no output. – Cassie Apr 17 '17 at 8:26
  • @Cassie I did another update, have a look and make sure you are not missing anything... after that. if it's still not working, let me know what is the output of sudo ls /home. – Ravexina Apr 17 '17 at 8:29
  • 1
    @ByteCommander My root account had password and I didn't knew it. so I wasn't able to use root recovery shell, also I didn't had pkexec installed. as I mentioned, that section is for when neither of recommended instructions works. I'll clean up instruction a little bit more... any suggestions to make the answer more helpful are welcome :) thanks. – Ravexina Apr 17 '17 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.