Windows 與 Linux 互通性的 Windows 子系統Windows Subsystem for Linux interoperability with Windows

Fall Creators update 更新。Updated for Fall Creators Update.
如果您執行 Creators Update 或年度更新版,跳至Creators/年度更新版 」 一節If you're running Creators Update or Anniversary Update, jump to the Creators/Anniversary Update section.

Windows for 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. 叫用 Linux 的 Windows 主控台的二進位檔。Invoke Linux binaries from a Windows console.
  3. Windows Insiders 組建 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 可以叫用直接從 WSL 命令列中使用的 Windows 二進位檔[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

在 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

使用混合的 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

共用 WSL 與 Windows 環境變數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.

停用 InteropDisable Interop

使用者可能會停用單一 WSL 工作階段執行 Windows 二進位檔,藉由執行下列命令以 root 身分執行的能力: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

停用 interop 不會保存 WSL 工作階段之間--interop 時才會啟用一次啟動新的工作階段。Disabling interop will not persist between WSL sessions -- interop will be enabled again when a new session is launched.

Creators Update 」 和 「 年度更新版Creators Update and Anniversary Update

雖然 interop 體驗前 Fall Creators Update 也是較新的 interop 體驗類似,但在 handfull 的主要差異。While the interop experience pre-Fall Creators Update is similar to more recent interop experiences, there are a handfull 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

停用 interop 的程序不會變更。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"

就像任何其他 Windows 應用程式處理這種方式呼叫的 Linux 命令。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>) 不支援 with 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