Windows command line linux commands

How to call Linux commands through WSL in Windows command line?

I would like to use ls , and many other Linux tools, from the command line on Windows 10. I know that there is GnuWin32 and other binary replacements, but none of those works as elegantly as the Windows Subsystem for Linux (WSL); including the update mechanisms. With WSL, one can call ls . from the command line via bash -c «ls . » , which works great. Ideally, however, I would just type ls on the command line. I can create an ls.bat which basically does @bash -c «ls %*» — but I would have to do that for every single command I want to export. Is there a more direct way of exporting ls (and many others) to the command line?

The bash commands available in WSL are not intended for use outside of WSL. I would be surprised if that is ever implemented.

@ifconfig Well, it doesn’t have be intended or implemented. I would be happy with a one-liner aliasing «many» well-known Linux commands to the respective WSL commands.

4 Answers 4

Apparently this was the most requested feature for WSL, and Microsoft now supports this feature. To use linux commands from within Command Prompt (or PowerShell), just prefix the command with wsl . So, for example, here’s how you run ls from CMD.

Or here’s how you update package lists.

C:\temp> wsl sudo apt-get update [sudo] password for username: Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB] 

More details can be found in the official docs. I’m not sure if you are looking for a solution which doesn’t involve any kind of prefixing. If that’s the case, I don’t know of any solution yet.

Microsoft doesn’t directly support what you’re asking for, however.

  • You can create a «Console Alias» using doskey.exe, but these Console Aliases are not persistent (when you open a new Command Prompt window they’re all gone).
  • CMD /? informs us of the following two Registry keys: HKey_Local_Machine\Software\Microsoft\Command Processor\AutoRun and HKey_Current_User\Software\Microsoft\Command Processor\AutoRun , which (as long as the /D option wasn’t specified) will both be checked for contents to run (in that order) when a new Command Prompt window is opened.

If we put these two things together with a FOR loop, and a nice, easily customizable file in your user folder, we can get a pretty close approximation of what I think you want.
I’m going to put this in HKLM, and reference a file in %USERPROFILE% , called exportlinuxcommands , so that it’s available to all users, but easily customizable on a per-user basis, as well as only functional for users who set it up. If you only want it for one user, or don’t want it to be customizable on a per-user basis, you’ll have to modify the instructions a bit.

  1. Open the Registry Editor
  2. Go to HKey_Local_Machine\Software\Microsoft\Command Processor
  3. Right-click on an empty portion of the window, and choose «New» > «String Value» (or «Expandable String Value», but the expansions seem a little pointless when you realize that the Command Processor will expand them after it reads the value anyway.)
  4. Name it «AutoRun» (without the quotes)
  5. Double-click on the new value you just created
  6. Enter IF EXIST %USERPROFILE%\exportlinuxcommands FOR /F %i in (%USERPROFILE%\exportlinuxcommands) DO @doskey %i=bash -c «%i $*» as the «Value data».
Читайте также:  Linux shell program command

Now you just need to make a file in your %USERPROFILE% folder (i.e. C:\Users\YourUserName ) called exportlinuxcommands , with each Linux command you want an alias to on a separate line. For example:

Want to remove one? Remove it from the file, and it won’t be there in the next Command Prompt window you open.
Want to add another later? Just add it to the file on it’s own line, and the next time you open up a Command Prompt window, it’ll be there.

Источник

Basic commands for WSL

The WSL commands below are listed in a format supported by PowerShell or Windows Command Prompt. To run these commands from a Bash / Linux distribution command line, you must replace wsl with wsl.exe . For a full list of commands, run wsl —help .

Install

Install WSL and the default Ubuntu distribution of Linux. Learn more. You can also use this command to install additional Linux distributions by running wsl —install . For a valid list of distribution names, run wsl —list —online .

  • —distribution : Specify the Linux distribution to install. You can find available distributions by running wsl —list —online .
  • —no-launch : Install the Linux distribution but do not launch it automatically.
  • —web-download : Install from an online source rather than using the Microsoft Store.

When WSL is not installed options include:

  • —inbox : Installs WSL using the Windows component instead of using the Microsoft Store. (WSL updates will be received via Windows updates, rather than pushed out as-available via the store).
  • —enable-wsl1 : Enables WSL 1 during the install of the Microsoft Store version of WSL by also enabling the «Windows Subsystem for Linux» optional component.
  • —no-distribution : Do not install a distribution when installing WSL.

If you running WSL on Windows 10 or an older version, you may need to include the -d flag with the —install command to specify a distribution: wsl —install -d .

List available Linux distributions

