The WSL commands below are listed in a format supported by PowerShell or Windows Command Prompt. To run these commands from a Bash / Linux distribution command line, you must replace wsl with wsl.exe. For a full list of commands, run wsl --help. 下面的 WSL 命令以 PowerShell 或 Windows 命令提示符支持的格式列出,。要從 Bash / Linux 發(fā)行版命令行運行這些命令,,必須將 wsl 替換為 wsl.exe ,。有關(guān)命令的完整列表,,請運行 wsl --help ,。
Install
PowerShell
wsl --install
Install WSL and the default Ubuntu distribution of Linux. Learn more. You can also use this command to install additional Linux distributions by running wsl --install <Distribution Name>. For a valid list of distribution names, run wsl --list --online. 安裝 WSL 和默認的 Linux Ubuntu 發(fā)行版,。 了解更多信息。您還可以使用此命令通過運行 wsl --install <Distribution Name> 來安裝其他 Linux 發(fā)行版,。有關(guān)分發(fā)名稱的有效列表,,請運行 wsl --list --online 。
Options include:
--distribution: Specify the Linux distribution to install. You can find available distributions by running wsl --list --online. --distribution :指定要安裝的 Linux 發(fā)行版,。您可以通過運行 wsl --list --online 來查找可用的發(fā)行版,。
--no-launch: Install the Linux distribution but do not launch it automatically. --no-launch :安裝 Linux 發(fā)行版,但不自動啟動它,。
--web-download: Install from an online source rather than using the Microsoft Store. --web-download :從聯(lián)機源安裝,,而不是使用 Microsoft 應(yīng)用商店。
When WSL is not installed options include: 未安裝 WSL 時,,選項包括:
--inbox: Installs WSL using the Windows component instead of using the Microsoft Store. (WSL updates will be received via Windows updates, rather than pushed out as-available via the store). --inbox :使用 Windows 組件而不是Microsoft存儲安裝 WSL,。(WSL 更新將通過 Windows 更新接收,而不是通過應(yīng)用商店推送為可用),。
--enable-wsl1: Enables WSL 1 during the install of the Microsoft Store version of WSL by also enabling the "Windows Subsystem for Linux" optional component. --enable-wsl1 :在安裝Microsoft存儲版本的 WSL 期間啟用 WSL 1,,方法是同時啟用“Windows Subsystem for Linux”可選組件。
--no-distribution: Do not install a distribution when installing WSL. --no-distribution :安裝 WSL 時不要安裝分發(fā),。
Note
If you running WSL on Windows 10 or an older version, you may need to include the -d flag with the --install command to specify a distribution: wsl --install -d <distribution name>. 如果在 Windows 10 或舊版本上運行 WSL,,則可能需要在 --install 命令中包含 -d 標志以指定分配: wsl --install -d <distribution name> 。
List available Linux distributions 列出可用的 Linux 發(fā)行版
PowerShell
wsl --list --online
See a list of the Linux distributions available through the online store. This command can also be entered as: wsl -l -o. 查看通過在線商店提供的 Linux 發(fā)行版列表,。此命令也可以輸入為: wsl -l -o ,。
List installed Linux distributions 列出已安裝的 Linux 發(fā)行版
PowerShell
wsl --list --verbose
See a list of the Linux distributions installed on your Windows machine, including the state (whether the distribution is running or stopped) and the version of WSL running the distribution (WSL 1 or WSL 2). Comparing WSL 1 and WSL 2. This command can also be entered as: wsl -l -v. Additional options that can be used with the list command include: --all to list all distributions, --running to list only distributions that are currently running, or --quiet to only show distribution names. 查看 Windows 計算機上安裝的 Linux 發(fā)行版的列表,包括狀態(tài)(分發(fā)是正在運行還是已停止)和運行該發(fā)行版的 WSL 版本(WSL 1 或 WSL 2),。 比較 WSL 1 和 WSL 2 .此命令也可以輸入為: wsl -l -v ,。可與 list 命令一起使用的其他選項包括: --all 列出所有分配,, --running 僅列出當前正在運行的分配,,或 --quiet 僅顯示分配名稱。
To designate the version of WSL (1 or 2) that a Linux distribution is running on, replace <distribution name> with the name of the distribution and replace <versionNumber> with 1 or 2. Comparing WSL 1 and WSL 2. WSL 2 is only available in Windows 11 or Windows 10, Version 1903, Build 18362 or later. 若要指定運行 Linux 分發(fā)版的 WSL(1 或 2)版本,,請將 <distribution name> 替換為分發(fā)版的名稱,,并將 <versionNumber> 替換為 1 或 2。 比較 WSL 1 和 WSL 2 .WSL 2 僅在 Windows 11 或 Windows 10 版本 1903,、內(nèi)部版本 18362 或更高版本中可用,。
Set default WSL version 設(shè)置默認 WSL 版本
PowerShell
wsl --set-default-version <Version>
To set a default version of WSL 1 or WSL 2, replacing <Version> with either the number 1 or 2 to represent which version of WSL you would like the installation to default on for new Linux distribution installations. For example, wsl --set-default-version 2. Comparing WSL 1 and WSL 2. WSL 2 is only available in Windows 11 or Windows 10, Version 1903, Build 18362 or later. 若要設(shè)置 WSL 1 或 WSL 2 的默認版本,請將 <Version> 替換為數(shù)字 1 或 2,,以表示你希望新 Linux 分發(fā)版安裝默認安裝的 WSL 版本,。例如, wsl --set-default-version 2 . 比較 WSL 1 和 WSL 2 .WSL 2 僅在 Windows 11 或 Windows 10 版本 1903,、內(nèi)部版本 18362 或更高版本中可用,。
Set default Linux distribution 設(shè)置默認的 Linux 發(fā)行版
PowerShell
wsl --set-default <Distribution Name>
To set the default Linux distribution that WSL commands will use to run, replace <Distribution Name> with the name of your preferred Linux distribution. 若要設(shè)置 WSL 命令將用于運行的默認 Linux 分發(fā)版,,請將 <Distribution Name> 替換為首選 Linux 分發(fā)版的名稱。
Change directory to home 將目錄更改為主頁
PowerShell
wsl ~
The ~ can be used with wsl to start in the user's home directory. To jump from any directory back to home from within a WSL command prompt, you can use the command: cd ~. ~ 可以與 wsl 一起使用,,以在用戶的主目錄中啟動,。若要從 WSL 命令提示符內(nèi)從任何目錄跳轉(zhuǎn)到主目錄,可以使用以下命令: cd ~ ,。
Run a specific Linux distribution from PowerShell or CMD 從PowerShell或CMD運行特定的Linux發(fā)行版
To run a specific Linux distribution with a specific user, replace <Distribution Name> with the name of your preferred Linux distribution (ie. Debian) and <User Name> with the name of an existing user (ie. root). If the user doesn't exist in the WSL distribution, you will receive an error. To print the current user name, use the command whoami. 若要與特定用戶一起運行特定的 Linux 發(fā)行版,,請將 <Distribution Name> 替換為首選 Linux 發(fā)行版的名稱(即。Debian) 和 <User Name> 與現(xiàn)有用戶(即 root)的名稱,。如果用戶在 WSL 分發(fā)中不存在,,你將收到錯誤。要打印當前用戶名,,請使用命令 whoami ,。
Update WSL
PowerShell
wsl --update
Update your WSL version to the latest version. Options include: 將 WSL 版本更新到最新版本。選項包括:
--web-download: Download the latest update from the GitHub rather than the Microsoft Store. --web-download :從 GitHub 而不是Microsoft存儲下載最新更新,。
Check WSL status 檢查 WSL 狀態(tài)
PowerShell
wsl --status
See general information about your WSL configuration, such as default distribution type, default distribution, and kernel version. 請參閱有關(guān) WSL 配置的常規(guī)信息,,例如默認分發(fā)類型、默認分發(fā)和內(nèi)核版本,。
Check WSL version 檢查 WSL 版本
PowerShell
wsl --version
Check the version information about WSL and its components. 檢查有關(guān) WSL 及其組件的版本信息,。
Help command
PowerShell
wsl --help
See a list of options and commands available with WSL. 請參閱 WSL 可用的選項和命令列表。
Run as a specific user 以特定用戶身份運行
PowerShell
wsl -u <Username>`, `wsl --user <Username>
To run WSL as a specified user, replace <Username> with the name of a user that exists in the WSL distribution. 若要以指定用戶身份運行 WSL,,請將 <Username> 替換為 WSL 分發(fā)中存在的用戶的名稱,。
Change the default user for a distribution 更改分配的默認用戶
Change the default user for your distribution log-in. The user has to already exist inside the distribution in order to become the default user. 更改分發(fā)登錄的默認用戶。用戶必須已經(jīng)存在于發(fā)行版中才能成為默認用戶,。
For example:
ubuntu config --default-user johndoe would change the default user for the Ubuntu distribution to the "johndoe" user. 例如: ubuntu config --default-user johndoe 會將 Ubuntu 發(fā)行版的默認用戶更改為“johndoe”用戶,。
Note
If you are having trouble figuring out the name of your distribution, use the command wsl -l. 如果您在確定分配的名稱時遇到問題,請使用命令 wsl -l ,。
Warning
This command will not work for imported distributions, because these distributions do not have an executable launcher. You can instead change the default user for imported distributions using the /etc/wsl.conf file. See the Automount options in the Advanced Settings Configuration doc. 此命令不適用于導入的發(fā)行版,,因為這些發(fā)行版沒有可執(zhí)行啟動器。您可以改為使用 /etc/wsl.conf 文件更改導入分配的默認用戶,。請參閱高級設(shè)置配置文檔中的自動掛載選項,。
Shutdown
PowerShell
wsl --shutdown
Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as changing memory usage limits or making a change to your .wslconfig file. 立即終止所有正在運行的分發(fā)和 WSL 2 輕型實用工具虛擬機。在需要重新啟動 WSL 2 虛擬機環(huán)境(例如更改內(nèi)存使用限制或更改 .wslconfig 文件)的情況下,,可能需要此命令,。
Terminate
PowerShell
wsl --terminate <Distribution Name>
To terminate the specified distribution, or stop it from running, replace <Distribution Name> with the name of the targeted distribution. 若要終止指定的分配或停止其運行,請將 <Distribution Name> 替換為目標分配的名稱,。
Identify IP address 識別 IP 地址
wsl hostname -i for the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address) wsl hostname -i 表示通過 WSL 2 安裝的 Linux 分發(fā)版的 IP 地址(WSL 2 VM 地址)
cat /etc/resolv.conf for the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM) cat /etc/resolv.conf 表示從 WSL 2(WSL 2 VM)看到的 Windows 計算機的 IP 地址
Imports and exports the specified tar file as a new distribution. The filename can be - for standard input. Options include: 將指定的 tar 文件作為新分發(fā)導入和導出,。對于標準輸入,文件名可以是 -。選項包括:
--vhd: Specifies the import/export distribution should be a .vhdx file instead of a tar file --vhd :指定導入/導出分發(fā)應(yīng)為 .vhdx 文件而不是 tar 文件
--version: For import only, specifies whether to import the distribution as a WSL 1 or WSL 2 distribution --version :僅用于導入,,指定是將分發(fā)版作為 WSL 1 還是 WSL 2 版
Imports the specified .vhdx file as a new distribution. The virtual hard disk must be formatted in the ext4 filesystem type. 將指定的 .vhdx 文件作為新分發(fā)導入,。虛擬硬盤必須格式化為 ext4 文件系統(tǒng)類型。
Unregister or uninstall a Linux distribution 取消注冊或卸載 Linux 發(fā)行版
While Linux distributions can be installed through the Microsoft Store, they can't be uninstalled through the store. 雖然可以通過Microsoft商店安裝 Linux 發(fā)行版,,但無法通過商店卸載它們。
To unregister and uninstall a WSL distribution: 若要注銷和卸載 WSL 分發(fā),,請執(zhí)行以下操作:
PowerShell
wsl --unregister <DistributionName>
Replacing <DistributionName> with the name of your targeted Linux distribution will unregister that distribution from WSL so it can be reinstalled or cleaned up. Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. Reinstalling from the store will install a clean copy of the distribution. For example, wsl --unregister Ubuntu would remove Ubuntu from the distributions available in WSL. Running wsl --list will reveal that it is no longer listed. 將 <DistributionName> 替換為目標 Linux 分發(fā)的名稱將從 WSL 中注銷該分發(fā),,以便可以重新安裝或清理它。注意:取消注冊后,,與該發(fā)行版關(guān)聯(lián)的所有數(shù)據(jù),、設(shè)置和軟件將永久丟失。從存儲重新安裝將安裝發(fā)行版的干凈副本,。例如,, wsl --unregister Ubuntu 將從 WSL 中可用的發(fā)行版中刪除 Ubuntu。運行 wsl --list 將顯示它不再列出,。
You can also uninstall the Linux distribution app on your Windows machine just like any other store application. To reinstall, find the distribution in the Microsoft Store and select "Launch". 您還可以像卸載任何其他商店應(yīng)用程序一樣卸載 Windows 計算機上的 Linux 分發(fā)應(yīng)用程序,。要重新安裝,請在Microsoft商店中找到發(fā)行版,,然后選擇“啟動”,。
Mount a disk or device 裝載磁盤或設(shè)備
PowerShell
wsl --mount <DiskPath>
Attach and mount a physical disk in all WSL2 distributions by replacing <DiskPath> with the directory\file path where the disk is located. See Mount a Linux disk in WSL 2. Options include: 通過將 <DiskPath> 替換為磁盤所在的目錄\文件路徑,在所有 WSL2 分發(fā)版中附加和裝載物理磁盤,。請參閱 在 WSL 2 中裝載 Linux 磁盤 ,。選項包括:
--vhd: Specifies that <Disk> refers to a virtual hard disk. --vhd :指定 <Disk> 引用虛擬硬盤。
--name: Mount the disk using a custom name for the mountpoint --name :使用裝入點的自定義名稱裝載磁盤
--bare: Attach the disk to WSL2, but don't mount it.
--type <Filesystem>: Filesystem type to use when mounting a disk, if not specified defaults to ext4. This command can also be entered as: wsl --mount -t <Filesystem>.You can detect the filesystem type using the command: blkid <BlockDevice>, for example: blkid <dev/sdb1>.
--partition <Partition Number>: Index number of the partition to mount, if not specified defaults to the whole disk.
--options <MountOptions>: There are some filesystem-specific options that can be included when mounting a disk. For example, ext4 mount options like: wsl --mount -o "data-ordered" or wsl --mount -o "data=writeback. However, only filesystem-specific options are supported at this time. Generic options, such as ro, rw, or noatime, are not supported.
Note
If you're running a 32-bit process in order to access wsl.exe (a 64-bit tool), you may need to run the command in the following manner: C:\Windows\Sysnative\wsl.exe --command.
Unmount disks
PowerShell
wsl --unmount <DiskPath>
Unmount a disk given at the disk path, if no disk path is given then this command will unmount and detach ALL mounted disks.
Deprecated WSL commands
PowerShell
wslconfig.exe [Argument] [Options]
PowerShell
bash [Options]
PowerShell
lxrun /[Argument]
These commands were the original wsl syntax for configuring Linux distributions installed with WSL, but have been replaced with the wsl or wsl.exe command syntax.