Unable to stat /etc/sudoers: no such file or directory
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.
Related videos on Youtube
Cassie
Updated on September 18, 2022Comments
-
Cassie over 1 year
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.
-
Admin about 7 yearsYou 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. -
Admin about 7 yearsCan you try to reinstall the
sudo
package by runningpkexec apt-get install --reinstall sudo
? -
Admin about 7 years@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
-
Admin about 7 yearsYou 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? -
Admin about 7 years@ByteCommander I was careless and I gave 777 rights to sudoers.
-
-
Cassie about 7 yearsI 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)
-
Ravexina about 7 years@Cassie I added some new instruction for when apt does not works... ;)
-
Cassie about 7 yearsI 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.
-
Ravexina about 7 years@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
. -
Cassie about 7 yearsThank you for help. But it still does not work. When I ran visudo and just save and exit, I get this
visudo: /etc/sudoers.d is world writable
-
Ravexina about 7 yearsso it's not only your
sudoers
file, it seems that you messed aroundsoudoers.d
directory too. runls -ld /etc/sudoers.d
let me know what is the output. -
Cassie about 7 yearsYou are right, there is a problem with sudoers permissions. The output of it is:
d------rwx 2 root root 4096
-
Ravexina about 7 yearsUpdated my answer, check the shortcut part ;) hope it solve the problem.
-
Cassie about 7 yearsThank you for your help and patience. I did what you wrote and
ls -ld /etc/sudoers.d
now gives this resultdrwxr-xr-x 2 root root 4096
. But my sudo does not work anyway :( – -
Ravexina about 7 yearsLet us continue this discussion in chat.
-
Byte Commander about 7 yearsWhy do you enable the root account? Just perform your steps from the recovery root shell you booted into, or use
pkexec
as replacement forsudo
. There is absolutely no need to mess with the real root account. -
Ravexina about 7 years@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. -
Youssef Moawad almost 5 yearsMounting the file system with read/write permissions fixed it for me (managed to edit the sudoers file from the root shell). Thanks!