Mastering the WSL Command Line: Essential Commands and Best Practices

Mastering the WSL Command Line: Essential Commands and Best Practices
Photo by bady abbas / Unsplash

Windows Subsystem for Linux (WSL) is a powerful tool that allows developers to run Linux distributions directly on Windows without a virtual machine. However, many users only scratch the surface of what WSL's command-line interface (CLI) offers. This article explores essential WSL commands, hidden gems, and best practices to enhance your workflow.


1. Managing WSL Distributions

Listing Installed Distributions

To see all installed WSL distributions:

wsl --list

Or, for more details (running state, WSL version, etc.):

wsl --list --verbose

Shortcut:

wsl -l -v

Setting the Default Distribution

To change which Linux distribution is used when running wsl:

wsl --set-default <DistroName>

Shortcut:

wsl -s <DistroName>

Launching a Specific Distribution

wsl --distribution <DistroName>

or

wsl -d <DistroName>

2. Controlling WSL Execution

Shutting Down WSL

To completely shut down all running WSL instances and free resources:

wsl --shutdown

Stopping a Specific Distribution

If you want to terminate a single distribution instead of shutting down everything:

wsl --terminate <DistroName>

Running a Linux Command Without Entering WSL

To execute a Linux command directly from PowerShell or CMD:

wsl --exec <command>

Example:

wsl --exec ls -la

Shortcut:

wsl -e <command>

Example:

wsl -e uname -a

Running a Command in a Specific Distribution

wsl -d <DistroName> -- <command>

Example:

wsl -d Ubuntu -- uname -r

3. Managing WSL Versions

Checking the Default WSL Version

wsl --status

Setting the Default WSL Version

To ensure that all newly installed distributions use WSL 2 by default:

wsl --set-default-version 2

Converting Between WSL 1 and WSL 2

If you want to switch a specific distribution to WSL 2:

wsl --set-version <DistroName> 2

To revert back to WSL 1:

wsl --set-version <DistroName> 1

4. Networking and File Access

Opening the WSL Filesystem in Windows Explorer

To quickly access your Linux files from Windows:

explorer.exe .

Accessing WSL from Windows via \wsl$

You can navigate to WSL files using Windows File Explorer:

\\wsl$\<DistroName>

Enabling WSLg (For GUI Apps on Windows 11)

For users running WSL 2 with GUI support (Windows 11):

wsl --set-network <options>

5. Keeping WSL Updated

Updating the WSL Kernel

If you are using WSL 2, it's important to keep the Linux kernel updated:

wsl --update

Rolling Back to the Previous WSL Kernel

If a recent update causes issues, you can revert:

wsl --update rollback

6. Advanced Cleanup and Uninstallation

Unregistering (Deleting) a WSL Distribution

This permanently deletes a WSL distribution, including all files and settings:

wsl --unregister <DistroName>

Warning: This action is irreversible.

Freeing Up Resources

If WSL is using too much memory or behaving oddly, restart it:

wsl --shutdown

This stops all running instances and clears allocated RAM.


Finally: Mastering WSL

WSL is a powerful tool that bridges the gap between Windows and Linux, making it easier for developers to work across platforms. By mastering these CLI commands, you can manage your distributions efficiently, optimize performance, and integrate WSL seamlessly into your workflow.

Key Takeaways:

  • Use wsl --list -v to check running distributions.
  • wsl --shutdown and wsl --terminate <DistroName> free up system resources.
  • Convert distributions between WSL 1 and WSL 2 using wsl --set-version.
  • wsl --exec <command> runs Linux commands directly from Windows.
  • Keep WSL updated with wsl --update for best performance.

By using these commands effectively, you can streamline your development environment and get the most out of WSL. 🚀

Support Us