Linux Networking: Bridged and Routed VM Networking
Scenario 1: Bridged Networking (Layer 2 Integration)
Goal: VMs/containers appear as full LAN peers, get IPs from the LAN’s DHCP, and are reachable directly.
- Create a bridge interface (
br0). - Add the physical interface (e.g.,
eth0) to the bridge. - Assign IP or DHCP to
br0only.eth0should have no IP. - Virtual interfaces (e.g.,
tap0) are also added tobr0.
Key Commands:
No NAT, no subnetting. Full LAN access.
Scenario 2: Routed Subnet for VMs (Layer 3 Isolation)
Goal: Create a dedicated VM network (10.0.0.0/24) separate from the LAN (192.168.1.0/24). The host routes between them.
Steps:
- Create a bridge for VMs:
- Enable IP forwarding:
- Add NAT to route external traffic:
| |
- Attach VM interfaces (
tapX,vethX) tobr0. - (Optional) Run a DHCP server (e.g., dnsmasq) on
br0.
Gives you subnet isolation, control, and routing. Useful for test environments, services segregation, or firewall zones.
Step-by-Step: Create and Attach a TAP Interface for a VM
1. Create a TAP Device
You must create it as the user that will run the VM, or as root.
| |
tap0is the virtual NIC.mode tapmeans Ethernet-like interface.userensures your VM process (e.g., QEMU/KVM) can access the device.
2. Attach the TAP Device to the Bridge
Now, tap0 is a bridge port just like a physical NIC. Anything connected to it is part of the bridge network (br0).
3. Launch Your VM with TAP Networking
If you’re using QEMU/KVM directly:
-netdev tap: Connects QEMU totap0.script=no: Tells QEMU not to run legacy up/down scripts.virtio-net-pci: Fast virtual NIC (recommended).
4. Configure Networking Inside the VM
Inside the guest OS (e.g., Ubuntu):
- Use DHCP (auto-configure if
br0has access to a DHCP server). - Or assign a static IP on the appropriate subnet (
10.0.0.x/24if routed, or whatever your LAN is if bridged toeth0).
Comparison
| Mode | L2 Bridging | L3 Routed Subnet |
|---|---|---|
| Host uses | br0 for LAN | eth0 (LAN), br0 (VM subnet) |
| VMs get IP | From LAN DHCP | From internal DHCP or static |
| IP Forwarding | Not needed | Required |
| NAT Needed | No | Optional (only if no static routes on LAN) |
| Isolation | None (same LAN) | Full (separate network) |
| Routing | Bridged | Host-level router |