When I type a sudo command into the terminal it shows the following error:
sudo: /etc/sudoers is owned by uid 1000, should be 0
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
How do I fix this?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityChange the owner back to root:
pkexec chown root:root /etc/sudoers /etc/sudoers.d -R
Or use the visudo
command to ensure general correctness of the files:
pkexec visudo
Error executing command as another user: Not authorized
Mar 15, 2022 at 21:00
Another option, in the case that one doesn't have the password for root
or ubuntu
users. I've fat-fingered ownership (more times than I want to admit) and ending up doing this:
sudo chown -R owner:group /
instead of this:
sudo chown -R owner:group .
This has almost always been in the context of a Vagrant-managed VirtualBox VM running Ubuntu headless, so YMMV. I'd never had a good fix until now, but this seems to do the trick easyishly.
Repair
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
chown -R root:root /etc/sudoers.d
chown root:root /etc/sudoers
chmod 440 /etc/sudoers
exit 0
After the fix is in place the commands in /etc/rc.local can be removed.
to recover from
sudo chown myuser:myuser /etc/sudoers
chmod u+w /etc/sudoers
chmod u-w /etc/sudoers
sudo chown root:root /etc/sudoers
The last of which results in the "sudo: /etc/sudoers is owned by uid 1000, should be 0", etc. errors. I tried to su - sudo which I've seen suggested but I don't think the root password was ever set so that did not work.¹
To fix this issue, I rebooted, dropped into a root shell and²
chown root:root /etc/sudoers
passwd root #for good measure, e.g., so su - root would work in the future!
Rebooted, voila.
NB: The pkexec commands suggested did not work for me while I was initially trying to fix the problem. After it was fixed via the recovery mode root shell, I subsequently tried it and a GUI window popped up asking for my password and it did work so YMMV.
¹ After fixing the problem, I repeated the steps and was able to recover with²
su - root
chown root:root /etc/sudoers
² The list of commands Rohlt suggests were unnecessary in my case but they might apply in other cases.
In my case, I was running Windows Subsystem for Linux(WSL2). I had created a folder using VSCode(running in windows) and opened it for creating more files in it.
After closing VSCode(which kept running in background), I tried deleting the created folder from WSL2 terminal and got the above error.
The solution was to terminate instance of VSCode fully(or restart system) as it was keeping the folder opened.
if you have set and have the root password, first run the following command
$ su - root
it will ask for the root password and then run following commands one by one
chown root:root /etc/sudoers
chmod 440 /etc/sudoers
chown -R root:root /etc/sudoers.d
chmod 755 /etc/sudoers.d
chmod 440 /etc/sudoers.d/*
pkexec
should work, if the only damage is to sudo
's config.