14

So, I've messed up my sudoers file in the windows subsystem for linux. I was trying to add something, and the guy of the tutorial said to edit sudoers file with nano/vim, and that's what I did (later I discovered that you should only edit it with visudo, nice). When I saved, I realized I couldn't use "sudo" anymore, the terminal would complain saying:

>>> /etc/sudoers: syntax error near line 33 <<<
sudo: parse error in /etc/sudoers near line 33
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

I've read a bunch of articles saying that I would have to run:

pkexec /usr/sbin/visudo

But the result was always the same:

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

I tried everything, spent like two hours reading stuff and couldn't do anything. Can anyone help me? As you can see, I'm clearly new when it comes to real linux.

Augusto Lessa
  • 141
  • 1
  • 1
  • 3

3 Answers3

47

I found quite a simple solution for my case.

Just open Windows console (cmd), then:

wsl -u root visudo

And fix the file.

Note that visudo does not catch all errors. To avoid getting locked out of sudo, keep a root shell (sudo -s) or visudo open until after you've thoroughly tested your new config.

xiota
  • 5,038
Rovny Art
  • 571
2

On my side because the command wsl -u root visudo did not worked, I used the following alternative:

I changed the default user to be root by following the instruction in this answer:

ubuntu config --default-user root

then restart LxssManager service (from services.msc)

When I open the WSL, by default is connect as root and I can safely execute the command visudo from that WSL instance.

I corrected the bad entry and saved it.

My WSL was starting fine after the correction.

Just in case, here is the default sudoers file I had in the beginning:

#
# 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

%sudo ALL=NOPASSWD: /sbin/start-stop-daemon %sudo ALL=NOPASSWD: /etc/init.d/ssh

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

It's possible that some entries have been added by "apt install" command.

don't forget to switch back to the original user after the operation:

ubuntu1804.exe config --default-user <username>

The command ubuntu1804.exe can be adapted to your distro (20.04, others linux, etc...)

0

Close the WSL terminal, open Windows command prompt and make the default WSL user as root using the below command

ubuntu2004 config --default-user root

Open a new WSL terminal, you will be logged in as root. Type

nano /etc/sudoers

Now you can edit the sudoers file. Cheers!

Extra: ubuntu1804 config --default-user root for Ubuntu 18.04 and above or ubuntu config --default-user root for versions below.

Kulfy
  • 18,154