See a list of the Linux distributions available through the online store. This command can also be entered as: wsl -l -o .

List installed Linux distributions

See a list of the Linux distributions installed on your Windows machine, including the state (whether the distribution is running or stopped) and the version of WSL running the distribution (WSL 1 or WSL 2). Comparing WSL 1 and WSL 2. This command can also be entered as: wsl -l -v . Additional options that can be used with the list command include: —all to list all distributions, —running to list only distributions that are currently running, or —quiet to only show distribution names.

Читайте также:  Linux on dvd player

Set WSL version to 1 or 2

To designate the version of WSL (1 or 2) that a Linux distribution is running on, replace with the name of the distribution and replace with 1 or 2. Comparing WSL 1 and WSL 2.

Set default WSL version

To set a default version of WSL 1 or WSL 2, replacing with either the number 1 or 2 to represent which version of WSL you would like the installation to default on for new Linux distribution installations. For example, wsl —set-default-version 2 . Comparing WSL 1 and WSL 2.

Set default Linux distribution

To set the default Linux distribution that WSL commands will use to run, replace with the name of your preferred Linux distribution.

Change directory to home

The ~ can be used with wsl to start in the user’s home directory. To jump from any directory back to home from within a WSL command prompt, you can use the command: cd ~ .

Run a specific Linux distribution from PowerShell or CMD

To run a specific Linux distribution with a specific user, replace with the name of your preferred Linux distribution (ie. Debian) and with the name of an existing user (ie. root). If the user doesn’t exist in the WSL distribution, you will receive an error. To print the current user name, use the command whoami .

Update WSL

Update your WSL version to the latest version. Options include:

Check WSL status

See general information about your WSL configuration, such as default distribution type, default distribution, and kernel version.

Check WSL version

Check the version information about WSL and its components.

Help command

See a list of options and commands available with WSL.

Run as a specific user

To run WSL as a specified user, replace with the name of a user that exists in the WSL distribution.

Change the default user for a distribution

Change the default user 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 .

This command will not work for imported distributions, because these distributions do not have an executable launcher. You can instead change the default user for imported distributions using the /etc/wsl.conf file. See the Automount options in the Advanced Settings Configuration doc.

Shutdown

Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as changing memory usage limits or making a change to your .wslconfig file.

Читайте также:  Kali linux 2016 pdf

Terminate

To terminate the specified distribution, or stop it from running, replace with the name of the targeted distribution.

Import and export a distribution

Imports and exports the specified tar file as a new distribution. The filename can be — for standard input. Options include:

  • —vhd : Specifies the import/export distribution should be a .vhdx file instead of a tar file
  • —version : For import only, specifies whether to import the distribution as a WSL 1 or WSL 2 distribution

Import a distribution in place

Imports the specified .vhdx file as a new distribution. The virtual hard disk must be formatted in the ext4 filesystem type.

Unregister or uninstall a Linux 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:

Replacing with the name of your targeted Linux distribution will unregister that 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.

You can also uninstall the Linux distribution app on your Windows machine just like any other store application. To reinstall, find the distribution in the Microsoft Store and select «Launch».

Mount a disk or device

Attach and mount a physical disk in all WSL2 distributions by replacing with the directory\file path where the disk is located. See Mount a Linux disk in WSL 2. Options include:

  • —vhd : Specifies that refers to a virtual hard disk.
  • —name : Mount the disk using a custom name for the mountpoint
  • —bare : Attach the disk to WSL2, but don’t mount it.
  • —type : Filesystem type to use when mounting a disk, if not specified defaults to ext4. This command can also be entered as: wsl —mount -t .You can detect the filesystem type using the command: blkid , for example: blkid .
  • —partition : Index number of the partition to mount, if not specified defaults to the whole disk.
  • —options : There are some filesystem-specific options that can be included when mounting a disk. For example, ext4 mount options like: wsl —mount -o «data-ordered» or wsl —mount -o «data=writeback . However, only filesystem-specific options are supported at this time. Generic options, such as ro , rw , or noatime , are not supported.

If you’re running a 32-bit process in order to access wsl.exe (a 64-bit tool), you may need to run the command in the following manner: C:\Windows\Sysnative\wsl.exe —command .

Unmount disks

Unmount a disk given at the disk path, if no disk path is given then this command will unmount and detach ALL mounted disks.

Deprecated WSL commands

wslconfig.exe [Argument] [Options] 

These commands were the original wsl syntax for configuring Linux distributions installed with WSL, but have been replaced with the wsl or wsl.exe command syntax.

Feedback

Submit and view feedback for

Источник

Оцените статью
Adblock
detector