Windows command line linux commands

Working across Windows and Linux file systems

There are a number of considerations to keep in mind when working between Windows and Linux file systems. We have outlined a few of them for you in this guide, including some examples of interoperability support for mixing Windows and Linux-based commands.

File storage and performance across file systems

We recommend against working across operating systems with your files, unless you have a specific reason for doing so. For the fastest performance speed, store your files in the WSL file system if you are working in a Linux command line (Ubuntu, OpenSUSE, etc). If you’re working in a Windows command line (PowerShell, Command Prompt), store your files in the Windows file system.

For example, when storing your WSL project files:

  • Use the Linux file system root directory: \\wsl$\Ubuntu-18.04\home\ \Project
  • Not the Windows file system root directory: /mnt/c/Users/ /Project$ or C:\Users\ \Project

When you see /mnt/ in the file path of a WSL command line, it means that you are working from a mounted drive. So the Windows file system C:/ drive ( C:\Users\ \Project ) will look like this when mounted in a WSL command line: /mnt/c/Users/ /Project$ . It is possible to store your project files on a mounted drive, but your performance speed will improve if you store them directly on the \\wsl$ drive.

View your current directory in Windows File Explorer

You can view the directory where your files are stored by opening the Windows File Explorer from the command line, using:

Alternatively, you can also use the command: powershell.exe /c start . Be sure to add the period at the end of the command to open the current directory.

To view all of your available Linux distributions and their root file systems in Windows File explorer, in the address bar enter: \\wsl$

Filename and directory case sensitivity

Case sensitivity determines whether uppercase (FOO.txt) and lowercase (foo.txt) letters are handled as distinct (case-sensitive) or equivalent (case-insensitive) in a file name or directory. Windows and Linux file systems handle case sensitivity in different ways — Windows is case-insenstive and Linux is case-sensitive. Learn more about how to adjust case sensitivity, particularly when mounting disks with WSL, in the Adjust case sensitivity how-to article.

Interoperability between Windows and Linux commands

Windows and Linux tools and commands can be used interchangeably with WSL.

  • Run Windows tools (ie. notepad.exe) from a Linux command line (ie. Ubuntu).
  • Run Linux tools (ie. grep) from a Windows command line (ie. PowerShell).
  • Share environment variables between Linux and Windows. (Build 17063+)

Run Linux tools from a Windows command line

Run Linux binaries from the Windows Command Prompt (CMD) or PowerShell using wsl (or wsl.exe ).

Binaries invoked in this way:

  • Use the same working directory as the current CMD or PowerShell prompt.
  • Run as the WSL default user.
  • Have the same Windows administrative rights as the calling process and terminal.

The Linux command following wsl (or wsl.exe ) is handled like any command run in WSL. Things such as sudo, piping, and file redirection work.

Example using sudo to update your default Linux distribution:

Your default Linux distribution user name will be listed after running this command and you will be asked for your password. After entering your password correctly, your distribution will download updates.

Mixing Linux and Windows commands

Here are a few examples of mixing Linux and Windows commands using PowerShell.

To use the Linux command ls -la to list files and the PowerShell command findstr to filter the results for words containing «git», combine the commands:

To use the PowerShell command dir to list files and the Linux command grep to filter the results for words containing «git», combine the commands:

To use the Linux command ls -la to list files and the PowerShell command > out.txt to print that list to a text file named «out.txt», combine the commands:

The commands passed into wsl.exe are forwarded to the WSL process without modification. File paths must be specified in the WSL format.

To use the Linux command ls -la to list files in the /proc/cpuinfo Linux file system path, using PowerShell:

To use the Linux command ls -la to list files in the C:\Program Files Windows file system path, using PowerShell:

Run Windows tools from Linux

WSL can run Windows tools directly from the WSL command line using [tool-name].exe . For example, notepad.exe .

Applications run this way have the following properties:

  • Retain the working directory as the WSL command prompt (for the most part — exceptions are explained below).
  • Have the same permission rights as the WSL process.
  • Run as the active Windows user.
  • Appear in the Windows Task Manager as if directly executed from the CMD prompt.
Читайте также:  Windows application driver что это

