How to Run Linux Commands Directly in Windows Terminal

For developers, IT professionals, and enthusiasts, the ability to run Linux commands directly within Windows Terminal offers a powerful and flexible workflow. This integration is made possible primarily through the Windows Subsystem for Linux (WSL), a compatibility layer developed by Microsoft that allows users to run a Linux environment directly on Windows, without the overhead of a traditional virtual machine.

This guide will explain how to set up WSL and then use Windows Terminal to execute Linux commands and interact with your Linux distribution.

Understanding WSL and Windows Terminal

Before diving into the steps, it is important to understand the key components:

  • Windows Subsystem for Linux (WSL): This is a feature of Windows that enables you to run a full Linux operating system distribution (like Ubuntu, Debian, or Fedora) directly on Windows. WSL effectively creates a lightweight Linux environment where you can execute Linux command-line tools, utilities, and applications.
  • Windows Terminal: This is a modern, multi-tabbed command-line interface application developed by Microsoft. It allows you to open multiple command-line shells (like Command Prompt, PowerShell, Azure Cloud Shell, and various WSL distributions) in a single window, providing a centralized and customizable environment.

WSL was first introduced in Windows 10, with significant improvements in WSL 2, which uses a lightweight virtual machine to provide better performance and full system call compatibility. Most modern setups will use WSL 2.

Step 1: Enable Windows Subsystem for Linux (WSL)

Before you can install a Linux distribution, you need to enable the WSL feature on your Windows operating system.

  1. Open PowerShell as Administrator:
    • Type powershell in the Windows search bar.
    • Right-click on “Windows PowerShell” and select “Run as administrator”.
  2. Enable WSL Feature:
    • In the PowerShell window, type the following command and press Enter:

PowerShell

wsl –install

    • This command will enable the necessary WSL features and, by default, install the Ubuntu Linux distribution. If you want to install a different distribution later, you can do so.
    • If you encounter issues or want to manually enable components, you can use:

Code snippet

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

These commands enable the core WSL feature and the Virtual Machine Platform (required for WSL 2).

  1. Restart Your Computer:
    • After running the wsl –install command (or the manual DISM commands), you will be prompted to restart your computer. It is crucial to restart for the changes to take effect.

Step 2: Install a Linux Distribution

After enabling WSL, you need to install a specific Linux distribution from the Microsoft Store. If you used wsl –install, Ubuntu will be installed automatically. If not, or if you want another distribution:

  1. Open Microsoft Store:
    • Type Microsoft Store in the Windows search bar and open the app.
  2. Search for Linux Distribution:
    • In the Microsoft Store, search for a Linux distribution. Popular choices include:
      • Ubuntu (most common and beginner-friendly)
      • Debian
      • Kali Linux (for cybersecurity tasks)
      • openSUSE
      • Alpine WSL (minimalist)
  3. Install the Distribution:
    • Click on your chosen distribution (e.g., Ubuntu).
    • Click the “Get” or “Install” button.
    • Once the download is complete, click “Open”.
  4. Complete Initial Setup:
    • The first time you open a newly installed Linux distribution, a console window will appear. It will take a few moments to finish its installation.
    • You will be prompted to create a UNIX username and password. This is specific to your Linux environment and does not have to be the same as your Windows credentials.
    • Enter a username and set a strong password. This password will be used for sudo (superuser do) commands, which grant administrative privileges within your Linux environment.

Step 3: Install Windows Terminal (If Not Already Installed)

Windows Terminal is the ideal interface for running Linux commands. It is often pre-installed on newer Windows versions, but if not, you can get it from the Microsoft Store.

  1. Open Microsoft Store:
    • Search for Microsoft Store in the Windows search bar.
  2. Search for Windows Terminal:
    • In the store, search for “Windows Terminal”.
  3. Install Windows Terminal:
    • Click the “Get” or “Install” button.
    • Once installed, open Windows Terminal.

Step 4: Run Linux Commands in Windows Terminal

