意外删除/ etc / sudoers文件

尝试更新我的存储库时出现以下错误,

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 

好像我不小心删除了我的/etc/sudoers文件。 /etc/sudoers文件实际上属于sudo包,所以我尝试通过pkexec重新安装sudo但它不起作用。

 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:~$ 

而且我也尝试过,

 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 

我如何让sudo命令再次工作?

我为/etc/sudoers文件做了备份之后:

 sudo mv /etc/sudoers{,.bak} 

我得到了与你的情况相同的错误。

如果你使用

 pkexec apt-get install sudo 

不会起作用,因为apt-get会看到:

 sudo is already the newest version. 

如果您使用:

 pkexec apt-get install --reinstall sudo 

也不会工作,因为找不到/etc/sudoers文件被删除和替换。

但是如果你使用:

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

正如这个答案中所描述的,一切都会像魅力一样。 我可以这样说,因为我只是再次测试它。

因此,没有必要浪费时间并使用实时磁盘启动系统。

只需从Ubuntu live disk启动并将/etc/sudoers文件复制到已安装的Ubuntu分区的/etc目录。

  • 启动Ubuntu live dis并在启动时单击尝试Ubuntu选项。

  • 运行sudo blkid命令以了解已安装的Ubuntu的分区ID。

  • 将已安装的Ubuntu分区挂载到特定目录,如下所示,

     sudo mkdir /media/foo sudo mount /dev/sdaX /media/foo # /dev/sdaX installed Ubuntu's partition id. 
  • 现在将活动磁盘的sudoers文件复制到已安装的Ubuntu分区的/etc目录中。

     sudo cp /etc/sudoers /media/foo/etc 
  • 现在从硬盘启动(启动你的Ubuntu操作系统)。它会工作。

这是Ubuntu 13.10上/etc/sudoers的原始内容:

 # # 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 

和权限:

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

要应用这些,它是chmod 440 /etc/sudoerschown root:root /etc/sudoers

执行pkexec nano /etc/sudoers

并粘贴

 # # 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