Windows executables run in WSL are handled similarly to native Linux executables — piping, redirects, and even backgrounding work as expected.

To run the Windows tool ipconfig.exe , use the Linux tool grep to filter the «IPv4» results, and use the Linux tool cut to remove the column fields, from a Linux distribution (for example, Ubuntu) enter:

Let’s try an example mixing Windows and Linux commands. Open your Linux distribution (ie. Ubuntu) and create a text file: touch foo.txt . Now use the Linux command ls -la to list the direct files and their creation details, plus the Windows PowerShell tool findstr.exe to filter the results so only your foo.txt file shows in the results:

Windows tools must include the file extension, match the file case, and be executable. Non-executables including batch scripts. CMD native commands like dir can be run with cmd.exe /C command.

For example, list the contents of your Windows files system C:\ directory, by entering:

Or use the ping command to send an echo request to the microsoft.com website:

Parameters are passed to the Windows binary unmodified. As an example, the following command will open C:\temp\foo.txt in notepad.exe :

This will also work:

Share environment variables between Windows and WSL with WSLENV

WSL and Windows share a special environment variable, WSLENV , created to bridge Windows and Linux distributions running on WSL.

Properties of WSLENV variable:

  • It is shared; it exists in both Windows and WSL environments.
  • It is a list of environment variables to share between Windows and WSL.
  • It can format environment variables to work well in Windows and WSL.
  • It can assist in the flow between WSL and Win32.

Prior to 17063, only Windows environment variable that WSL could access was PATH (so you could launch Win32 executables from under WSL). Starting in 17063, WSLENV begins being supported. WSLENV is case sensitive.

WSLENV flags

There are four flags available in WSLENV to influence how the environment variable is translated.

  • /p — translates the path between WSL/Linux style paths and Win32 paths.
  • /l — indicates the environment variable is a list of paths.
  • /u — indicates that this environment variable should only be included when running WSL from Win32.
  • /w — indicates that this environment variable should only be included when running Win32 from WSL.

Flags can be combined as needed.

Read more about WSLENV, including FAQs and examples of setting the value of WSLENV to a concatenation of other pre-defined environment vars, each suffixed with a slash followed by flags to specify how the value should be translated and passing variables with a script. This article also includes an example for setting up a dev environment with the Go programming language, configured to share a GOPATH between WSL and Win32.

Disable interoperability

Users may disable the ability to run Windows tools for a single WSL session by running the following command as root:

To re-enable Windows binaries, exit all WSL sessions and re-run bash.exe or run the following command as root:

Disabling interop will not persist between WSL sessions — interop will be enabled again when a new session is launched.

Источник

Configure Linux distributions

Windows Subsystem for Linux (WSL) supports running as many different Linux distributions as you would like to install. This can include choosing distributions from the Microsoft Store, importing a custom distribution, or building your own custom distribution.

Ways to run WSL

There are several ways to run a Linux distribution with WSL once it’s installed:

  1. The number one way that we recommend you run Linux distributions is by installing Windows Terminal. Using Windows Terminal enables you to open multiple tabs or window panes to display and quickly switch between multiple Linux distributions or other command lines (PowerShell, Command Prompt, PowerShell, Azure CLI, etc). You can fully customize your terminal with unique color schemes, font styles, sizes, background images, and custom keyboard shortcuts. Learn more.
  2. You can directly open your Linux distribution by visiting the Windows Start menu and typing the name of your installed distributions. For example: «Ubuntu». This will open Ubuntu in it’s own console window.
  3. From Windows Command Prompt or PowerShell, you can enter the name of your installed distribution. For example: ubuntu
  4. From Windows Command Prompt or PowerShell, you can open your default Linux distribution inside your current command line, by entering: wsl.exe .
  5. From Windows Command Prompt or PowerShell, you can use your default Linux distribution inside your current command line, without entering a new one, by entering: wsl [command] . Replacing [command] with a WSL command, such as: wsl -l -v to list installed distributions or wsl pwd to see where the current directory path is mounted in wsl. From PowerShell, the command get-date will provide the date from the Windows file system and wsl date will provide the date from the Linux file system.

The method you select should depend on what you’re doing. If you’ve opened a WSL command line within a Windows Prompt or PowerShell window and want to exit, enter the command: exit .

