Hyper-V Sockets in WSL 2: Host-Guest IPC Without an IP Network
A boundary-focused guide to Hyper-V socket transport in WSL 2, service identifiers, AF_VSOCK and AF_HYPERV sides, framing, identity, lifetime, and testing.
WSL 2 runs Linux in a lightweight managed virtual machine. Windows and the Linux guest can communicate through IP networking, but Hyper-V also provides a host/guest socket transport that does not require assigning an IP address to the peer. WSL components use virtualization sockets internally. Product code should distinguish documented Hyper-V socket APIs from WSL implementation details that can change with the Store-delivered WSL package.
The endpoints use platform-native address families
On Windows, Hyper-V sockets are exposed through Winsock with the AF_HYPERV address family. Addresses identify a virtual machine and a service by GUID. Hyper-V defines VM ID values, including documented wildcard/parent/loopback concepts, while a service ID distinguishes protocols inside the guest.
Linux represents host/guest virtual sockets through AF_VSOCK, using a context identifier (CID) and port. The Hyper-V transport in the Linux kernel maps this model onto the underlying VMBus/Hyper-V channel. The Windows service GUID and Linux port mapping are not permission to invent a random undocumented WSL endpoint; use a documented application integration or own both endpoints and registration/configuration.
A socket supplies an ordered byte stream where supported, not application message boundaries. Define framing explicitly:
uint32 network_order_length
uint16 protocol_version
uint16 message_type
bounded payload
Reject lengths above a small maximum before allocation, read until the declared frame is complete, and handle EOF in the middle of a frame. Serialize integers and text encodings explicitly so Windows and Linux structures are never copied with compiler padding or native endianness assumptions.
“Not IP” does not mean “trusted”
Hyper-V socket traffic bypasses ordinary TCP port binding and IP routing, so Windows Firewall rules written only for TCP addresses may not describe it. The transport identifies virtualization endpoints, but an application still needs authentication and authorization. Another process with access to the service or a compromised guest can send malformed requests.
Use an authenticated handshake tied to a locally provisioned secret or public-key identity, validate protocol version and role, apply least privilege on the Windows service and Linux daemon, and rate-limit connections. Do not transfer a privileged Windows handle or execute guest-provided paths merely because the bytes arrived through Hyper-V.
If confidentiality against host administrators is a requirement, WSL 2 is the wrong boundary: the Windows host manages the VM. Encryption can protect protocol mistakes and other processes, but it does not turn the guest into a confidential VM against its host.
WSL lifetime is observable state
A distro can stop after its activity ends, wsl --shutdown terminates the WSL VM, updates can replace components, and suspend/restart tears down channels. Neither endpoint should assume a socket survives indefinitely. Implement reconnect with bounded backoff, heartbeat only when it provides real liveness, idempotent requests, and clean handling of duplicated work after a retry.
Multiple installed distributions also need identity. Do not assume one global “WSL guest.” Bind configuration to the intended distribution/VM context and ensure a cloned/imported distro cannot impersonate another application instance with copied credentials.
Choose sockets for the right reason
Hyper-V sockets are useful for host/guest control planes that should work before IP configuration or avoid exposing an IP listener. Ordinary development services are usually simpler over documented WSL localhost/mirrored networking. Windows/WSL executable interop or a file protocol may be better for one-shot commands.
Test partial frames, oversized input, concurrent clients, wrong identity, distro terminate/restart, WSL update, Windows sleep, multiple distros, service crash, and host reboot. The transport removes IP addressing; it does not remove protocol, security, compatibility, or lifecycle engineering.
Related:
- WSL Localhost Forwarding: How Windows Reaches Linux Services and Where It Breaks
- How to Build and Register a Custom WSL Distribution from a Root Filesystem
Sources: