The essential WSL commands for PowerShell you need to know


    Linux Mint on WSLSource: Windows Central

    The Windows Subsystem for Linux (WSL) is an incredibly powerful tool, and with WSL2, it puts an actual Linux kernel on your Windows 10 or Windows 11 PC.

    WSL is almost entirely managed through the terminal, even though on Windows 11 it supports GUI applications. Once you have a Linux distro installed, or multiple even, managing and interacting with them through PowerShell is something you’ll want to be familiar with, as it means you can carry out multiple tasks without having to flip between the distros.

    Here are some of the essential commands you’ll want to commit to memory.

    How to enable Windows Subsystem for Linux

    Microsoft has been iterating on WSL for a while now and has made getting set up easier than ever.

    If you’re running version 2004 of Windows 10 or later and you’ve installed all the latest optional updates, you can set up WSL2 with a single command. Inside a PowerShell window simply type:

    wsl --install

    The entire process will now happen automatically, and WSL will pull in and install Ubuntu by default.

    Alternatively, the latest preview of WSL can be downloaded from the Microsoft Store.


    WSL

    WSL Preview for Windows 11

    Download the latest version of WSL onto your Windows 11 PC now from the Microsoft Store for easy updates in the future.

    The essential WSL commands for PowerShell

    Some of the basics you need to know, especially if you’re running WSL2 or a mixture of both versions include commands to manage which Linux distro runs on which version. For all of these commands, any shortened versions will only be available in Windows 10 19041 and higher, but the full versions are worth knowing anyway, as they better explain exactly what you’re doing.

    Basics

    WSLSource: Windows Central

    If you’re inside a PowerShell window and simply want to get into your default WSL Linux distro:

    wsl

    Where exit will always then take you back to PowerShell. If you want to launch a specific distro then enter:

    wsl -d <distribution name> so for example, to launch Debian it would be wsl -d debian

    It’s also worth mentioning perhaps the most useful tool of all when learning and managing WSL, and that’s how to get help:

    wsl --help

    If you ever need a prod, this will give you a handy list in your terminal of the main commands.

    Handling WSL versions

    WSLSource: Windows Central

    To see which version of WSL is assigned to each of your installed Linux distros:

    wsl --list --verbose or wsl -l -v

    The results will also show which of your versions is your default wsl distro with an asterisk. If you only want to see what distros you have installed, simply remove the verbose option.

    If you wish to go one step further and dictate which distro you have installed is your default, enter:

    wsl --set-default <distro> or shorten to wsl -s <distro> so, for example, if you wish to set Ubuntu as default you would enter wsl -s ubuntu.

    If you’ve made the upgrade from WSL1 to WSL2 and want that to be your default going forward, enter this command:

    wsl --set-default-version 2

    This will ensure that any future Linux installs use WSL2, but it won’t convert any that you already have installed that are on WSL version 1. To do that you’ll need to manually change with the command below:

    To amend the version of WSL attached to a distro:

    wsl --set -version <distribution name> <version> so for example, to change Debian to WSL 2 you would enter wsl --set -version Debian 2

    WSLSource: Windows Central

    Running commands using wsl.exe

    It’s possible to run execute a number of Linux commands without having to first launch into the dedicated shell. This is particularly handy for quick processes, for example running an update.

    WSLSource: Windows Central

    To do this you would use the template wsl <argument> <options> <commandline>

    If you just want to run commands in your default Linux distro, then you don’t need to specify an argument at all. For example, to run the sudo apt update command for Ubuntu if it’s your default, you would simply enter:

    wsl sudo apt update

    You’ll be asked for your Linux password, but otherwise it’ll execute the command without ever having to leave PowerShell. If you want to target a specific version of Linux, then you would target it with an argument and an option. So, using the same command but this time using Debian as an example, you would enter:

    wsl --distribution debian sudo apt update

    Where --distribution or -d is the argument and debian is your option.

    WSLSource: Windows Central

    This works for all kinds of commands and can be used to install software and much more, while also supporting standard Linux flags such as -y to auto accept. An example command here where we’re installing youtube-dl on Debian again, just using wsl.exe:

    wsl -d debian sudo apt install youtube-dl -y

    Likewise launching applications and many other regular Linux functions, you can follow the same template. To run as a specific user if you have multiple set up, the template above will also accept --user <username> or -u <username> as well, even when mixed with other arguments.

    WSLSource: Windows Central

    Managing WSL

    WSLSource: Windows Central

    There are some basic commands that you’ll want to remember too that are designed to help you manage your WSL setup. Some of them we’ve already covered, such as listing distributions and setting defaults, but there are some other handy bits to know.

    If you want to export your distribution to a tar file use this template:

    wsl --export <distribution> <location> <filename>

    It’s important to enter the full path of the file you want to create as well, so in the example command below I’m creating an export of Debian into a folder on my C: drive called “Code” and calling it “debiantest”:

    wsl --export debian c:codedebiantest.tar

    WSLSource: Windows Central

    By contrast you can also import a tar file using the same template, substituting the --export argument for --import. To then run the imported distro you would enter:

    wsl --distribution <yourdistributionname>

    You can, of course, substitute --distribution for -d. After importing the “debiantest” file created above, to run it would be:

    wsl --distribution debiantest1 or wsl -d debiantest1.

    Simple! This process is also handy for setting up WSL distros not downloaded from the Microsoft Store, such as the latest rolling releases of Ubuntu.

    There’s much more you can do with WSL on Windows 10 and Windows 11, but this should cover most of the basics of interacting with it from within PowerShell and managing your installations.

    We may earn a commission for purchases using our links. Learn more.





    Source link

    Previous articleBitcoin’s Value Depends on Its Decentralization
    Next articleNSAV ANNOUNCES LAUNCH OF THE TECHNICORUM INFLUENCE TOKEN ON