WSL使用基本教程

Microsoft official document

Installation

Microsoft official document of wsl installation

# Check your wsl version and set the default to wsl2
wsl --version
wsl --set-default-version 2
 
# List available Linux distributions and install the selected one
wsl --list --online
wsl --install -d <Distribution Name>
​
# List installed Linux distributions
wsl --list --verbos

Set WSL version to 1 or 2

To designate the version of WSL (1 or 2) that a Linux distribution is running on:

wsl --set-version <DistributionName> 2

20250304121604195

Switching between WSL 1 and WSL 2 can be time-consuming and result in failures due to the differences between the two architectures.

For distributions with large projects, we recommend backing up files before attempting a conversion.

Reset WSL password

# wsl -d <your-distro-name> -u root
wsl --user root
​
# Reset the password of root
passwd root
​
# Reset the password of user
passwd <username>

Shutdown

Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine.

wsl --shutdown

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.

Terminate

To terminate the specified distribution, or stop it from running, replace <Distribution Name> with the name of the targeted distribution.

wsl --terminate <Distribution Name>

Identify IP address

  • wsl hostname -I: Returns the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)

  • ip route show | grep -i default | awk '{ print $3}': Returns the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)

Unregister or uninstall a Linux distribution

While Linux distributions can be installed through the Microsoft Store, they can't be uninstalled through the store.

Replacing <DistributionName> with the name of your targeted Linux distribution will unregister that distribution from WSL so it can be reinstalled or cleaned up:

wsl --unregister <DistributionName>

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.

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".

Snapshot and rollback

Export a distribution

  1. Check the VM status

    wsl -l -v

    image-20250304121604126

  2. Select the machine on which you want to take the snapshot:

    wsl --export <Distribution Name> <FileName>
    ​
    #wsl --export Debian d:\wsl-debian.tar
  3. If you want to roll back:

    wsl --import <Distribution Name> <InstallLocation> <FileName>

    for example,

    wsl --unregister Debian
    
    # wsl is installed on C: by default, you can change the install loctation
    wsl --import Debian d:\wsl d:\wsl-debian.tar --version 2
    
    Debian config --default-user [USERNAME]
    
    rm d:\wsl-debian.tar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值