Читайте также:  Работа с windows core

List installed distributions

To see a list of the Linux distributions you have installed, enter: wsl —list or wsl -l -v for a verbose list. To set an installed Linux distribution as the default that is used with the wsl command, enter: wsl -s or wsl —setdefault , replacing with the name of the Linux distribution you would like to use. For example, from Powershell, enter: wsl -s Debian to set the default distribution to Debian. Now running wsl npm init from Powershell will run the npm init command in Debian.

Unregister and reinstall a 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
Unregisters the 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. To reinstall, find the distribution in the Microsoft Store and select «Launch».

Run as a specific user

wsl -u , wsl —user

Run WSL as the specified user. Please note that user must exist inside of the WSL distribution.

Change the default user for a distribution

Change the default user that for your distribution log-in. The user has to already exist inside the distribution in order to become the default user.

For example: ubuntu config —default-user johndoe would change the default user for the Ubuntu distribution to the «johndoe» user.

If you are having trouble figuring out the name of your distribution, use the command wsl -l .

Run a specific distribution

wsl -d , wsl —distribution

Run a specified distribution of WSL, can be used to send commands to a specific distribution without having to change your default.

Configure settings with .wslconfig and wsl.conf

You can configure options for your installed Linux distributions, such as automount options and network configuration, that will automatically be applied every time you launch WSL in two ways:

  • Globally for all installed distributions running in WSL 2 mode with a .wslconfig file stored in your %UserProfile% directory
  • On a per-distribution basis with a wsl.conf file stored in the /etc directory of the distribution

To get to your %UserProfile% directory, in PowerShell, use cd

to access your home directory (which is typically your user profile, C:\Users\ ) or you can open Windows File Explorer and enter %UserProfile% in the address bar. The directory path for globally configuring WSL options will be C:\Users\ \.wslconfig .

To get to the /etc directory for an installed distribution, use the distribution’s command line with cd / to access the root directory, then ls to list files or explorer.exe . to view in Windows File Explorer. The directory path for configuring WSL options on a per-distribution basis will be /etc/wsl.conf .

WSL will detect the existence of these files and read the contents. If the file is missing or malformed (that is, improper markup formatting), WSL will continue to launch as normal.

Adjusting per-distribution settings with the .wsl.conf file is only available in Windows Build 17093 and later.

Per distribution configuration options with wsl.conf

The wsl.conf sample file below demonstrates some of the configuration options available to add into your distributions:

When launching multiple Linux shells for the same distribution, you must wait until the Linux subsystem stops running, this can take approximately 8 seconds after closing the last instance of the distribution shell. If you launch a distribution (ie. Ubuntu), modify the wsl.conf file, close the distribution, and then re-launch it. You might assume that your changes to the wsl.conf file have immediately gone into effect. This is not currently the case as the subsystem could still be running. You must wait

8 seconds for the subsystem to stop before relaunching in order to give enough time for your changes to be picked up. You can check to see whether your Linux distribution (shell) is still running after closing it by using PowerShell with the command: wsl —list —running . If no distributions are running, you will receive the response: «There are no running distributions.» You can now restart the distribution to see your wsl.conf updates applied.

wsl —shutdown is a fast path to restarting WSL 2 distributions, but it will shut down all running distributions, so use wisely.

Options for wsl.conf

In keeping with .ini conventions, keys are declared under a section.

WSL supports four sections: automount , network , interop , and user .

automount

key value default notes
enabled boolean true true causes fixed drives (i.e C:/ or D:/ ) to be automatically mounted with DrvFs under /mnt . false means drives won’t be mounted automatically, but you could still mount them manually or via fstab .
mountFsTab boolean true true sets /etc/fstab to be processed on WSL start. /etc/fstab is a file where you can declare other filesystems, like an SMB share. Thus, you can mount these filesystems automatically in WSL on start up.
root String /mnt/ Sets the directory where fixed drives will be automatically mounted. For example, if you have a directory in WSL at /windir/ and you specify that as the root, you would expect to see your fixed drives mounted at /windir/c
options comma-separated list of values empty string This value is appended to the default DrvFs mount options string. Only DrvFs-specific options can be specified. Options that the mount binary would normally parse into a flag are not supported. If you want to explicitly specify those options, you must include every drive for which you want to do so in /etc/fstab.

