Recover from “Username is not in the sudoers file. This incident will be reported” on WSL2
I tried to improve WSL2 memory consumption improvement. I found a workaround on this issue.
However, through the process, I mistook. I can’t use sudo
command, anymore. How to fix it? Since I can’t be root
without sudo
How to fix it?
Solution
WSL2 have a command to manage it. Start command prompt on Windows10.
> wsl --user root
Now you can login the WSL2 as a root
. The issue was happened, since I modified /etc/sudoers
file with visudo
command. The problem is, by default, visudo
command start nano
editor that I’m not familiar with. It has two option for save DOS
and MAC
. I though. No. I’m using Linux
How can I choose the one. Mac is closer to Linux, so I choose it. These process adds special character on that file. I confuse a lot about it. Even if I correctly configured it, it still emit error for each line except for comment lines. There is no clue.
NOTE: My friend find a Stack Exchange post about this topic.
root@DESKTOP-SDE49UK:/etc/sudoers.d# visudo
>>> /etc/sudoers: syntax error near line 7 <<<
>>> /etc/sudoers: syntax error near line 8 <<<
>>> /etc/sudoers: syntax error near line 9 <<<
>>> /etc/sudoers: syntax error near line 10 <<<
>>> /etc/sudoers: syntax error near line 12 <<<
>>> /etc/sudoers: syntax error near line 14 <<<
>>> /etc/sudoers: syntax error near line 16 <<<
>>> /etc/sudoers: syntax error near line 18 <<<
>>> /etc/sudoers: syntax error near line 19 <<<
>>> /etc/sudoers: syntax error near line 20 <<<
>>> /etc/sudoers: syntax error near line 22 <<<
>>> /etc/sudoers: syntax error near line 23 <<<
>>> /etc/sudoers: syntax error near line 25 <<<
>>> /etc/sudoers: syntax error near line 26 <<<
>>> /etc/sudoers: syntax error near line 28 <<<
>>> /etc/sudoers: syntax error near line 30 <<<
You can use this command to make sure there is no special character
$ cat -A /etc/sudoers
Then, convert it to unix format file to fix the issue of /etc/sudoers
.
$ apt install dos2unix
$ dos2unix /etc/sudoers
/etc/sudoers
is in charge of manage sudo
configuration. It should be edited by visudo
. Since this operation could be very dangerous. Once mis configured, I can’t login anymore. So that we use visudo
. It helps us to check the modify is correct or not. I recommend to change it from nano to vi. So, before modifying it you can do
$ update-alternatives --config editor
You can choose vim now.
Then
$ visudo
You can successfully modify it. For the grammar of visudo
You can refer this page:
FYI: This is the default /etc/sudoers
file on WSL2 (18.04, 20.04). You might want to rollback to this.
Finally, you can use this command to reboot
$ wsl --shutdown
Then restart your WSL2. You will find you can use sudo
again.
Resources
Linux and character encoding (Japanese)