Now that WSL and a Linux distribution are set up, and Windows Terminal is installed, you can start running Linux commands.

  1. Open Windows Terminal:
    • Type Windows Terminal in the Windows search bar and open the application.
  2. Open a New WSL Tab/Pane:
    • In Windows Terminal, you will see a dropdown arrow (usually next to the plus + sign) in the title bar.
    • Click this dropdown arrow. You will see a list of available profiles, including Command Prompt, PowerShell, and any installed WSL distributions (e.g., “Ubuntu”).
    • Select your installed Linux distribution (e.g., “Ubuntu”). A new tab or pane will open, displaying the Linux command-line interface.
  3. Execute Linux Commands:
    • You are now inside your Linux environment. You can run standard Linux commands:
      • ls: Lists files and directories in the current directory.
      • pwd: Prints the current working directory.
      • cd /mnt/c: Changes directory to your Windows C: drive (WSL automatically mounts Windows drives under /mnt).
      • sudo apt update: Updates the package lists for Ubuntu (requires your Linux password).
      • grep “error” logfile.txt: Searches for the word “error” in logfile.txt.
      • python3 –version: Checks the installed Python version (if Python is installed in your WSL).

Interacting with Windows Files from WSL:

WSL allows you to access your Windows file system. Windows drives are mounted under the /mnt directory.

  • Your C: drive is at /mnt/c/.
  • Your D: drive (if you have one) is at /mnt/d/.

Example: To navigate to your Windows Desktop from within your WSL terminal:

Bash

cd /mnt/c/Users/<YourWindowsUsername>/Desktop

Replace <YourWindowsUsername> with your actual Windows username.

Running WSL Commands from PowerShell or Command Prompt:

You can also execute WSL commands from a standard PowerShell or Command Prompt window using the wsl prefix.

Example:

  • To list your installed WSL distributions: wsl -l -v
  • To run an Ubuntu command directly without opening an Ubuntu terminal: wsl ubuntu ls -a
  • To run the default WSL distribution: wsl

This method is useful for quick, one-off Linux command executions from your Windows command line.

Step 5: Customizing Windows Terminal Profiles (Optional)

Windows Terminal offers customization for each profile.

  1. Open Settings:
    • In Windows Terminal, click the dropdown arrow and select “Settings”.
  2. Customize WSL Profile:
    • In the settings JSON file, you can find your WSL distribution’s profile.
    • You can change the starting directory, font size, color scheme, and more.
    • For example, you could set the startingDirectory for your Ubuntu profile to /home/<your_linux_username> or a specific Windows folder.

By following these steps, you can effectively integrate Linux command-line functionality into your Windows environment using WSL and Windows Terminal, streamlining your workflow for development, scripting, and system administration tasks.

Frequently Asked Questions (FAQ)

Q1: What is WSL and why is it useful for running Linux commands on Windows?

A1: WSL (Windows Subsystem for Linux) is a Windows feature that allows you to run a Linux operating system environment directly on Windows without needing a separate virtual machine. It is useful because it enables you to use Linux command-line tools, utilities, and applications seamlessly within Windows, which is beneficial for development and system administration tasks.

Q2: Which Linux distributions can I install with WSL?

A2: You can install popular Linux distributions like Ubuntu, Debian, Kali Linux, openSUSE, and others directly from the Microsoft Store. Ubuntu is often the default installation if you use wsl –install.

Q3: Do I need a separate Linux account for WSL?

A3: Yes, when you first set up a Linux distribution within WSL, you will be prompted to create a UNIX username and password specific to that Linux environment. This account is separate from your Windows user account.

Q4: How do I access my Windows files from within my WSL Linux environment?

A4: Your Windows drives are automatically mounted within your WSL Linux environment under the /mnt directory. For example, your Windows C: drive can be accessed at /mnt/c/, and your D: drive (if present) at /mnt/d/.

Q5: Can I run Linux commands directly from Windows PowerShell or Command Prompt without opening a WSL terminal?

A5: Yes, you can. You can use the wsl prefix followed by the Linux command you want to execute. For example, to list files in your Linux home directory from PowerShell, you can type

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *