24.3. Connecting Guests to Networks

Libvirt networks are host-side objects that describe a bridge, an address range, forwarding rules, and optionally DHCP. A guest's NIC can also attach directly to an existing host bridge without a libvirt network object. The choice determines who assigns addresses, whether the guest is reachable from outside the host, and how much of the host's network configuration is visible to the guest.

24.3.1. Libvirt NAT Networks and Address Assignment

A NAT network gives guests a private address range behind a host bridge, with libvirt's dnsmasq providing DHCP and DNS and the host forwarding outbound traffic. The guest can reach the Internet and other guests on the same libvirt network, but unsolicited inbound connections from outside the host do not reach the guest. The network is defined with XML; the following content goes in /etc/libvirt/qemu/networks/handbook-nat.xml on virt1:

<network>
  <name>handbook-nat</name>
  <forward mode='nat'/>
  <bridge name='virbr24' stp='on' delay='0'/>
  <ip address='192.168.236.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.236.100' end='192.168.236.199'/>
    </dhcp>
  </ip>
</network>

The forward mode='nat' element asks libvirt to install masquerading and forwarding rules. The ip element gives the host bridge an address and defines the DHCP pool; without a dhcp section, guests would need static addresses. The bridge name is chosen by the administrator; virbrN is only a convention. On virt1, in the root shell, define and start it:

# virsh -c qemu:///system net-define /etc/libvirt/qemu/networks/handbook-nat.xml
# virsh -c qemu:///system net-start handbook-nat
# virsh -c qemu:///system net-autostart handbook-nat

The three commands form one setup sequence; each prints a one-line confirmation. Verify the effective XML; libvirt adds a UUID and the masquerade port range:

# virsh -c qemu:///system net-dumpxml handbook-nat
<network connections='1'>
  <name>handbook-nat</name>
  <uuid>a365b3e4-486d-447e-b9e5-051ae8028d32</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr24' stp='on' delay='0'/>
  <mac address='52:54:00:11:0b:65'/>
  <ip address='192.168.236.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.236.100' end='192.168.236.199'/>
    </dhcp>
  </ip>
</network>

The connections='1' attribute shows one guest NIC attached. The port range bounds the source ports that the host's masquerading rules use for translated connections. The host bridge has the address .1, and the DHCP pool is .100 through .199; the guest receives one of those addresses.

On virt1, as john, inspect the lease the guest obtained:

$ sudo virsh -c qemu:///system net-dhcp-leases handbook-nat
 Expiry Time           MAC address         Protocol   IP address           Hostname       Client ID or DUID
----------------------------------------------------------------------------------------------------------------------------------------------------
 2026-11-23 09:08:48   52:54:00:ec:63:66   ipv4       192.168.236.117/24   handbook-web   ff:56:50:4d:98:00:02:00:00:ab:11:74:c4:fe:f7:90:09:22:b4

The lease binds the guest's MAC to its address and hostname. The client ID is what the DHCP client sent; it is not the same thing as the MAC, and some clients use a DUID. A guest may keep the same lease for the DHCP lifetime or request a new one after reboot.

On the guest, as john, confirm the route and outbound access:

$ ip route
default via 192.168.236.1 dev enp1s0 proto dhcp src 192.168.236.117 metric 100
192.168.236.0/24 dev enp1s0 proto kernel scope link src 192.168.236.117 metric 100
192.168.236.1 dev enp1s0 proto dhcp scope link src 192.168.236.117 metric 100

The default route points at the host bridge. The host performs NAT and forwarding, so the guest can reach external addresses. A ping of the bridge address shows the first hop:

$ ping -c 2 192.168.236.1
PING 192.168.236.1 (192.168.236.1) 56(84) bytes of data.
64 bytes from 192.168.236.1: icmp_seq=1 ttl=64 time=1.41 ms
64 bytes from 192.168.236.1: icmp_seq=2 ttl=64 time=0.605 ms

--- 192.168.236.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.605/1.006/1.408/0.401 ms

A quick HTTP check shows the full path works:

$ curl -fsS -o /dev/null -w '%{http_code}\n' http://deb.debian.org/
200

curl -fsS fails on HTTP errors and suppresses progress; -o /dev/null discards the body and -w prints only the status code. A 000 result indicates that the connection failed before an HTTP response; check the guest route, the host's forwarding rules, and DNS.

