在 WSL 上忘记了 Linux 密码？下面是如何轻松重设的方法
WSL 密码
对于那些想从舒适的 Windows 中享受 Linux 命令行的人来说，WSL（Windows Subsystem for Linux） 是一个方便的工具。


当你 [在 Windows 上使用 WSL 安装 Linux](https://itsfoss.com/install-bash-on-windows/) 时，会要求你创建一个用户名和密码。当你在 WSL 上启动 Linux 时，这个用户会自动登录。


现在的问题是，如果你有一段时间没有使用它，你可能会忘记 WSL 的账户密码。而如果你要使用 `sudo` 的命令，这将成为一个问题，因为这里你需要输入密码。


![](/data/attachment/album/202107/03/155824ua3xfpzpal7irnx8.png)


不要担心。你可以很容易地重置它。


### 在 Ubuntu 或任何其他 Linux 发行版上重置遗忘的 WSL 密码


要在 WSL 中重设 Linux 密码，你需要：


* 将默认用户切换为 `root`
* 重置普通用户的密码
* 将默认用户切换回普通用户


让我向你展示详细的步骤和截图。


#### 步骤 1：将默认用户切换为 root


记下你的普通/常规用户名将是明智之举。如你所见，我的普通帐户的用户名是 `abhishek`。


![Note down the account username](/data/attachment/album/202107/03/155825mflz1lj0efrh2zd2.png)


WSL 中的 `root` 用户是无锁的，没有设置密码。这意味着你可以切换到 `root` 用户，然后利用 `root` 的能力来重置密码。


由于你不记得帐户密码，切换到 `root` 用户是通过改变你的 Linux WSL 应用的配置，使其默认使用 `root` 用户来完成。


这是通过 Windows 命令提示符完成的，你需要知道你的 Linux 发行版需要运行哪个命令。


这个信息通常在 [Windows 商店](https://www.microsoft.com/en-us/store/apps/windows) 中的发行版应用的描述中提供。这是你首次下载发行版的地方。


![Know the command to run for your distribution app](/data/attachment/album/202107/03/155825ps2ftt6fftjkus22.png)


从 Windows 菜单中，启动命令提示符：


![Start Command Prompt](/data/attachment/album/202107/03/155826way5ywdcrhy5m7wt.jpg)


在这里，以这种方式使用你的发行版的命令。如果你使用的是 Windows 商店中的 Ubuntu 应用，那么该命令将是：



```
ubuntu config --default-user root

```

截图中，我正在使用 Windows 商店中的 Ubuntu 20.04 应用。所以，我使用了 ubuntu2004 命令。


![Set root as default user in Linux app’s configuration](/data/attachment/album/202107/03/155826ujerby8j4y8rmhfh.png)


为了减少你的麻烦，我在这个表格中列出了一些发行版和它们各自的命令：




| 发行版应用 | Windows 命令 |
| --- | --- |
| Ubuntu | `ubuntu config –default-user root` |
| Ubuntu 20.04 | `ubuntu2004 config –default-user root` |
| Ubuntu 18.04 | `ubuntu1804 config –default-user root` |
| Debian | `debian config –default-user root` |
| Kali Linux | `kali config –default-user root` |


#### 步骤 2：重设帐户密码


现在，如果你启动 Linux 发行程序，你应该以 `root` 身份登录。你可以重新设置普通用户帐户的密码。


你还记得 WSL 中的用户名吗？（LCTT 译注：请使用你的“用户名”替换下列命令中的 `username`）如果没有，你可以随时检查 `/home` 目录的内容。当你有了用户名后，使用这个命令：



```
passwd username

```

它将要求你输入一个新的密码。\*\*当你输入时，屏幕上将不会显示任何内容。这很正常。只要输入新的密码，然后点击回车就可以了。\*\*你必须重新输入新的密码来确认，当你输入密码时，屏幕上也不会显示任何东西。


![Reset the password for the regular user](/data/attachment/album/202107/03/155827cpspiqpv7oo9q8po.png)


恭喜你。用户账户的密码已经被重置。但你还没有完成。默认用户仍然是 `root`。你应该把它改回你的普通用户帐户，否则它将一直以 `root` 用户的身份登录。


#### 步骤 3：再次将普通用户设置为默认用户


你需要你在上一步中用 [passwd 命令](https://linuxhandbook.com/passwd-command/) 使用的普通帐户用户名。


再次启动 Windows 命令提示符。**使用你的发行版命令**，方式与第 1 步中类似。然而，这一次，用普通用户代替 `root`。



```
ubuntu config --default-user username

```

![Set regular user as default user](/data/attachment/album/202107/03/155827g1tub4lunnt49yjb.png)


现在，当你在 WSL 中启动你的 Linux 发行版时，你将以普通用户的身份登录。你已经重新设置了密码，可以用它来运行 `sudo` 命令。


如果你将来再次忘记了密码，你知道重置密码的步骤。


### 如果重设 WSL 密码如此简单，这难道不是一种安全风险吗？


并非如此。你需要有对计算机的物理访问权以及对 Windows 帐户的访问权。如果有人已经有这么多的访问权，他/她可以做很多事情，而不仅仅是改变 WSL 中的 Linux 密码。


### 你是否能够重新设置 WSL 密码？


我给了你命令并解释了步骤。我希望这对你有帮助，并能够在 WSL 中重置你的 Linux 发行版的密码。


如果你仍然遇到问题，或者你对这个话题有疑问，请随时在评论区提问。




---


via: <https://itsfoss.com/reset-linux-password-wsl/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Reset Forgotten Linux Password on WSL

Forgot the password of your Linux distribution app you installed in Windows via WSL? No worries. You can easily reset the Linux password in WSL.

WSL (Windows Subsystem for Linux) is a handy tool for people who want to enjoy the power of Linux command line from the comfort of Windows.

When you [install Linux using WSL on Windows](https://itsfoss.com/install-bash-on-windows/), you are asked to create a username and password. This user is automatically logged on when you start Linux on WSL.

Now, the problem is that if you haven’t used it for some time, you may forget the account password of WSL. And this will become a problem if you have to use a command with sudo because here you’ll need to enter the password.

![reset wsl password](https://itsfoss.com/content/images/wordpress/2021/06/reset-wsl-password.png)

Don’t worry. You can easily reset it.

## Reset forgotten password for Ubuntu or any other Linux distribution on WSL

To reset the Linux password in WSL, you have to:

- Switch the default user to root
- Reset the password for the normal user
- Switch back the default user to the normal user

Let me show you the steps in detail and with screenshots. If you want a video, you can watch that as well.

### Step 1: Switch to root as default user

It will be wise to note down your account’s normal/regular username. As you can see, my regular account’s username is abhishek.

![username wsl](https://itsfoss.com/content/images/wordpress/2021/06/username-wsl-800x296.png)

The root user in WSL is unlocked and doesn’t have a password set. This means that you can switch to the root user and then use the power of root to reset the password.

Since you don’t remember the account password, switching to the root user is done by changing the configuration of your Linux WSL application and make it use root user by default.

This is done through Windows Command Prompt and you’ll need to know which command you need to run for your Linux distribution.

This information is usually provided in the description of the distribution app in the [Windows Store](https://www.microsoft.com/en-us/store/apps/windows). This is from where you had downloaded your distribution in the first place.

![wsl distro command](https://itsfoss.com/content/images/wordpress/2021/06/wsl-distro-command-800x602.png)

From the Windows menu, start the command prompt:

![Start Command Prompt in windows](https://itsfoss.com/content/images/wordpress/2021/06/start-cmd-windows-800x500.jpg)

In here, use your distribution’s command in this fashion. If you were using the Ubuntu app from Windows store, the command would be:

`ubuntu config --default-user root`


In the screenshot, I am using Ubuntu 20.04 app from the Windows store. So, I have used `ubuntu2004`

command.

![wsl set root as default](https://itsfoss.com/content/images/wordpress/2021/06/wsl-set-root-as-default-800x288.png)

To save you the trouble, I am listing some distributions and their respective commands in this table:

Distribution App | Windows Command |
---|---|

Ubuntu | ubuntu config –default-user root |

Ubuntu 20.04 | ubuntu2004 config –default-user root |

Ubuntu 18.04 | ubuntu1804 config –default-user root |

Debian | debian config –default-user root |

Kali Linux | kali config –default-user root |


### Step 2: Reset the password for the account

Now, if you start the Linux distribution app, you should be logged in as root. You can reset the password for the normal user account.

Do you remember the username in WSL? If not, you can always check the contents of the /home directory. When you have the username, use this command:

`passwd username`


It will ask you to enter a new password. **When you type here, nothing will be displayed on the screen. That’s normal. Just type the new password and hit enter.** You’ll have to retype the new password to confirm and once again, nothing will be displayed on the screen while you type the password.

![resetting wsl password](https://itsfoss.com/content/images/wordpress/2021/06/resetting-wsl-password-800x366.png)

Congratulations. The password for the user account has been reset. But you are done just yet. The default user is still root. You should change it back to your regular account user, otherwise, it will keep on logging in as the root user.

### Step 3: Set regular user as default again

You’ll need the regular account username that you used with the [passwd command](https://linuxhandbook.com/passwd-command/) in the previous step.

Start the Windows command prompt once again. **Use your distribution’s command** in the similar manner you did in step 1. However, this time, replace root with the regular user.

`ubuntu config --default-user username`


![set regular user as default wsl](https://itsfoss.com/content/images/wordpress/2021/06/set-regular-user-as-default-wsl-800x288.png)

Now when you start your Linux distribution app in WSL, you’ll be logged in as a regular user. You have reset the password fresh and can use it to run commands with sudo.

If you forgot the password again in the future, you know the steps to reset it.

## If resetting WSL password is this easy, is this not a security risk?

Not really. You need physical access to the computer and access to the Windows account. If someone already has this much access, they can do much more than just change the Linux password in WSL.

## Were you able to reset WSL password?

I gave you the commands and explained the steps. I hope this was helpful to you and you were able to reset the password of your Linux distribution in WSL.

If you are still facing issues or if you have a question on this topic, please feel free to ask in the comment section.