意外删除/ 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/sudoers
和chown 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