windows子系统wsl_您(或我)不知道的Cool WSL(Linux的Windows子系统)提示和技巧

本文介绍了Windows子系统Linux(WSL)的最新进展,特别是WSL2带来的优势,如轻量级虚拟化、高性能和紧密集成。用户可以在Windows上直接运行Linux命令,反之亦然,甚至可以使用Visual Studio Code进行Linux应用程序的本地开发。此外,还展示了如何在不同Windows系统间轻松迁移WSL发行版,以及利用Windows Git凭据提供程序。通过这些技巧,开发者可以更加高效地在Windows环境中利用Linux工具和资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows子系统wsl

windows子系统wsl

It's no secret I dig WSL (Windows Subsystem for Linux) and now that WSL2 is available in Windows Insiders Slow it's a great time to really explore the options that are available. What I'm finding is so interesting about WSL and how it relates to the Windows system around it is how you can cleanly move data between worlds. This isn't an experience you can easily have with full virtual machines, and it speaks to the tight integration of Linux and Windows.

我挖WSL(Linux的Windows子系统)已经不是什么秘密了,现在Windows Insiders中可以使用WSL2了。现在是真正探索可用选项的好时机。 我发现的有关WSL的内容非常有趣,它与周围的Windows系统之间的关系是如何在各个世界之间干净地移动数据。 对于完整的虚拟机来说,这不是一次轻松的体验,它说明了Linux和Windows的紧密集成。

Look at all this cool stuff you can do when you mix your peanut butter and chocolate!

看看混合花生酱和巧克力时可以做的所有这些酷事!

从Linux运行Windows资源管理器并访问发行版的文件 (Run Windows Explorer from Linux and access your distro's files)

When you're at the WSL/bash command line and you want to access your files visually, you can run "explorer.exe ." where . is the current directory, and you'll get a Windows Explorer window with your Linux files served to you over a local network plan9 server.

当您在WSL / bash命令行中并且想要直观地访问文件时,可以运行“ explorer.exe”。 在哪里。 是当前目录,您将获得一个Windows资源管理器窗口,其中包含通过本地网络plan9服务器提供Linux文件。

Accessing WSL files from Explorer

从Windows使用Real Linux命令(非Cgywin) (Use Real Linux commands (not Cgywin) from Windows)

I've blogged this before, but there are now aliases for PowerShell functions that allow you to use real Linux commands from within Windows.

我以前已经写过这篇文章,但是现在有PowerShell函数的别名,这些别名使您可以从Windows内部使用真正Linux命令

You can call any Linux command directly from DOS/Windows/whatever by just putting it after WSL.exe, like this!

只需将其放在WSL.exe之后,就可以直接从DOS / Windows /任何地方调用任何Linux命令!

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

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...

从Linux使用Real Windows命令(不是Wine) (Use Real Windows commands (not Wine) from Linux)

Windows executables are callable/runnable from WSL/Linux because the the Windows Path is in the $PATH until Windows. All you have to do is call it with .exe at the end, explicitly. That's how "Explorer.exe ." works above. You can also notepad.exe, or whatever.exe!

Windows可执行文件可从WSL / Linux调用/运行,因为Windows路径在Windows之前一直在$ PATH中。 您要做的就是在末尾显式地使用.exe进行调用。 这就是“ Explorer.exe”的方式。 上面的作品。 您也可以notepad.exe或任何其他文件!

运行Visual Studio代码并在Windows上本地访问(并构建!)您Linux应用 (Run Visual Studio Code and access (and build!) your Linux apps natively on Windows)

You can run "code ." when you're in a folder within WSL and you'll get prompted to install the VS Remote extensions. That effectively splits Visual Studio Code in half and runs the headless VS Code Server inside Linux with the VS Code client in the Windows world.

您可以运行“代码”。 当您位于WSL中的文件夹中时,系统会提示您安装VS Remote扩展。 这样可以有效地将Visual Studio Code分成两部分,并通过Windows世界中的VS Code客户端在Linux内部运行无头VS Code Server。

You'll also need to install Visual Studio Code and the Remote - WSL extension. Optionally, check out the beta Windows Terminal for the best possible terminal experience on Windows.

您还需要安装Visual Studio CodeRemote-WSL扩展。 (可选)查看Beta Windows Terminal ,以获得Windows上最佳的终端体验。

Here's a great series from the Windows Command LIne blog:

这是Windows Command LIne博客的精彩系列文章:

You can find the full series here:

您可以在此处找到完整的系列:

Here's the benefits of WSL 2

这是WSL 2的好处

  • Virtual machines are resource intensive and create a very disconnected experience.

    虚拟机占用大量资源,并且会产生非常脱节的体验。
  • The original WSL was very connected, but had fairly poor performance compared to a VM.

    原始的WSL连接非常紧密,但是与VM相比,性能却相当差。
  • WSL 2 brings a hybrid approach with a lightweight VM, a completely connected experience, and high performance.

    WSL 2带来了一种混合方法,该方法具有轻量级VM,完全连接的体验和高性能。

Again, now available on Windows 10 Insiders Slow.

再次,现在Windows 10 Insiders Slow上可用。

在几秒钟内并排运行多个Linux (Run multiple Linuxes in seconds, side by side)

Here I'm running "wsl --list --all" and I have three Linuxes already on my system.

在这里,我正在运行“ wsl --list --all”,并且我的系统上已经有三个Linux。

C:\Users\scott>wsl --list --all
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)
Ubuntu-16.04
Pengwin

I can easily run them, and also assign a profile to each so they appear in my Windows Terminal dropdown.