Network mode Guest address source Inbound from outside host Host setup Typical use
NAT libvirt DHCP or static in range Not without port forwarding libvirt-managed bridge and firewall rules Lab guests, outbound-only workloads
Routed Static or DHCP on a routed prefix Yes, if host routes and firewall allow Host route and forwarding rules Guests needing public addresses
Bridge External DHCP or static Yes, as a LAN peer Existing host bridge and uplink Servers that must appear on the LAN
Isolated libvirt DHCP or static No libvirt bridge with no forwarding Recovery, private service tests

NAT is the default choice when guests need updates and management but do not need to accept connections from the wider network. Port forwarding can publish a specific service, but it complicates address changes and should be documented with the guest definition.

24.3.2. Attaching Guests to an Existing Host Bridge

A bridge makes the guest a peer on the host's LAN. The host needs a bridge device, an uplink enslaved to it, and an address if the host itself uses that LAN. On virt1, in the root shell, create a simple bridge with a dummy uplink for the exercise:

# ip link add br-ch24 type bridge
# ip link add dummy-ch24 type dummy
# ip link set dummy-ch24 master br-ch24
# ip addr add 192.168.237.1/24 dev br-ch24
# ip link set br-ch24 up
# ip link set dummy-ch24 up

None of the commands prints output on success. The dummy interface stands in for a physical uplink; it keeps the bridge up and lets the host and guest exchange traffic without touching the management NIC. On a production host, replace it with the real uplink and move the host's address from the uplink to the bridge. That change can interrupt SSH if performed remotely, so use the management interface or a local console, as in Chapter 19. Verify the bridge address:

# ip -br addr show br-ch24
br-ch24          UP             192.168.237.1/24 fe80::48e4:6ff:fe0c:c355/64

A libvirt network can describe a bridge that libvirt did not create. The XML is minimal because the host owns the bridge and its addressing; the following content goes in /etc/libvirt/qemu/networks/handbook-bridge.xml on virt1:

<network>
  <name>handbook-bridge</name>
  <forward mode='bridge'/>
  <bridge name='br-ch24'/>
</network>

In the root shell, define and start it:

# virsh -c qemu:///system net-define /etc/libvirt/qemu/networks/handbook-bridge.xml
# virsh -c qemu:///system net-start handbook-bridge

net-dumpxml returns the same three elements plus a UUID. libvirt does not run DHCP on a bridge network unless the XML contains an ip element, and it does not add firewall rules for a bridge the host already manages.

Attach a new virtio NIC to the running guest. On virt1, as john:

$ sudo virsh -c qemu:///system attach-interface handbook-web bridge br-ch24 --model virtio --config --live
Interface attached successfully

The options of attach-interface:

attach-interface option Effect
bridge NAME Attach to an existing host bridge
network NAME Attach to a libvirt network
--model virtio Use the virtio NIC driver
--config Persist the interface in the domain XML
--live Attach to the running domain now
--current Change the current definition without persisting

--live changes the running domain; --config records the change in the persistent definition. Without --config, the interface disappears at the next shutdown. Without --live, the guest must be restarted to see it. The guest now has a second interface. On the guest, as john, configure it with an address in the bridge subnet:

$ sudo ip addr add 192.168.237.10/24 dev enp7s0
$ sudo ip link set enp7s0 up

Both commands are silent on success. Verify the address:

$ ip -br addr show enp7s0
enp7s0           UP             192.168.237.10/24 fe80::5054:ff:fec6:51e9/64

This is a temporary address; it survives only until reboot or a network manager change. For persistence, put the address in the guest's network configuration, as in Chapter 17. Verify from the host, in the root shell:

# ping -c 2 192.168.237.10
PING 192.168.237.10 (192.168.237.10) 56(84) bytes of data.
64 bytes from 192.168.237.10: icmp_seq=1 ttl=64 time=1.45 ms
64 bytes from 192.168.237.10: icmp_seq=2 ttl=64 time=0.477 ms

--- 192.168.237.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.477/0.965/1.454/0.488 ms

The host reaches the guest over the bridge without NAT. A guest on the bridge can reach other bridge peers and, if the bridge is connected to a real uplink, the physical LAN. It can also receive inbound connections, which makes firewall policy on the guest and the LAN important.

If the guest already has a default route on the NAT interface, adding a bridge interface does not automatically make it preferred. Address selection and routing depend on the guest's configuration; test with ip route get before assuming which path traffic takes.

24.3.3. Isolated Networks for Service and Recovery Exercises

An isolated network has a bridge and an IP address but no forwarding element. The host can reach the guests, and the guests can reach each other, but there is no NAT and no route to the outside. This is useful for recovery exercises, malware analysis, and service tests that should not depend on external DNS or an Internet connection. The following content goes in /etc/libvirt/qemu/networks/handbook-isolated.xml on virt1:

