意外删除了 /etc/sudoers 文件

意外删除了 /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包,所以我尝试重新安装sudopkexec但它不起作用。

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命令再次工作?

答案1

/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

如下所述这个答案,一切都会顺利进行。我可以这么说,因为我刚刚又测试了一次。

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

答案2

只需从 Ubuntu 活动磁盘启动并将/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文件复制到/etc已安装 Ubuntu 分区的目录中。

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

答案3

/etc/sudoers以下是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

以及权限:

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

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

答案4

我只是想添加这个简单的解决方案来重新安装 sudo 如果您使用以下方法删除了 sudo:

sudo apt-get remove sudo

您可以再次安装。使用:

su -

输入你的 root 密码,然后:

apt-get update
apt-get install sudo

出口

相关内容