我可以轻松地运行它们,还可以为每个文件分配一个配置文件,以便它们出现在Windows Terminal下拉列表中。

使用Pengwin在Windows下运行X Windows Server (Run an X Windows Server under Windows using Pengwin)

Pengwin is a custom WSL-specific Linux distro that's worth the money. You can get it at the Windows Store. Combine Pengwin with an X Server like X410 and  you've got a very cool integrated system.

Pengwin是值得定制的WSL特定于Linux的发行版。 您可以在Windows Store中获得它。 将Pengwin和X410之类X服务器结合使用,您将获得一个非常酷的集成系统。

在Windows系统之间轻松移动WSL发行版 (Easily move WSL Distros between Windows systems)

Ana Betts points out this great technique where you can easily move your perfect WSL2 distro from one machine to n machines.

Ana Betts指出了这项出色的技术,您可以轻松地将完美的WSL2发行版从一台机器转移到n台机器。

wsl --export MyDistro ./distro.tar

# put it somewhere, dropbox, onedrive, elsewhere

mkdir ~/AppData/Local/MyDistro
wsl --import MyDistro ~/AppData/Local/MyDistro ./distro.tar --version 2

That's it. Get your ideal Linux setup sync'ed on all your systems.

而已。 在所有系统上同步理想Linux安装程序。

在WSL中使用Windows Git凭据提供程序 (Use the Windows Git Credential Provider within WSL)

All of these things culminate in this lovely blog post by Ana Betts where she integrates the Windows Git Credential Provider in WSL by making /usr/bin/git-credential-manager into a shell script that calls the Windows git creds manager. Genius. This would only be possible given this clean and tight integration.

所有这些事情在Ana Betts的这篇可爱的博客文章中达到了高潮,她通过将/ usr / bin / git-credential-manager制作为调用Windows git creds管理器的shell脚本,在WSL中集成了Windows Git凭据提供程序。 天才。 鉴于这种干净紧密的集成,这才有可能。

Now, go out there, install WSL, Windows Terminal, and make yourself a shiny Linux Environment on Windows.

现在,去那里,安装WSL, Windows Terminal ,并使自己成为Windows上闪亮Linux环境

Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!

赞助商:喜欢C#吗? 我们也这样做! 这就是为什么我们开发了一个快速,智能,跨平台的.NET IDE的原因,它为您提供了更多的编码能力。 巧妙的代码分析,丰富的代码完成,即时搜索和导航,高级调试器...使用JetBrains Rider,您所需的一切都唾手可得。 在Linux,Mac或Windows上以思考的速度编写C#代码。 立即尝试JetBrains Rider

翻译自: https://www.hanselman.com/blog/cool-wsl-windows-subsystem-for-linux-tips-and-tricks-you-or-i-didnt-know-were-possible

windows子系统wsl

### IEEE 802.1Q VLAN Tagging Protocol Standard IEEE 802.1Q 是支持虚拟局域网(VLAN)的标准协议之一,通常被称为 Dot1q。该标准定义了一种用于以太网帧的 VLAN 标记系统以及交换机桥接器处理这些标记帧的操作流程[^2]。 #### 协议结构概述 IEEE 802.1Q 的核心功能在于通过在以太网数据帧中插入特定字段来实现 VLAN 标签的功能。这种标签使得网络设备能够识别哪些流量属于哪个 VLAN,并据此执行转发决策。具体来说: - **Tag Header**: 在原始以太网帧头部增加了一个额外的 4 字节字段作为 VLAN 标签头。这四个字节包含了以下部分: - **Priority Code Point (PCP)**: 使用 3 比特表示优先级级别,范围从 0 到 7,主要用于 QoS 控制。 - **Canonical Format Indicator (CFI)**: 这是一个单比特位,在传统以太网环境中设置为零。 - **VLAN Identifier (VID)**: 使用 12 比特标识具体的 VLAN ID,理论上可以支持多达 4096 个同的 VLAN(编号从 0 至 4095),其中某些特殊值保留给内部用途管理目的。 #### 数据包处理机制 当一个带有 VLAN tag 的数据包进入支持 IEEE 802.1Q 的交换机时,它会依据此标签决定如何路由者过滤该数据流。如果目标端口属于同一 VLAN,则会传输至其他无关联的物理接口上;反之亦然——只有相同 VLAN 成员之间才允许互相通信除非经过路由器跨网段访问[^1]。 此外,为了简化管理配置过程并增强互操作性,还引入了一些辅助性的子协议服务组件比如 GARP(通用属性注册协议)。GARP 可帮助分发有关 VLAN 成员资格的信息到各个连接节点以便动态调整其行为模式而无需频繁手动干预[^3]。 以下是创建带 VLAN TAG 的 Python 示例代码片段展示如何模拟构建这样的 Ethernet Frame: ```python from scapy.all import Ether, Dot1Q, IP, sendp def create_vlan_packet(src_mac="00:aa:bb:cc:dd:ee", dst_mac="ff:ff:ff:ff:ff:ff", vlan_id=100, src_ip="192.168.1.1", dst_ip="192.168.1.2"): ether = Ether(src=src_mac, dst=dst_mac) dot1q = Dot1Q(vlan=vlan_id) ip_layer = IP(src=src_ip, dst=dst_ip) packet = ether / dot1q / ip_layer return packet packet = create_vlan_packet() sendp(packet, iface="eth0") # Replace 'eth0' with your network interface name. ``` 上述脚本利用 Scapy 库生成包含指定源地址、目的地址及所属 VLAN 编号的数据报文并通过选定的网卡发送出去测试实际效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值