<network>
  <name>handbook-isolated</name>
  <bridge name='virbr25' stp='on' delay='0'/>
  <ip address='192.168.238.1' netmask='255.255.255.0'>
  </ip>
</network>

The empty ip element deliberately provides no DHCP range. Guests can use static addresses, or you can add a dhcp child if you want libvirt to assign them. On virt1, in the root shell, define and start it:

# virsh -c qemu:///system net-define /etc/libvirt/qemu/networks/handbook-isolated.xml
# virsh -c qemu:///system net-start handbook-isolated

Inspect the effective definition:

# virsh -c qemu:///system net-dumpxml handbook-isolated
<network>
  <name>handbook-isolated</name>
  <uuid>66c29fbb-ec9e-41e6-9564-26ad70fda87c</uuid>
  <bridge name='virbr25' stp='on' delay='0'/>
  <mac address='52:54:00:9e:50:41'/>
  <ip address='192.168.238.1' netmask='255.255.255.0'>
  </ip>
</network>

There is no <forward> element. libvirt creates the bridge and the host address but does not install NAT or forwarding. Attach the guest:

# virsh -c qemu:///system attach-interface handbook-web network handbook-isolated --model virtio --config --live
Interface attached successfully

On the guest, as john, assign an address and bring the interface up:

$ sudo ip addr add 192.168.238.10/24 dev enp8s0
$ sudo ip link set enp8s0 up

Both commands are silent. Confirm the address:

$ ip -br addr show enp8s0
enp8s0           UP             192.168.238.10/24 fe80::5054:ff:fe8c:d6f3/64

Then inspect the routing table:

$ ip route
default via 192.168.236.1 dev enp1s0 proto dhcp src 192.168.236.117 metric 100
192.168.236.0/24 dev enp1s0 proto kernel scope link src 192.168.236.117 metric 100
192.168.236.1 dev enp1s0 proto dhcp scope link src 192.168.236.117 metric 100
192.168.237.0/24 dev enp7s0 proto kernel scope link src 192.168.237.10
192.168.238.0/24 dev enp8s0 proto kernel scope link src 192.168.238.10

The guest still has its NAT default route because the guest also has a NAT NIC. A guest with only the isolated NIC would have no default route and no Internet access; that is the intended isolation. The host reaches the isolated address directly. On virt1, in the root shell:

# ping -c 2 192.168.238.10
PING 192.168.238.10 (192.168.238.10) 56(84) bytes of data.
64 bytes from 192.168.238.10: icmp_seq=1 ttl=64 time=0.925 ms
64 bytes from 192.168.238.10: icmp_seq=2 ttl=64 time=0.326 ms

--- 192.168.238.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.326/0.625/0.925/0.299 ms

Isolation is a property of the network definition, not of the guest firewall. If you later add a forwarded port or a route, the network is no longer isolated.

NAT, bridged, and isolated libvirt networks

Figure 24.3. NAT hides guests behind the host; a bridge makes them LAN peers; an isolated network gives the host local reachability without an outside path.

Use isolated networks for service tests that must not leak traffic, and keep a management NIC on a separate network for recovery. A guest with only an isolated NIC cannot fetch packages or reach a time server; install the packages and prepare configuration before isolating it, or provide a local mirror on the isolated segment.

24.3.4. Guest Firewall and Host Firewall Interactions

Packets between a host and a guest on a libvirt network pass through several filtering points. For NAT, the guest's own firewall sees inbound packets after libvirt's bridge and NAT rules. For a bridge, the guest is a LAN peer and the host's firewall may see the traffic only if it is routed through the host or filtered on the bridge. The guest's firewall is the first line for services running inside it; the host's firewall and the network's forwarding policy control what can reach the guest at all.

Start a simple service in the guest to demonstrate the guest firewall. On the guest, as john, create a page and run a temporary HTTP server:

$ mkdir -p ~/debian-handbook/virtualization/service
$ echo handbook-service > ~/debian-handbook/virtualization/service/index.html
$ cd ~/debian-handbook/virtualization/service

The commands create the directory and page and change into it; none prints output. Start the server in the foreground:

$ python3 -m http.server 8080 --bind 0.0.0.0

The server runs in the foreground and prints a line for each request; leave it running and open another guest shell for the next command. Stop it with Ctrl+C when the exercise is done. Do not use this as a production service; it has no access control and no TLS. In the second shell, confirm that it is listening:

