适用于 Linux 的 windows 子系统与 Windows 的互操作性Windows Subsystem for Linux interoperability with Windows

更新了秋季创意者更新。Updated for Fall Creators Update.
如果正在运行创建者更新或周年周年更新, 请跳转到 "创建者/周年更新" 部分If you're running Creators Update or Anniversary Update, jump to the Creators/Anniversary Update section.

适用于 Linux 的 Windows 子系统 (WSL) 不断改进 Windows 和 Linux 的集成。The Windows Subsystem for Linux (WSL) is continuously improving integration between Windows and Linux. 你可以:You can:

  1. 从 Linux 控制台调用 Windows 二进制文件。Invoke Windows binaries from the Linux console.
  2. 从 Windows 控制台调用 Linux 二进制文件。Invoke Linux binaries from a Windows console.
  3. Windows 预览体验内部版本 17063 + 在 Linux 和 Windows 之间共享环境变量。Windows Insiders Builds 17063+ Share environment variables between Linux and Windows.

这在 Windows 和 WSL 之间提供了无缝体验。This delivers a seamless experience between Windows and WSL. WSL 博客上的技术详细信息。Technical details are on the WSL blog.

从 Windows 命令行运行 Linux 工具Run Linux tools from a Windows command line

使用wsl.exe <command>从 Windows 命令提示符 (CMD 或 PowerShell) 运行 Linux 二进制文件。Run Linux binaries from the Windows Command Prompt (CMD or PowerShell) using wsl.exe <command>.

以这种方式调用的二进制文件:Binaries invoked in this way:

  1. 使用与当前 CMD 或 PowerShell 提示符相同的工作目录。Use the same working directory as the current CMD or PowerShell prompt.
  2. 以 WSL 默认用户身份运行。Run as the WSL default user.
  3. 具有与调用进程和终端相同的 Windows 管理权限。Have the same Windows administrative rights as the calling process and terminal.

例如:For example:

C:\temp> wsl ls -la
<- contents of C:\temp ->

以下wsl.exe Linux 命令的处理方式类似于在 WSL 中运行的任何命令。The Linux command following wsl.exe is handled like any command run in WSL. 例如, sudo、管道和文件重定向的工作方式。Things such as sudo, piping, and file redirection work.

使用 sudo 的示例:Example using sudo:

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

混合 WSL 和 Windows 命令的示例:Examples mixing WSL and Windows commands:

C:\temp> wsl ls -la | findstr "foo"
-rwxrwxrwx 1 root root     14 Sep 27 14:26 foo.bat

C:\temp> dir | wsl grep foo
09/27/2016  02:26 PM                14 foo.bat

C:\temp> wsl ls -la > out.txt

传入的命令wsl.exe将在不进行修改的情况下转发到 WSL 进程。The commands passed into wsl.exe are forwarded to the WSL process without modification. 文件路径必须以 WSL 格式指定。File paths must be specified in the WSL format.

路径示例:Example with paths:

C:\temp> wsl ls -la /proc/cpuinfo
-r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo

C:\temp> wsl ls -la "/mnt/c/Program Files"
<- contents of C:\Program Files ->

从 WSL 运行 Windows 工具Run Windows tools from WSL

WSL 可以使用[binary name].exe从 WSL 命令行直接调用 Windows 二进制文件。WSL can invoke Windows binaries directly from the WSL command line using [binary name].exe. 例如,notepad.exeFor example, notepad.exe. 为了使 windows 可执行文件更易于运行, windows 路径包含在$PATH秋季创意者更新中。To make Windows executables easier to run, Windows path is included in the Linux $PATH in Fall Creators Update.

以这种方式运行的应用程序具有以下属性:Applications run this way have the following properties:

  1. 将工作目录保留为 WSL 命令提示符 (大多数情况下, 以下说明除外)。Retain the working directory as the WSL command prompt (for the most part -- exceptions are explained below).
  2. 具有与 WSL 进程相同的权限。Have the same permission rights as the WSL process.
  3. 以活动 Windows 用户身份运行。Run as the active Windows user.
  4. 在 Windows 任务管理器中显示, 就像直接从 CMD 提示符执行一样。Appear in the Windows Task Manager as if directly executed from the CMD prompt.

