Windows 与 Linux 互操作性的 Windows 子系统Windows Subsystem for Linux interoperability with Windows

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

Windows 子系统用于 Linux (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

从 Windows 命令提示符 (CMD 或 PowerShell) 使用运行 Linux 的二进制文件wsl.exe <command>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 ->

以下 Linux 命令wsl.exe处理就像在 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 可以调用 Windows 二进制文件直接从 WSL 命令行使用[binary name].exeWSL can invoke Windows binaries directly from the WSL command line using [binary name].exe. 例如, notepad.exeFor example, notepad.exe. 若要使 Windows 可执行文件更轻松地运行,Windows 路径包括在 Linux $PATH Fall Creators Update 中。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 任务管理器中显示像直接在命令提示符处执行。Appear in the Windows Task Manager as if directly executed from the CMD prompt.

例如:Example:

$ notepad.exe

为本机 Linux 可执行文件-通过管道传递重定向,并按预期方式甚至后台处理的工作类似方式处理在 WSL 中运行的 Windows 可执行文件。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

使用混合的 Windows 和 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.txtnotepad.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

修改位于 VolFs 上的文件 (文件不在/mnt/<x>) 与 Windows 应用程序在 WSL 中的不支持。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 之前, 仅 Windows 环境变量可访问 WSL 是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

用户可能会禁止以根身份运行以下命令为单一的 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.exe 或以 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

虽然互操作体验预 Fall Creators 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

WSL 命令传递到bash -c转发到 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 ->

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

适用于 Linux 的 Windows 子系统可以调用 Windows 二进制文件直接从 WSL 命令行。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 任务管理器中显示像直接在命令提示符处执行。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.txtnotepad.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

修改位于 VolFs 上的文件 (文件不在/mnt/<x>) 与 Windows 应用程序不支持。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