By default, WSL sets the uid and gid to the value of the default user (in Ubuntu distro, the default user is created with uid=1000,gid=1000). If the user specifies a gid or uid option explicitly via this key, the associated value will be overwritten. Otherwise, the default value will always be appended.

Note: These options are applied as the mount options for all automatically mounted drives. To change the options for a specific drive only, use /etc/fstab instead.

Mount options

Setting different mount options for Windows drives (DrvFs) can control how file permissions are calculated for Windows files. The following options are available:

Key Description Default
uid The User ID used for the owner of all files The default User ID of your WSL distro (On first installation this defaults to 1000)
gid The Group ID used for the owner of all files The default group ID of your WSL distro (On first installation this defaults to 1000)
umask An octal mask of permissions to exclude for all files and directories 000
fmask An octal mask of permissions to exclude for all files 000
dmask An octal mask of permissions to exclude for all directories 000
metadata Whether metadata is added to Windows files to support Linux system permissions disabled
case Determines directories treated as case sensitive and whether new directories created with WSL will have the flag set. See case sensitivity for a detailed explanation of the options. off

The permission masks are put through a logical OR operation before being applied to files or directories.

network

Section label: [network]

key value default notes
generateHosts boolean true true sets WSL to generate /etc/hosts . The hosts file contains a static map of hostnames corresponding IP address.
generateResolvConf boolean true true set WSL to generate /etc/resolv.conf . The resolv.conf contains a DNS list that are capable of resolving a given hostname to its IP address.

interop

Section label: [interop]

These options are available in Insider Build 17713 and later.

key value default notes
enabled boolean true Setting this key will determine whether WSL will support launching Windows processes.
appendWindowsPath boolean true Setting this key will determine whether WSL will add Windows path elements to the $PATH environment variable.

Section label: [user]

These options are available in Build 18980 and later.

key value default notes
default string The initial username created on first run Setting this key specifies which user to run as when first starting a WSL session.

User preview options

These options are only available in the latest preview builds if you are on the latest builds of the Windows Insiders program.

Section label: [boot]

key value default notes
command string «» A string of the command that you would like to run when the WSL instance starts. This command is run as the root user. e.g: service docker start

Global configuration options with .wslconfig

You can add a file named .wslconfig to your Windows home directory (e.g: C:\Users\crloewen\.wslconfig ) to control global WSL options across Linux distributions. Please see the sample file below as an example.

Global configuration options with .wslconfig in only available for distributions running as WSL 2 in Windows Build 19041 and later. Keep in mind you may need to run wsl —shutdown to shut down the WSL 2 VM and then restart your WSL instance for these changes to take affect.

This file can contain the following options:

Options for .wslconfig

Section label: [wsl2]

These settings affect the VM that powers any WSL 2 distribution.

key value default notes
kernel string The Microsoft built kernel provided inbox An absolute Windows path to a custom Linux kernel.
memory size 50% of total memory on Windows or 8GB, whichever is less; on builds before 20175: 80% of your total memory on Windows How much memory to assign to the WSL 2 VM.
processors number The same number of processors on Windows How many processors to assign to the WSL 2 VM.
localhostForwarding boolean true Boolean specifying if ports bound to wildcard or localhost in the WSL 2 VM should be connectable from the host via localhost:port .
kernelCommandLine string Blank Additional kernel command line arguments.
swap size 25% of memory size on Windows rounded up to the nearest GB How much swap space to add to the WSL 2 VM, 0 for no swap file.
swapFile string %USERPROFILE%\AppData\Local\Temp\swap.vhdx An absolute Windows path to the swap virtual hard disk.

Entries with the path value must be Windows paths with escaped backslashes, e.g: C:\\Temp\\myCustomKernel

Entries with the size value must be a size followed by a unit, for example 8GB or 512MB .

WSL 2 setting preview options

These options are only available in the latest preview builds if you are on the latest builds of the Windows Insiders program.

Источник

Читайте также:  Как обновить список отозванных сертификатов windows
Оцените статью