windows命令和linux,Windows 与 Linux 的互操作性

Windows 与 Linux 的互操作性Windows interoperability with Linux

05/12/2020

本文内容

适用于 Linux 的 Windows 子系统 (WSL) 正在持续改进 Windows 与 Linux 之间的集成。The Windows Subsystem for Linux (WSL) is continuously improving integration between Windows and Linux. 您可以:You can:

从 Linux 命令行(即 Ubuntu)运行 Windows 工具(即 notepad.exe)。Run Windows tools (ie. notepad.exe) from a Linux command line (ie. Ubuntu).

从 Windows 命令行(即 PowerShell)运行 Linux 工具(即 grep)。Run Linux tools (ie. grep) from a Windows command line (ie. PowerShell).

在 Windows 与 Windows 之间共享环境变量。Share environment variables between Linux and Windows. (版本 17063+)(Build 17063+)

备注

如果运行的是创意者更新(2017 年 10 月的版本16299)或周年更新(2016 年 8 月的版本 14393),请跳转到 Windows 10 的早期版本。If you're running Creators Update (Oct 2017, Build 16299) or Anniversary Update (Aug 2016, Build 14393), jump to the Earlier versions of Windows 10.

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

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

例如:For example:

C:\temp> wsl ls -la

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

使用当前 CMD 或 PowerShell 提示符中提到的同一工作目录。Use the same working directory as the current CMD or PowerShell prompt.

以 WSL 默认用户的身份运行。Run as the WSL default user.

拥有与调用方进程和终端相同的 Windows 管理权限。Have the same Windows administrative rights as the calling process and terminal.

wsl(或 wsl.exe)后面的 Linux 命令的处理方式与 WSL 中运行的任何命令的处理方式类似。The Linux command following wsl (or wsl.exe) is handled like any command run in WSL. 可以执行 sudo、管道处理和文件重定向等操作。Things such as sudo, piping, and file redirection work.

使用 sudo 更新默认 Linux 分发版的示例:Example using sudo to update your default Linux distribution:

C:\temp> wsl sudo apt-get update

运行此命令后,将会列出默认的 Linux 分发版用户名,并将要求你输入密码。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.

混合 Linux 和 Windows 命令Mixing Linux and Windows commands

下面是几个使用 PowerShell 混合 Linux 和 Windows 命令的示例。Here are a few examples of mixing Linux and Windows commands using PowerShell.

若要使用 Linux 命令 ls -la 列出文件,并使用 PowerShell 命令 findstr 来筛选包含“git”的单词的结果,请组合这些命令: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:

wsl ls -la | findstr "git"

若要使用 PowerShell 命令 dir 列出文件,并使用 Linux 命令 grep 来筛选包含“git”的单词的结果,请组合这些命令: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:

C:\temp> dir | wsl grep git

若要使用 Linux 命令 ls -la 列出文件,并使用 PowerShell 命令 > out.txt 将该列表输出到名为“out.txt”的文本文件,请组合这些命令: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:

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.

若要使用 Linux 命令 ls -la 列出 /proc/cpuinfo Linux 文件系统路径中的文件,请使用 PowerShell:To use the Linux command ls -la to list files in the /proc/cpuinfo Linux file system path, using PowerShell:

C:\temp> wsl ls -la /proc/cpuinfo

若要使用 Linux 命令 ls -la 列出 C:\Program Files Windows 文件系统路径中的文件,请使用 PowerShell:To use the Linux command ls -la to list files in the C:\Program Files Windows file system path, using PowerShell:

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

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

WSL 可以使用 [tool-name].exe 直接从 WSL 命令行运行 Windows 工具。WSL can run Windows tools directly from the WSL command line using [tool-name].exe. 例如,notepad.exe。For example, notepad.exe.

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

按 WSL 命令提示保留工作目录(大部分情况下是这样 -- 下面所述的情况除外)。Retain the working directory as the WSL command prompt (for the most part -- exceptions are explained below).

拥有与 WSL 进程相同的权限。Have the same permission rights as the WSL process.

以活动 Windows 用户的身份运行。Run as the active Windows user.

显示在 Windows 任务管理器中,就如同直接从 CMD 提示符执行的一样。Appear in the Windows Task Manager as if directly executed from the CMD prompt.

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

若要运行 Windows 工具 ipconfig.exe,请使用 Linux 工具 grep 筛选“IPv4”结果,并使用 Linux 工具 cut 删除列字段,请从 Linux 分发版(例如 Ubuntu)输入: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:

ipconfig.exe | grep IPv4 | cut -d: -f2

