top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

Running Linux Commands within Windows Using WSL

  • Writer: Paul Troncone
    Paul Troncone
  • Jul 3, 2020
  • 3 min read

Updated: Jul 3, 2021


In the first chapter of Cybersecurity Ops with bash we go over some of the ways you can leverage the power of the Linux command line directly from the Windows environment.

One of my favorite ways of doing this is by using the Windows Subsystem for Linux or WSL.

WSL was introduced as part of Windows 10, and it allows you to run an emulated or virtualized version of Linux natively within Windows.

There are several Linux distributions to choose from including Ubuntu and Kali.

How to Install WSL in Windows 10


In order to use WSL you will first need to install it.


1. Using the Windows Start Menu, search for "Windows Features"


2. Select "Turn Windows features on or off"

Turn On Windows Feature
Turn On Windows Feature

3. Check the box next to "Windows Subsystem for Linux" and select "OK"

Enable the Windows Subsystem for Linux
Enable the Windows Subsystem for Linux

4. After the installation completes select "Restart now"


5. Once the system is restarted use the Windows Start Menu to search for "Microsoft Store"


6. In the Microsoft Store, search for "Linux" and select the Linux distribution you would like to install

Microsoft Store
Microsoft Store

7. Click the "Install" button and then "Get"

Install Ubuntu
Install Ubuntu

8. After the Linux distribution is done installing click the "Launch" button.


You are now ready to start using the Windows Subsystem for Linux!


For more advance directions and troubleshooting see Microsoft's instructions.


How to Run WSL


Once the Windows Subsystem for Linux is enabled and you have installed a Linux distribution you can start it by opening the Windows Command Prompt and typing wsl

C:\>wsl

Welcome to Ubuntu 20.04.2 LTS (5.4.72-microsoft-standard-WSL2 x86_64)
$

Error Message - No Installed Distributions


Sometimes when running WSL for the first time you may receive the following error:

C:\>wsl

Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Microsoft Store:

To resolve this, perform a search using the Windows Start Menu for the name of the Linux distribution you installed from the Microsoft Store. For example, search for "Ubuntu":


Launch WSL Ubuntu Instance
Launch WSL Ubuntu Instance

Click the program to launch it and that should finish the installation on your system. You should not longer receive the error message when running wsl from the Command Prompt.

Once inside the WSL Linux distribution you can do things you normally would such as run Linux commands and install packages.

Running Linux Commands from Within WSL
Running Linux Commands from Within WSL

You can also directly run Linux commands from the Windows Command Prompt by using the wsl command and providing the Linux command as an argument. The command will execute in the WSL Linux distribution and the results will be returned back to the Windows command prompt.


In the example below you can see the Linux cowsay command run directly from the Windows Command Prompt:

Running Linux Commands from the Windows Command Prompt
Running Linux Commands from the Windows Command Prompt

Note that you must precede the Linux command with wsl, if you do not you will receive an error. Here is the incorrect method:

C:\>grep

'grep' is not recognized as an internal or external command,
operable program or batch file.

And here is the correct method:


C:\>wsl grep

Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

Piping and Redirection


You can also use input/output piping and redirection when using the wsl command, but you need to be careful how you do it to ensure it occurs within the correct operating system environment. Let's see what happens if you run the following command pipeline from the Windows command prompt:


C:\>wsl ps -e | grep ps

'grep' is not recognized as an internal or external command,
operable program or batch file.

Note that the output states that the grep command is not recognized. That is because Windows executes the ps -e command from within the WSL environment, but then returns the result back to the Windows environment. Windows then tries to pipe that output into the grep command, which of course does not exist in the Windows environment. To overcome this, simply add the wsl command again after the pipe so the grep command runs in the Linux environment.


C:\>wsl ps -e | wsl grep ps
    7 tty1     00:00:00 ps

I/O redirection works the same way. Here is an example:


C:\>wsl ps -e > ps.txt

Here the ps -e command is executed in the Linux environment using wsl and the results are returned to the Windows environment. Windows then redirects the output to the file ps.txt.


WSL and Windows PowerShell


Running Linux Commands from PowerShell
Running Linux Commands from PowerShell

Running WSL works exactly the same way from the Windows PowerShell. That means you are able to access your favorite Linux commands, bash scripts, and programs from directly within PowerShell scripts!


WSL 2


In mid-2020 Microsoft released WSL version 2. WSL 2 provides a more full-featured virtualized environment allowing for complete access to system calls. That means you can now install the complete array of Linux applications including Docker and similar programs.


Recent releases of WSL also supports running Linux programs that have graphical user interfaces.


Links and Resources


20 Comments


Avellyne Sherman
Avellyne Sherman
3 days ago

In many professional settings, financial reporting plays a key role in communication. The idea of what is financial accounting helps explain how this communication is structured. It ensures that financial information is presented clearly to stakeholders. This clarity supports informed decisions. The College of Contract Management provides insights into these processes. This strengthens understanding.

Like

toootaa1210
Apr 02

Mình có lần lướt đọc mấy trao đổi trên mạng شيخ روحاني thì thấy nhắc nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu sâu rauhane chỉ xem qua trong thời gian ngắn để quan sát bố cục s3udy cách sắp xếp các mục và trình bày nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn, các mục rõ ràng nên đọc lướt cũng không bị rối Berlinintim, với mình như vậy là đủ để nắm   tin cơ bản rồi. q8yat

Like

Simon Jack
Simon Jack
Mar 28

The final grade calculator is one of the most helpful tools a student can use when trying to stay on top of academic performance. It clearly shows what scores are needed in upcoming exams or assignments to reach a desired final grade. This kind of clarity removes a lot of stress and guesswork. Instead of worrying about outcomes, students can focus on actionable goals and plan their studies more effectively with confidence and direction.

Like

every4 you
every4 you
Mar 25

zerodha leverage for intraday, Thank you so much for your kindness and support. I truly appreciate the time, effort, and care you shared. Your help made a real difference and means more than I can say. I’m grateful for you and everything you’ve done, and I will always remember your generosity and warm spirit.

Like

Jean Marie Santos
Jean Marie Santos
Feb 26

Discussions about quantity surveying qualifications frequently overlap with contract administration topics. Distance learning providers, including the College of Contract Management, appear in several professional education listings. Modules commonly address tender evaluation and compliance procedures.

Like
bottom of page