sudo - 意外删除了/etc/sudoers 文件

分享于 

7分钟阅读

ubuntu

  繁體

问题

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


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软件包,所以我尝试通过sudo重新安装pkexec,但它无法工作。


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 live磁盘启动,并将/etc/sudoers文件复制到已安装的Ubuntu分区的/etc目录。

  • 启动Ubuntu live 磁盘,并在启动时点击尝试Ubuntu选项。

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

  • 将已安装的Ubuntu分区挂载到一个特定的目录,

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

    
    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

    执行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
    
    
    
    


    文件  acc  DEL  SUDO