在 WSL Ubuntu 中无法将所有者更改回 root(无法使用 sudo 命令)

在 WSL Ubuntu 中无法将所有者更改回 root(无法使用 sudo 命令)

当我想使用sudo命令时遇到了这个问题:

sudo: /etc/sudoers is owned by uid 1000, should be 0
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

然后我尝试了类似问题的解决方案,以便将所有者改回根:

$ pkexec visudo

我也尝试过这个

$ pkexec chown root:root /etc/sudoers /etc/sudoers.d -R

但是在使用这些命令时我遇到了另一个问题:

Error getting authority: Error initializing authority: Could not connect: No such file or directory

那么这个问题有解决办法吗?


完整 shell 问题片段:

ubuntu@LAPTOP-D4T16P7J:~$ sudo -i
sudo: /etc/sudoers is owned by uid 1000, should be 0
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
ubuntu@LAPTOP-D4T16P7J:~$ pkexec visudo
Error getting authority: Error initializing authority: Could not connect: No such file or directory
ubuntu@LAPTOP-D4T16P7J:~$ pkexec chown root:root /etc/sudoers /etc/sudoers.d -R
Error getting authority: Error initializing authority: Could not connect: No such file or directory


这是我的 /etc/sudoers 文件:

ubuntu@LAPTOP-D4T16P7J:~$ cat /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:/snap/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


操作系统信息:

  • Ubuntu WSL2。

答案1

答复如下:

  • 通过在 Windows 命令行(CMD)中发出此命令来访问 root 用户:

    wsl -u root
    
  • 将 /etc/sudoers 文件的所有权更改为 root:

    chown root:root /etc/sudoers
    


谢谢@Terrance帮助他。

答案2

我也遇到过类似的问题。

使用wsl -u root导致错误。

打开cmd,运行wsl -l获取发行版名称。

在我的例子中输出是Ubuntu-18.04

跑步wsl -d Ubuntu-18.04 -u root

您现在应该正在使用 root。

跑步chmod 440 /etc/sudoers

相关内容