例如:Example:

$ notepad.exe

在 WSL 中运行的 Windows 可执行文件的处理方式类似于本机 Linux 可执行文件-管道、重定向, 甚至后台处理按预期工作。Windows executables run in WSL are handled similarly to native Linux executables -- piping, redirects, and even backgrounding work as expected.

使用管道的示例:Examples using pipes:

$ ipconfig.exe | grep IPv4 | cut -d: -f2
172.21.240.1
10.159.21.24

使用混合窗口和 WSL 命令的示例:Example using mixed Windows and WSL commands:

$ ls -la | findstr.exe foo.txt

$ cmd.exe /c dir
<- contents of C:\ ->

Windows 二进制文件必须包含文件扩展名, 与文件大小写匹配, 并可执行。Windows binaries must include the file extension, match the file case, and be executable. 包括批处理脚本的不可执行文件。Non-executables including batch scripts. CMD 本机命令 ( dir如) 可以通过cmd.exe /C命令运行。CMD native commands like dir can be run with cmd.exe /C command.

示例:Examples:

$ cmd.exe /C dir
<- contents of C:\ ->

$ PING.EXE www.microsoft.com
Pinging e1863.dspb.akamaiedge.net [2600:1409:a:5a2::747] with 32 bytes of data:
Reply from 2600:1409:a:5a2::747: time=2ms

参数被传递到未修改的 Windows 二进制文件。Parameters are passed to the Windows binary unmodified.

例如, 以下命令将在中C:\temp\foo.txt notepad.exe打开:As an example, the following commands will open C:\temp\foo.txt in notepad.exe:

$ notepad.exe "C:\temp\foo.txt"
$ notepad.exe C:\\temp\\foo.txt

不支持在 WSL 中使用 Windows 应用程序/mnt/<x>修改位于 VolFs (不受下的文件) 上的文件。Modifying files located on VolFs (files not under /mnt/<x>) with a Windows application in WSL is not supported.

默认情况下, WSL 会尝试将 Windows 二进制文件的工作目录保留为当前的 WSL 目录, 但如果工作目录在 VolFs 上, 则将回退到实例创建目录。By default, WSL tries to keep the working directory of the Windows binary as the current WSL directory, but will fall back on the instance creation directory if the working directory is on VolFs.

例如,wsl.exe最初从C:\temp开始, 当前的 WSL 目录更改为用户的主目录。As an example; wsl.exe is initially launched from C:\temp and the current WSL directory is changed to the user’s home. notepad.exe从用户的主目录调用时, WSL 将自动还原为C:\temp notepad.exe 工作目录:When notepad.exe is called from the user’s home directory, WSL automatically reverts to C:\temp as the notepad.exe working directory:

C:\temp> wsl
/mnt/c/temp/$ cd ~
~$ notepad.exe foo.txt
~$ ls | grep foo.txt
~$ exit

exit
C:\temp>dir | findstr foo.txt
09/27/2016  02:15 PM                14 foo.txt

在 Windows 和 WSL 之间共享环境变量Share environment variables between Windows and WSL

在 Windows 有问必答版本17063及更高版本中可用。Available in Windows Insider builds 17063 and later.

在17063之前, 只有 WSL 可以访问的 Windows 环境变量是PATH (因此, 你可以从 WSL 下启动 Win32 可执行文件)。Prior to 17063, only Windows environment variable that WSL could access was PATH (so you could launch Win32 executables from under WSL).

从17063、WSL 和 Windows 共享WSLENV开始, 创建了一个特殊的环境变量, 用于在 WSL 上运行的 Windows 和 Linux 发行版。Starting in 17063, WSL and Windows share WSLENV, a special environment variable created to bridge Windows and Linux distros running on WSL.

