Logo Logo BW diosito's blog

Basic WSL Commands Cheat Sheet 🐧

December 11, 2024
4 min read
Table of Contents

Basic WSL Commands Cheat Sheet 🐧

List installed Linux distributions

wsl --list --verbose

Update WSL

wsl --update

Check WSL status

wsl --status

Shutdown

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.

Terminate

wsl --terminate <Distribution Name>

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

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)

Export a distribution

wsl --export <Distribution Name> <FileName>

Exports a snapshot of the specified distribution as a new distribution file. Defaults to tar format. The filename can beĀ -Ā for standard input. Options include:

  • --vhd: Specifies the export distribution should be a .vhdx file instead of a tar file (this is only supported using WSL 2)

Import a distribution

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

Imports the specified tar file as a new distribution. The filename can beĀ -Ā for standard input. Options include:

  • --vhd: Specifies the import distribution should be a .vhdx file instead of a tar file (this is only supported using WSL 2)
  • --version <1/2>: Specifies whether to import the distribution as a WSL 1 or WSL 2 distribution

Import a distribution in place

wsl --import-in-place <Distribution Name> <FileName>

Imports the specified .vhdx file as a new distribution. The virtual hard disk must be formatted in the ext4 filesystem type.

Unregister or uninstall a Linux distribution

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

To unregister and uninstall a WSL distribution:

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.

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ā€.

Mount a disk or device

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:

  • --vhd: Specifies thatĀ <Disk>Ā refers to a virtual hard disk.
  • --name: Mount the disk using a custom name for the mountpoint
  • --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.

Unmount disks

wsl --unmount <DiskPath>

Source