让我们尝试一个混合使用 Windows 和 Linux 命令的示例。Let's try an example mixing Windows and Linux commands. 打开 Linux 分发版(即 Ubuntu)并创建文本文件:touch foo.txt。Open your Linux distribution (ie. Ubuntu) and create a text file: touch foo.txt. 现在使用 Linux 命令 ls -la 列出直接文件及其创建详细信息,并使用 Windows PowerShell 工具 findstr.exe 来筛选结果,以便仅在结果中显示 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:

ls -la | findstr.exe foo.txt

Windows 工具必须包含文件扩展名,匹配文件大小写,并且可执行。Windows tools must include the file extension, match the file case, and be executable. 包含批处理脚本的不可执行文件。Non-executables including batch scripts. dir 等 CMD 本机命令可与 cmd.exe /C 命令一起运行。CMD native commands like dir can be run with cmd.exe /C command.

例如,通过输入以下命令列出 Windows 文件系统 C:\ 目录的内容:For example, list the contents of your Windows files system C:\ directory, by entering:

cmd.exe /C dir

或者使用 ping 命令将回显请求发送到 microsoft.com 网站:Or use the ping command to send an echo request to the microsoft.com website:

ping.exe www.microsoft.com

参数将按原样传递到 Windows 二进制文件。Parameters are passed to the Windows binary unmodified. 例如,以下命令将通过 notepad.exe 打开 C:\temp\foo.txt:As an example, the following command will open C:\temp\foo.txt in notepad.exe:

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

以下命令也会起作用:This will also work:

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

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

WSL 和 Windows 共享一个特殊环境变量 WSLENV(为了桥接 Windows 和 WSL 上运行的 Linux 分发版而创建)。WSL and Windows share a special environment variable, WSLENV, created to bridge Windows and Linux distributions running on WSL.

WSLENV 变量的属性:Properties of WSLENV variable:

它是共享的;它同时在 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.

它可以帮助 WSL 和 Win32 之间的流。It can assist in the flow between WSL and Win32.

备注

在 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 开始,WSLENV 开始受支持。Starting in 17063, WSLENV begins being supported.

WSLENV 区分大小写。WSLENV is case sensitive.

WSLENV 标志WSLENV flags

WSLENV 中有四个标志可以影响该环境变量的转换方式。There are four flags available in WSLENV to influence how the 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.

阅读有关 WSLENV 的详细信息,包括将 WSLENV 的值设置为其他预定义环境变量串联的常见问题解答和示例,每个示例都带有一个斜杠后缀,斜杠后跟标志,用于指定应如何转换值以及如何使用脚本传递变量。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. 本文还提供使用 编程语言设置开发环境的示例,该示例配置为在 WSL 和 Win32 之间共享 GOPATH。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

用户可以使用 root 身份运行以下命令,禁用针对单个 WSL 会话运行 Windows 工具的功能:Users may disable the ability to run Windows tools 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 re-enable Windows binaries, 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.

Windows 10 的早期版本Earlier versions of Windows 10

对于早期的 Windows 10 版本,互操作性命令有一些不同之处。There are several differences for the interoperability commands on earlier Windows 10 versions. 如果运行的是创意者更新(2017 年10 月的版本 16299)或周年更新(2016 年 8 月的版本 14393),则建议你更新到最新的 Windows 版本,但如果无法执行此操作,请了解下面概述的一些互操作差异。If you're running a Creators Update (Oct 2017, Build 16299), or Anniversary Update (Aug 2016, Build 14393) version of Windows 10, we recommend you update to the latest Windows version, but if that's not possible, we have outlined some of the interop differences below.

摘要:Summary:

bash.exe 已替换为 wsl.exe。bash.exe has been replaced with wsl.exe.

在 wsl.exe 中不需要指定用于运行单个命令的 -c 选项。-c option for running a single command isn't needed with wsl.exe.

Windows 路径包含在 WSL $PATH 中。Windows path is included in the WSL $PATH.

禁用互操作的过程未有变化。The process for disabling interop is unchanged.

可以从 Windows 命令提示或 PowerShell 运行 Linux 命令,但对于早期 Windows 版本,可能需要使用 bash 命令。Linux commands can be run from the Windows Command Prompt or from PowerShell, but for early Windows versions, you man need to use the bash command. 例如:For example:

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

可按预期方式执行输入、管道处理和文件重定向等操作。Things such as input, piping, and file redirection work as expected.

传入 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"

或...Or...

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

从早期的 Windows 10 版本中的 WSL 分发版调用 Windows 工具时,需要指定目录路径。When calling a Windows tool from a WSL distribution in an earlier version of Windows 10, you will need to specify the directory path. 例如,在 WSL 命令行中,输入:For example, from your WSL command line, enter:

/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. 例如:For example:

export PATH=$PATH:/mnt/c/Windows/System32

或Or

ipconfig.exe | grep IPv4 | cut -d: -f2

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. 例如:For example:

/mnt/c/Windows/System32/cmd.exe /C dir

其他资源Additional resources

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值