WSLENV属性:Properties of WSLENV:

  • 它是共享的;它存在于 Windows 和 WSL 环境中。It is shared; it exists in both Windows and WSL environments.
  • 它是要在 Windows 和 WSL 之间共享的环境变量的列表。It is a list of environment variables to share between Windows and WSL.
  • 它可以设置环境变量的格式, 以便在 Windows 和 WSL 中正常工作。It can format environment variables to work well in Windows and WSL.

WSLENV提供了四个标志来影响如何转换该环境变量。There are four flags available in WSLENV to influence how that environment variable is translated.

WSLENV随意WSLENV flags:

  • /p-翻译 WSL/Linux 样式路径和 Win32 路径之间的路径。/p - translates the path between WSL/Linux style paths and Win32 paths.
  • /l-指示环境变量是路径的列表。/l - indicates the environment variable is a list of paths.
  • /u-指示此环境变量只应在从 Win32 运行 WSL 时包括在内。/u - indicates that this environment variable should only be included when running WSL from Win32.
  • /w-指示此环境变量只应在从 WSL 运行 Win32 时包括在内。/w - indicates that this environment variable should only be included when running Win32 from WSL.

可以根据需要组合标志。Flags can be combined as needed.

禁用互操作Disable Interop

用户可以通过以 root 身份运行以下命令, 禁止为单个 WSL 会话运行 Windows 二进制文件的功能。Users may disable the ability to run Windows binaries for a single WSL session by running the following command as root:

$ echo 0 > /proc/sys/fs/binfmt_misc/WSLInterop

若要重新启用 Windows 二进制文件, 请退出所有 WSL 会话并重新运行 bash, 或以 root 身份运行以下命令:To reenable Windows binaries either exit all WSL sessions and re-run bash.exe or run the following command as root:

$ echo 1 > /proc/sys/fs/binfmt_misc/WSLInterop

禁用互操作不会在 WSL 会话之间保留--启动新会话时将再次启用互操作。Disabling interop will not persist between WSL sessions -- interop will be enabled again when a new session is launched.

创意者更新和周年周年更新Creators Update and Anniversary Update

尽管互操作体验前期编写者更新类似于最新的互操作体验, 但有很多重大差别。While the interop experience pre-Fall Creators Update is similar to more recent interop experiences, there are a handful of major differences.

总结:To summarize:

  • bash.exe已弃用并已替换为wsl.exebash.exe has been deprecated and replaced with wsl.exe.
  • -c不需要wsl.exe用于运行单个命令的选项。-c option for running a single command isn't needed with wsl.exe.
  • Windows 路径包含在 WSL 中。$PATHWindows path is included in the WSL $PATH

禁用互操作的过程保持不变。The process for disabling interop is unchanged.

从 Windows 命令行调用 WSLInvoking WSL from the Windows Command Line

可以从 Windows 命令提示符或 PowerShell 调用 Linux 二进制文件。Linux binaries can be invoked from the Windows Command Prompt or from PowerShell. 以这种方式调用的二进制文件具有以下属性:Binaries invoked in this way have the following properties:

  1. 使用与 CMD 或 PowerShell 提示符相同的工作目录。Use the same working directory as the CMD or PowerShell prompt.
  2. 以 WSL 默认用户身份运行。Run as the WSL default user.
  3. 具有与调用进程和终端相同的 Windows 管理权限。Have the same Windows administrative rights as the calling process and terminal.

例如:Example:

C:\temp> bash -c "ls -la"

以这种方式调用的 Linux 命令的处理方式与任何其他 Windows 应用程序一样。Linux commands called in this way are handled like any other Windows application. 输入、管道和文件重定向等功能按预期方式工作。Things such as input, piping, and file redirection work as expected.

示例:Examples:

C:\temp>bash -c "sudo apt-get update"
[sudo] password for username:
Hit:1 https://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 https://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
C:\temp> bash -c "ls -la" | findstr foo
C:\temp> dir | bash -c "grep foo"
C:\temp> bash -c "ls -la" > out.txt

