In modern operating systems, networking is not limited to physical Ethernet interfaces (eth0
, en0
, etc.). There are a variety of virtual networking abstractions and constructs—like tun
, tap
, bridge
, veth
, subnet
—that enable everything from virtual machines and containers to VPNs and complex local network simulations.
This document will break down how these components work, why they exist, and how they fit together. Examples will focus on Linux and macOS, with key differences noted.
-
Physical Interfaces: The ports on your machine—like
eth0
on Linux oren0
on macOS—represent actual hardware network adapters. They connect to physical networks and transmit data over cables (Ethernet) or radio waves (Wi-Fi). -
Virtual Interfaces: Software constructs that behave like network interfaces but do not correspond to a physical device. Instead, they pass packets between software endpoints. Examples include
tun
,tap
,veth
, and others.
Conceptual Overview:
TUN
and TAP
are virtual network interfaces. They are commonly used by VPN software, virtualization, and network simulation tools.
-
TUN (Network Tunneling):
- Layer: Operates at Layer 3 (IP layer).
- Behavior: A TUN interface sends and receives IP packets. It's like hooking directly into a router’s IP stack.
- Use Case: If you read data from a TUN device, you get raw IP packets. If you write IP packets to it, they appear to the kernel’s IP stack as if they arrived from some remote machine. This is ideal for encrypted tunnels or VPN endpoints that handle IP packets directly.
-
TAP (Network Tap):
- Layer: Operates at Layer 2 (Ethernet layer).
- Behavior: A TAP interface sends and receives Ethernet frames. This is like having a virtual Ethernet cable plugged into a software switch.
- Use Case: Useful when you need a full Ethernet simulation (e.g., bridging VMs into a LAN). Virtual machines often see a TAP device as a “virtual NIC” that can join a bridge, get DHCP leases, etc.
In Linux:
- You can create TUN/TAP interfaces via the
ip tuntap
command or oldertunctl
. - The kernel’s
tun
driver handles both TUN and TAP. - Example:
# Create a TAP device called tap0: sudo ip tuntap add mode tap tap0 sudo ip link set tap0 up
In macOS:
- Traditional TUN/TAP support used to be provided by third-party kernel extensions (like
tun/tap
from tuntaposx). - Modern macOS uses
utun
interfaces provided by the OS for TUN-like functionality. TAP equivalents are less common by default. - TUN in macOS integrates through
utun
devices, commonly used by VPN clients (e.g., WireGuard usesutun
).
Conceptual Overview:
veth
(virtual Ethernet) pairs are always created in pairs. Imagine them as two Ethernet interfaces connected by a virtual cable inside the OS kernel. Whatever packet you put into one end comes out the other. No external network needed.
Why do we need them?
- Containers and network namespaces use
veth
pairs to connect isolated network namespaces to the host’s networking stack. For instance, one end of theveth
pair goes inside a container’s namespace aseth0
for that container, and the other end remains in the host namespace. The host can then bridge or route the container’s traffic.
In Linux:
-
# Create a veth pair: veth0 and veth1 sudo ip link add veth0 type veth peer name veth1
- Move
veth1
into a container’s namespace. Now that container seesveth1
as its primary NIC, whileveth0
stays on the host and can be attached to a bridge or assigned an IP.
On macOS:
- macOS does not natively provide
veth
pairs in the same way Linux does. Virtualization solutions (Docker Desktop, HyperKit) emulate these features. Other approaches use frameworks likepf
andutun
or specialized kernel extensions.
Conceptual Overview:
A bridge
acts like a virtual switch (Layer 2 device). You can plug multiple interfaces (physical or virtual) into a bridge. All devices on the bridge appear to share the same Ethernet domain (broadcast domain).
Why do we need them?
- Allows VMs or containers to be on the same network as the host’s physical network, obtaining IP addresses via DHCP and communicating like physical machines on the LAN.
- Simplifies complex routing setups by making multiple virtual interfaces behave as if they were all on the same LAN segment.
In Linux:
- Use the
bridge
utility orip
command to create a bridge:sudo ip link add name br0 type bridge sudo ip link set br0 up # Add a physical NIC and a TAP interface to the bridge: sudo ip link set eth0 master br0 sudo ip link set tap0 master br0
On macOS:
- Bridges can be created via
ifconfig
:sudo ifconfig bridge0 create sudo ifconfig bridge0 addm en0 addm tap0 sudo ifconfig bridge0 up
Conceptual Overview:
A subnet
is a logical division of an IP network. For IPv4, a subnet is defined by an IP address and a subnet mask (e.g., 192.168.1.0/24
). Subnets organize IP addresses into manageable groups, define broadcast domains at the IP layer, and influence routing decisions.
Why do we need them?
- Helps structure networks, control broadcast domains, and manage IP address allocation.
- When you assign IP addresses to interfaces, you pick the correct subnet so the host knows which addresses are reachable directly vs. which require routing.
On Linux and macOS:
- Assigning an IP and subnet mask to an interface:
# Linux: sudo ip addr add 192.168.1.10/24 dev eth0 # macOS: sudo ifconfig en0 192.168.1.10 netmask 255.255.255.0
- macvlan / ipvlan (Linux): Virtual interfaces that let you assign multiple MAC or IP addresses to a single physical NIC for container isolation without bridging.
- Gre Tunnels / IP-in-IP (Linux): Encapsulate IP packets within other IP packets, enabling simple L3 tunnels.
- PF and Tunnel Interfaces (macOS): macOS uses
pf
firewall andutun
interfaces to handle VPN tunnels and traffic redirection. Some advanced setups may useTapFilter
or custom VPN plugins.
+-------------------------+
| USERSPACE |
| (VPN Daemon, etc.) |
+-----------+-------------+
|
| IP Packets (L3)
v
+-------------------------+
| TUN |
| (Layer 3 Interface) |
+-----------+-------------+
|
| Ethernet Frames (L2)
v
+-------------------------+
| TAP |
| (Layer 2 Interface) |
+-----------+-------------+
TUN works directly with IP packets; TAP works with Ethernet frames. The TUN interface gives you something akin to a point-to-point IP tunnel. TAP gives you a “virtual cable” to plug into a software switch or bridge.
+-------------------+
| HOST |
| Namespace |
| |
| veth0 |
| | |
| | (virtual) |
v v
+------------------------+
| br0 (bridge) |
| +---------+----------+|
| | eth0 | tap0 ||
+--|---------|----------+
|
| (physical network)
v
+-------------------+
| CONTAINER |
| Namespace |
| veth1 (peer) |
| IP: 192.168.1.5 |
+-------------------+
Here, veth0
and veth1
form a pair. veth0
is in the host namespace and plugged into the br0
bridge. veth1
is in the container namespace as eth0
inside the container. This allows the container to appear on the same subnet as the host or LAN.
-
Network Isolation: Containers, VMs, and user processes often need isolated networking environments that behave as if they are on separate machines or even separate networks. Virtual interfaces (
veth
,tun/tap
) and namespaces provide this. -
Integration with Real Networks: Bridges connect virtual endpoints (TAP devices, veth endpoints) to real networks. This allows seamless integration, where a container can get a DHCP lease from your router as if it were a physical machine on the LAN.
-
Flexible Networking Topologies: With TUN and TAP, you can build VPNs or complex routing topologies. With bridging, you can simulate switches. With veth pairs, you connect isolated namespaces easily. These tools form building blocks for any desired network scenario, all in software.
- eth0/en0 (Physical Interface): Connects to actual hardware, either Ethernet or Wi-Fi.
- TUN Interfaces: Virtual, handle IP packets, perfect for VPNs.
- TAP Interfaces: Virtual, handle Ethernet frames, enabling full Ethernet simulation.
- Bridges: Virtual switches that aggregate multiple interfaces into a single broadcast domain.
- Veth Pairs: Virtual cables connecting two namespaces or environments internally.
- Subnets: Logical grouping of IP addresses, controlling routing and broadcast domains.
On Linux, these features are well-integrated into the kernel and the ip
suite. On macOS, utun
and ifconfig
handle many tasks, though full parity with Linux’s virtualization options often requires additional tooling or kernel extensions.
All these components work together to form the foundation of modern virtualization, containerization, and secure tunneling setups.