无法使用sudo命令(Ubuntu 16.04.1 LTS)
当我在终端上键入sudo时,我得到:
sudo: unable to stat /etc/sudoers: Permission denied sudo: no valid sudoers sources found, quitting sudo: unable to initialize policy plugin
更多信息:
ls -ld / /etc /etc/sudoers drwxr-xr-x 24 root root 4096 Dez 21 22:44 / drw-rw-rx 162 root root 12288 Jan 18 15:13 /etc -r--r----- 1 root root 746 Jan 18 14:21 /etc/sudoers
pkexec visudo
:
Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # 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
运行pkexec chmod 755 /etc
来解决这个问题。
正如steeldriver建议的那样 ,这个错误是因为/etc
有错误的权限。 ls -ld /etc
显示的权限应如下所示:
drwxr-xr-x
但你的看起来像:
drw-rw-rx
这样可以防止sudo
遍历/etc
访问/etc/sudoers
。
只是尝试在文件上检索元数据(即,stat)的“权限被拒绝”错误消息几乎总是意味着文件丢失或者无法遍历导致它的目录,并且当您在文件上运行ls
时,它显示为存在。
我已经通过使用chmod ug-x
破坏/etc
上的权限来测试(在虚拟机上),并且能够生成您的确切错误消息。 (你不应该使用ug-x
运行chmod
– 这就是我故意破坏权限的方式,而不是如何解决它们。)
要修复/etc
上的权限 ,请为用户和组所有者提供可执行权限。 缺少这些权限是导致此问题的原因。 此外,默认情况下/etc
不需要为组所有者提供写入权限,因此除非您知道自己想要这个,否则我建议您更改它。
此命令将/etc
重置为默认权限:
pkexec chmod 755 /etc
这应该适合您,因为您可以使用pkexec
以root身份运行其他命令。 通过搜索找到这个的其他用户通常能够以相同的方式修复它,但是对于没有pkexec
系统(或pkexec
无法工作的地方),可能需要启动到恢复模式或从实时CD / DVD / USB到修复权限。 但是,您不应该这样做。 只需运行一个简单的命令, sudo
可以再次运行了。