传递给bash -c的 WSL 命令将在不进行修改的情况下转发到 WSL 进程。The WSL commands passed into bash -c are forwarded to the WSL process without modification. 文件路径必须以 WSL 格式指定, 必须小心才能转义相关字符。File paths must be specified in the WSL format and care must be taken to escape relevant characters. 例如:Example:

C:\temp> bash -c "ls -la /proc/cpuinfo"
-r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo

C:\temp> bash -c "ls -la \"/mnt/c/Program Files\""
<- contents of C:\Program Files ->

从 WSL 调用 Windows 二进制文件Invoking Windows binaries from WSL

适用于 Linux 的 Windows 子系统可以直接从 WSL 命令行调用 Windows 二进制文件。The Windows Subsystem for Linux can invoke Windows binaries directly from the WSL command line. 以这种方式运行的应用程序具有以下属性:Applications run this way have the following properties:

  1. 除了下面所述的方案之外, 将工作目录保留为 WSL 命令提示符。Retain the working directory as the WSL command prompt except in the scenario explained below.
  2. 与该bash.exe进程具有相同的权限权限。Have the same permission rights as the bash.exe process.
  3. 以活动 Windows 用户身份运行。Run as the active Windows user.
  4. 在 Windows 任务管理器中显示, 就像直接从 CMD 提示符执行一样。Appear in the Windows Task Manager as if directly executed from the CMD prompt.

例如:Example:

$ /mnt/c/Windows/System32/notepad.exe

在 WSL 中, 这些可执行文件的处理方式类似于本机 Linux 可执行文件。In WSL, these executables are handled similar to native Linux executables. 这意味着将目录添加到 Linux 路径, 并按预期方式在命令之间进行管道操作。This means adding directories to the Linux path and piping between commands works as expected. 示例:Examples:

$ export PATH=$PATH:/mnt/c/Windows/System32
$ notepad.exe
$ ipconfig.exe | grep IPv4 | cut -d: -f2
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir

Windows 二进制文件必须包含文件扩展名, 与文件大小写匹配, 并可执行。The Windows binary must include the file extension, match the file case, and be executable. 可执行文件 (包括批处理脚本) 和dir命令 (如) /mnt/c/Windows/System32/cmd.exe /C可以通过命令运行。Non-executables including batch scripts and command like dir can be run with /mnt/c/Windows/System32/cmd.exe /C command.

示例:Examples:

$ /mnt/c/Windows/System32/cmd.exe /C dir
$ /mnt/c/Windows/System32/PING.EXE www.microsoft.com

参数被传递到未修改的 Windows 二进制文件。Parameters are passed to the Windows binary unmodified.

例如, 以下命令将在中C:\temp\foo.txt notepad.exe打开:As an example, the following commands will open C:\temp\foo.txt in notepad.exe:

$ notepad.exe "C:\temp\foo.txt"
$ notepad.exe C:\\temp\\foo.txt

不支持使用 Windows 应用程序修改位于 VolFs /mnt/<x>(不在下) 的文件。Modifying files located on VolFs (files not under /mnt/<x>) with a Windows application is not supported. 默认情况下, WSL 会尝试将 Windows 二进制文件的工作目录保留为当前的 WSL 目录, 但如果工作目录在 VolFs 上, 则将回退到实例创建目录。By default, WSL attempts to keep the working directory of the Windows binary as the current WSL directory, but will fall back on the instance creation directory if the working directory is on VolFs.

例如,bash.exe最初从C:\temp开始, 当前的 WSL 目录更改为用户的主目录。As an example; bash.exe is initially launched from C:\temp and the current WSL directory is changed to the user’s home. notepad.exe从用户的主目录调用时, WSL 将自动还原为C:\temp notepad.exe 工作目录:When notepad.exe is called from the user’s home directory, WSL automatically reverts to C:\temp as the notepad.exe working directory:

C:\temp> bash
/mnt/c/temp/$ cd ~
~$ notepad.exe foo.txt
~$ ls | grep foo.txt
~$ exit
exit

C:\temp> dir | findstr foo.txt
09/27/2016  02:15 PM                14 foo.txt