$ ss -ltn | grep 8080
LISTEN 0      5            0.0.0.0:8080      0.0.0.0:*

The line shows a listening TCP socket on all interfaces, port 8080. From the host, as john, the service is reachable through the NAT address:

$ curl -fsS --max-time 3 http://192.168.236.117:8080/
handbook-service

Now block TCP port 8080 inside the guest. On the guest, as john, in the second shell:

$ sudo nft add table inet ch24
$ sudo nft 'add chain inet ch24 input { type filter hook input priority 0; policy accept; }'
$ sudo nft add rule inet ch24 input tcp dport 8080 drop

The three commands form one rule-setup sequence and are silent on success. List the table to confirm the rule:

$ sudo nft list table inet ch24
table inet ch24 {
    chain input {
        type filter hook input priority filter; policy accept;
        tcp dport 8080 drop
    }
}

The table is independent of the distribution's firewall service. It is loaded into the kernel immediately and is not persistent across reboot unless you add it to /etc/nftables.conf or a service unit. The rule drops new inbound TCP connections to 8080; established connections are not matched by this simple rule because it has no ct state expression.

From the host, as john, the request now times out:

$ curl -sS --max-time 3 http://192.168.236.117:8080/
curl: (28) Connection timed out after 3002 milliseconds

The timeout, rather than a connection refused, shows that packets are being dropped silently. A refused connection would mean the guest received the packet and no process was listening. The host's NAT rules are not the cause; the service worked before the guest rule was added.

Remove the table to restore access. On the guest, as john:

$ sudo nft delete table inet ch24

The command is silent on success. From the host, the service answers again:

$ curl -fsS --max-time 3 http://192.168.236.117:8080/
handbook-service

The host firewall has a different role. libvirt's NAT network installs forwarding and masquerade rules; a host firewall that drops forwarded traffic can block guests even when the guest firewall is open. On virt1, in the root shell, look for the chains libvirt maintains:

# nft list ruleset | grep -E 'LIBVIRT|masquerade' | head -20
    chain LIBVIRT_INP {
        counter packets 179431 bytes 243725149 jump LIBVIRT_INP
    chain LIBVIRT_OUT {
        counter packets 275625 bytes 8571645399 jump LIBVIRT_OUT
    chain LIBVIRT_FWO {
        counter packets 28579 bytes 273070004 jump LIBVIRT_FWX
        counter packets 28579 bytes 273070004 jump LIBVIRT_FWI
        counter packets 5582 bytes 401107 jump LIBVIRT_FWO
[...]
    chain LIBVIRT_PRT {
        ip saddr 192.168.236.0/24 ip daddr != 192.168.236.0/24 ip protocol tcp counter packets 4 bytes 240 masquerade to :1024-65535
        ip saddr 192.168.236.0/24 ip daddr != 192.168.236.0/24 ip protocol udp counter packets 4 bytes 304 masquerade to :1024-65535
        ip saddr 192.168.236.0/24 ip daddr != 192.168.236.0/24 counter packets 0 bytes 0 masquerade
        counter packets 1066 bytes 93179 jump LIBVIRT_PRT

Selected output; the filter keeps the first 20 lines that mention libvirt's chains or masquerading, and further matching lines are omitted here. The LIBVIRT_* chains are libvirt's hook points in the host's input, output, forward, and postrouting paths, and the masquerade rules translate the 192.168.236.0/24 guest range. The same listing also prints warnings that the legacy ip filter, ip nat, and ip mangle tables are managed by iptables-nft and should not be edited. Do not flush the host's ruleset to "fix" a guest: that removes the management network's rules and libvirt's chains along with whatever you meant to clear. Add explicit rules or use libvirt's hooks, and keep the management path out of the experiment.

Filtering point What it sees What it controls Where to configure
Guest firewall Traffic arriving at the guest NIC Services inside the guest nftables, firewalld, or the guest's distribution firewall
Host forward filter Traffic routed between networks Whether NAT or routed guests can pass Host nftables/iptables forward chain, libvirt rules
Bridge filter Frames on a host bridge Peer-to-peer traffic on a LAN bridge Bridge netfilter or switch ACLs, depending on topology
libvirt network definition Forwarding mode and addresses NAT, routed, isolated, or bridged behavior virsh net-edit
Upstream firewall Traffic leaving the physical host Internet and LAN reachability Site firewall or router

For a service that must be reachable from outside, prefer a bridge or an explicit port forward, document the address, and test from an independent client. Testing only from the host can succeed through a path that external clients do not have.