5

I get the following error while trying to update my repositories,

avinash@avinash-VirtualBox:~$ sudo apt-get update
sudo: unable to stat /etc/sudoers: No such file or directory
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

It seems like I had accidentally deleted my /etc/sudoers file./etc/sudoers file was actually belongs to sudo package, so i try to reinstall sudo by pkexec but it won't work.

avinash@avinash-VirtualBox:~$ pkexec apt-get install sudo
Reading package lists... Done
Building dependency tree       
Reading state information... Done
sudo is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 301 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? y
Setting up sudo (1.8.6p3-0ubuntu3.1) ...
WARNING:  /etc/sudoers not present!
chown: cannot access ‘/etc/sudoers’: No such file or directory
dpkg: error processing sudo (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 sudo
E: Sub-process /usr/bin/dpkg returned an error code (1)
avinash@avinash-VirtualBox:~$ 

And also I had tried,

avinash@avinash-VirtualBox:~$ pkexec apt-get install --reinstall sudo
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 301 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
E: Internal Error, No file name for sudo:amd64

How can i make sudo command to work again?

Avinash Raj
  • 78,556
  • Why this post got downvotes? – Avinash Raj Mar 23 '14 at 16:47
  • Good question though... – MrVaykadji Mar 23 '14 at 16:50
  • 1
    Any idea what (applications!, or something else like a malware!) deleted the file? Please edit your question to include that too. This would not only help us understand your problem better, but also help us all linuxers stay safe from such accidents.. big accident actually.. (it's just a guess but there could be some of those rare linux malware! (and a real dangerous one) in your system, have you been experiencing other similar problems lately? please share..) – rusty Mar 23 '14 at 18:19

5 Answers5

8

After I made a backup for /etc/sudoers file:

sudo mv /etc/sudoers{,.bak}

I get the same errors like in your case.

If you use

pkexec apt-get install sudo

will not work because apt-get will see that:

sudo is already the newest version.

If you use:

pkexec apt-get install --reinstall sudo

will also not work because /etc/sudoers file is not found to be removed and replaced.

But if you use:

pkexec apt-get purge sudo
pkexec apt-get install sudo

as described in this answer, everything will work like a charm. I can say this because I just test it again.

So, there is no point to lose time and boot your system with a live disk.

Radu Rădeanu
  • 169,590
  • 1
    +1 for pkexec - You can also just re-add yourself with pkexec to /etc/sudoers. I just copied the default sodoers file from another answer in this thread. – Derek Adair Mar 03 '15 at 22:00
  • After this, my /etc/sudoers is still empty – Black Mar 20 '17 at 07:38
  • 1
    CAUTION! apt-get purge sudo also uninstalled several other very important packages with it for me, including my desktop environment and display manager (Commandline: /usr/bin/apt-get purge sudo Purge: casper:amd64 (1.340.2), cinnamon:amd64 (2.6.13+rafaela), sudo:amd64 (1.8.9p5-1ubuntu1.3), ubuntu-minimal:amd64 (1.325), lupin-casper:amd64 (0.55), mdm:amd64 (2.0.4+rafaela), gdebi:amd64 (0.9.5.3ubuntu2), gksu:amd64 (2.0.2-6ubuntu2)). I spent 5 hours trying to get to my desktop again. I recommend NOT doing this – Bloke Mar 30 '17 at 12:32
4

Just boot from Ubuntu live disk and copy /etc/sudoers file to the installed Ubuntu partition's /etc directory.

  • Boot Ubuntu live dis and click try Ubuntu option on startup.

  • Run sudo blkid command to know the installed Ubuntu's partition id.

  • Mount the installed Ubuntu's partition on a specific directory like below,

    sudo mkdir /media/foo
    sudo mount /dev/sdaX /media/foo     # /dev/sdaX installed Ubuntu's partition id.
    
  • Now copy the live disk's sudoers file to the /etc directory of installed Ubuntu's partition.

    sudo cp /etc/sudoers /media/foo/etc
    
  • Now boot from the hard-disk(Boot your Ubuntu OS).It will work.

Avinash Raj
  • 78,556
4

Here's the raw content of /etc/sudoers on Ubuntu 13.10 :

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

And the permissions :

-r--r----- 1 root root

To apply those, it's chmod 440 /etc/sudoers and chown root:root /etc/sudoers

MrVaykadji
  • 5,875
  • 2
  • 32
  • 55
  • How would i place this file on /etc? It needs sudo. – Avinash Raj Mar 23 '14 at 17:09
  • With the root account, the real one, not your usual user with sudo privileges. It's accessible through Recovery I think. – MrVaykadji Mar 23 '14 at 17:12
  • @AvinashRaj you are right, that would need sudo. So what you'd have to do is boot to a live linux session, and create the file (of course with root privilege) and place the contents (suggested in MrVaykadji's answer) in the file, save it, close the editor, set the right permissions, and reboot.. this should fix a part of the problem (which I think is of a larger domain).. – rusty Mar 23 '14 at 18:40
  • 1
    @AvinashRaj You can place files in /etc and change permissions using... guess what?...pkexec. – Radu Rădeanu Mar 24 '14 at 09:48
2

execute pkexec nano /etc/sudoers

and paste

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
Kerem Er
  • 21
  • 1
1

I just wanted to add this simple solution to reinstall sudo If you removed sudo with:

sudo apt-get remove sudo

You can install it again. Use:

su -

Enter your root password and then:

apt-get update
apt-get install sudo

exit

muru
  • 197,895
  • 55
  • 485
  • 740
  • Nice alternative method, however I will say make sure a root password is set (passwd root) before doing this or you'll be unable to su - to the root account (by default on Ubuntu the root account does not have a password so you wouldn't be able login to it directly) (although you can still get root access from recovery mode). – Daniel M. May 22 '20 at 18:58
  • I think you saved me several hours! If you have a backup and just want to copy it to /etc/sudoers, just use su - – Shinigami Mar 06 '24 at 16:33