Linux Containers by Hand. Part 6: Connecting a container with veth, a bridge, and NAT
All parts of the series “Linux Containers by Hand”In Part 1, the CNI bridge and portmap plugins set up networking with the lab0 bridge, a veth pair, an address from the subnet, and iptables rules. The containers in Parts 2 through 5 had only lo. Today we will build the network with ip and iptables, inspect packets with tcpdump, and connect the container from Part 3.
The script that attaches any network namespace to the bridge is in the linux-containers-by-hand repository.
Lab setup
| Item | Value |
|---|---|
| Machine | lincont-lab.example.com, 192.168.10.31 on eth0, Hyper-V VM |
| OS | Ubuntu Server 26.04.1, kernel 7.0.0, iptables 1.8.11 (nf_tables) |
| Network DNS server | 192.168.10.1 |
| Another machine on the network | 192.168.10.50 |
| Container subnet | 10.6.0.0/24, bridge lab6 at 10.6.0.1 |
Run all commands as root (sudo -i). The machine has just rebooted.
ip -br link
sysctl net.ipv4.ip_forward
iptables -S
iptables -t nat -S
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 00:15:5d:02:a9:07 <BROADCAST,MULTICAST,UP,LOWER_UP>
net.ipv4.ip_forward = 0
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
There are two interfaces, forwarding between them is off, and there are no iptables rules. The CNI bridges and rules from Part 1 persist until reboot. If you still have them, they use different subnets and will not interfere.
Create a named network namespace
In Part 3, the network namespace lived with the unshare process. ip netns add creates a namespace with an independent lifetime.
ip netns add c6a
ls -l /run/netns
findmnt -n -o TARGET,SOURCE,FSTYPE /run/netns/c6a | sort -u
ip netns exec c6a ip a
total 0
-r--r--r-- 1 root root 0 Nov 10 20:28 c6a
/run/netns/c6a nsfs[net:[4026532137]] nsfs
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
The namespace is bind-mounted at /run/netns/c6a through nsfs. The namespace exists as long as the file is mounted, even if it has no processes. ip netns exec runs a command there, like nsenter -n. Inside we see lo down, as in Part 3.
Connect a veth pair
A veth pair is like the ends of a cable. Anything sent into one virtual interface comes out of the other. Create a pair and move the second end into c6a as eth0.
ip link add veth-c6a type veth peer name eth0 netns c6a
ip -br link show veth-c6a
ip netns exec c6a ip -br link
veth-c6a@if2 DOWN c6:66:aa:0f:10:ea <BROADCAST,MULTICAST>
lo DOWN 00:00:00:00:00:00 <LOOPBACK>
eth0@if3 DOWN 96:53:cc:cc:a5:12 <BROADCAST,MULTICAST>
@if2 and @if3 identify each interface’s peer. veth-c6a is interface 3 on the host, and eth0 is interface 2 in the namespace. Each has its own MAC address, the link-layer address used by Ethernet frames. Assign IP addresses and bring both ends up.
ip addr add 10.6.0.1/24 dev veth-c6a
ip link set veth-c6a up
ip netns exec c6a ip addr add 10.6.0.2/24 dev eth0
ip netns exec c6a ip link set eth0 up
ip netns exec c6a ip link set lo up
ip netns exec c6a ip -br a
ip netns exec c6a ip route
ip route | grep 10.6
ping -c 2 10.6.0.2
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if3 UP 10.6.0.2/24 fe80::9453:ccff:fecc:a512/64
10.6.0.0/24 dev eth0 proto kernel scope link src 10.6.0.2
10.6.0.0/24 dev veth-c6a proto kernel scope link src 10.6.0.1
PING 10.6.0.2 (10.6.0.2) 56(84) bytes of data.
64 bytes from 10.6.0.2: icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from 10.6.0.2: icmp_seq=2 ttl=64 time=0.028 ms
--- 10.6.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1010ms
rtt min/avg/max/mdev = 0.018/0.023/0.028/0.005 ms
A /24 address automatically adds a route. The 10.6.0.0/24 subnet is directly reachable through that interface (scope link). The host and namespace can now reach each other. ping sends ICMP echo requests and the destination returns echo replies.
Set up a bridge
A veth pair connects two nodes. To connect several containers, attach their host-side veth interfaces to a bridge, a software switch in the kernel. Move the gateway address from veth-c6a to the bridge, make veth-c6a a bridge port, and create a second namespace, c6b, at 10.6.0.3.
ip addr del 10.6.0.1/24 dev veth-c6a
ip link add lab6 type bridge
ip addr add 10.6.0.1/24 dev lab6
ip link set lab6 up
ip link set veth-c6a master lab6
ip netns add c6b
ip link add veth-c6b type veth peer name eth0 netns c6b
ip link set veth-c6b master lab6
ip link set veth-c6b up
ip netns exec c6b ip addr add 10.6.0.3/24 dev eth0
ip netns exec c6b ip link set eth0 up
ip netns exec c6b ip link set lo up
bridge link
ip netns exec c6a ip -br link show eth0
ip netns exec c6b ip -br link show eth0
3: veth-c6a@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master lab6 state forwarding priority 32 cost 2
5: veth-c6b@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master lab6 state forwarding priority 32 cost 2
eth0@if3 UP 96:53:cc:cc:a5:12 <BROADCAST,MULTICAST,UP,LOWER_UP>
eth0@if5 UP 46:6d:e0:bd:9b:88 <BROADCAST,MULTICAST,UP,LOWER_UP>
Inspect traffic on the bridge
Watch packets between c6a and c6b. Run tcpdump on the bridge in the second terminal. -e shows MAC addresses, -t omits timestamps, and -c 6 stops after six packets. Send tcpdump’s status messages to /dev/null.
tcpdump -i lab6 -n -e -t -c 6 'arp or icmp' 2> /dev/null
Run this in the first terminal.
ip netns exec c6a ping -c 2 10.6.0.3
Here is the tcpdump output.
96:53:cc:cc:a5:12 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.6.0.3 tell 10.6.0.2, length 28
46:6d:e0:bd:9b:88 > 96:53:cc:cc:a5:12, ethertype ARP (0x0806), length 42: Reply 10.6.0.3 is-at 46:6d:e0:bd:9b:88, length 28
96:53:cc:cc:a5:12 > 46:6d:e0:bd:9b:88, ethertype IPv4 (0x0800), length 98: 10.6.0.2 > 10.6.0.3: ICMP echo request, id 1511, seq 1, length 64
46:6d:e0:bd:9b:88 > 96:53:cc:cc:a5:12, ethertype IPv4 (0x0800), length 98: 10.6.0.3 > 10.6.0.2: ICMP echo reply, id 1511, seq 1, length 64
96:53:cc:cc:a5:12 > 46:6d:e0:bd:9b:88, ethertype IPv4 (0x0800), length 98: 10.6.0.2 > 10.6.0.3: ICMP echo request, id 1511, seq 2, length 64
46:6d:e0:bd:9b:88 > 96:53:cc:cc:a5:12, ethertype IPv4 (0x0800), length 98: 10.6.0.3 > 10.6.0.2: ICMP echo reply, id 1511, seq 2, length 64
On the local network an Ethernet frame carries the IP packet and uses a MAC destination. c6a first needs the MAC address for 10.6.0.3. ARP resolves it. The request asking who has 10.6.0.3 and to tell 10.6.0.2 goes to the broadcast address ff:ff:ff:ff:ff:ff, and the bridge floods it to the other ports. The owner answers the requester directly. The ICMP echoes then travel between the two MAC addresses. Both endpoints remember what they learned.
ip netns exec c6a ip neigh
bridge fdb show br lab6 | grep -v permanent
10.6.0.1 dev eth0 lladdr c6:66:aa:0f:10:ea DELAY
10.6.0.3 dev eth0 lladdr 46:6d:e0:bd:9b:88 REACHABLE
96:53:cc:cc:a5:12 dev veth-c6a master lab6
46:6d:e0:bd:9b:88 dev veth-c6b master lab6
ip neigh shows c6a’s ARP table, mapping neighbor IPs to MAC addresses. The 10.6.0.1 entry is left over from the host’s ping through veth-c6a. bridge fdb shows the bridge forwarding database, mapping MAC addresses to ports. The bridge learns from source addresses in passing frames and can then forward a frame to the correct port.
Reach the outside network
Add a default route
Try reaching the host at its external address.
ip netns exec c6a ping -c 1 -W 1 192.168.10.31
ping: connect: Network is unreachable
The namespace only has a route to 10.6.0.0/24. Add a default route via the bridge address, which belongs to the host.
for n in c6a c6b; do ip netns exec $n ip route add default via 10.6.0.1; done
ip netns exec c6a ip route
ip netns exec c6a ping -c 1 192.168.10.31
ip netns exec c6a ping -c 2 -W 2 1.1.1.1
default via 10.6.0.1 dev eth0
10.6.0.0/24 dev eth0 proto kernel scope link src 10.6.0.2
PING 192.168.10.31 (192.168.10.31) 56(84) bytes of data.
64 bytes from 192.168.10.31: icmp_seq=1 ttl=64 time=0.029 ms
--- 192.168.10.31 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
--- 1.1.1.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1052ms
The host now answers at 192.168.10.31 because the packet reached a machine that owns that address. 1.1.1.1 still does not respond.
Enable forwarding
The host must forward packets for 1.1.1.1 from the bridge out through eth0. This is routing, and Linux defaults to forwarding disabled. Enable it and watch eth0 in the second terminal.
sysctl -w net.ipv4.ip_forward=1
tcpdump -i eth0 -n -t -c 2 icmp 2> /dev/null
ip netns exec c6a ping -c 2 -W 2 1.1.1.1
Here is the tcpdump output.
IP 10.6.0.2 > 1.1.1.1: ICMP echo request, id 1526, seq 1, length 64
IP 10.6.0.2 > 1.1.1.1: ICMP echo request, id 1526, seq 2, length 64
Requests now leave, but no replies return. The source is 10.6.0.2. That subnet exists only inside our host, and the network router and 1.1.1.1 have no route back to it.
NAT
Use network address translation (NAT). The host replaces the source with its own address. When a reply arrives, it restores the original destination and delivers the packet to the container. Add a MASQUERADE rule in the nat table’s POSTROUTING chain for traffic from 10.6.0.0/24 leaving through any interface other than the bridge.
iptables -t nat -A POSTROUTING -s 10.6.0.0/24 ! -o lab6 -j MASQUERADE
tcpdump -i eth0 -n -t -c 2 icmp 2> /dev/null
ip netns exec c6a ping -c 1 -W 2 1.1.1.1
iptables -t nat -L POSTROUTING -v -n
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=54 time=10.0 ms
--- 1.1.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 10.036/10.036/10.036/0.000 ms
Chain POSTROUTING (policy ACCEPT 1 packets, 180 bytes)
pkts bytes target prot opt in out source destination
1 84 MASQUERADE all -- * !lab6 10.6.0.0/24 0.0.0.0/0
Here is the tcpdump output.
IP 192.168.10.31 > 1.1.1.1: ICMP echo request, id 1537, seq 1, length 64
IP 1.1.1.1 > 192.168.10.31: ICMP echo reply, id 1537, seq 1, length 64
The outgoing request now uses the host’s address, and the reply arrives at that address. The rule counter shows one packet because MASQUERADE handles only the first packet of a connection. The kernel records the connection in conntrack and applies the translation in both directions to subsequent packets. MASQUERADE uses the egress interface’s address, so the rule does not need to specify the host address. The CNI bridge plugin made the same kind of rules, one chain per container, with ipMasq: true in Part 1.
DNS
DNS resolves names like cloudflare.com to addresses. Programs read the DNS server address from /etc/resolv.conf.
ip netns exec c6a dig +short +time=2 +tries=1 cloudflare.com A
grep -v '^#' /etc/resolv.conf | grep .
;; communications error to 127.0.0.53#53: connection refused
;; no servers could be reached
nameserver 127.0.0.53
options edns0 trust-ad
search .
The command under ip netns exec sees the host’s files, so dig reads the host’s /etc/resolv.conf. It lists the systemd-resolved stub at 127.0.0.53 on host loopback. Our namespace has its own lo and nothing listens on that address there. ip netns exec supports an override: when /etc/netns/<name>/resolv.conf exists, the command mounts it over /etc/resolv.conf. Use the first upstream server from systemd-resolved.
mkdir -p /etc/netns/c6a
grep -m1 '^nameserver [0-9.]*$' /run/systemd/resolve/resolv.conf > /etc/netns/c6a/resolv.conf
ip netns exec c6a cat /etc/resolv.conf
ip netns exec c6a dig +short cloudflare.com A
nameserver 192.168.10.1
104.16.132.229
104.16.133.229
The DNS request reaches port 53 on 192.168.10.1 through the same NAT and the reply comes back. Container tools do this too. In Part 1, nerdctl wrote a resolv.conf listing the host’s upstream servers.
Publish a port
Run a web server in c6a. Python’s built-in http.server serves files over HTTP. The client sends GET / and the server returns 200 and the contents of index.html. HTTP here runs over TCP, which establishes a reliable connection with a three-way handshake. The client sends SYN, the server replies with SYN and ACK, and the client acknowledges with ACK.
mkdir -p /srv/c6/www
echo 'hello from c6a' > /srv/c6/www/index.html
ip netns exec c6a python3 -m http.server 80 -d /srv/c6/www > /srv/c6/http.log 2>&1 &
ip netns pids c6a | xargs ps -o pid,cmd -p
curl -s http://10.6.0.2/
curl -s -m 3 http://192.168.10.31:8080/ || echo "no answer on 8080"
[1] 1552
PID CMD
1552 python3 -m http.server 80 -d /srv/c6/www
hello from c6a
no answer on 8080
The host can reach the container address, but other machines cannot reach 10.6.0.2. Publish the server on host port 8080. This uses destination NAT (DNAT) to redirect 192.168.10.31:8080 to 10.6.0.2:80. PREROUTING handles traffic from the network. A second rule in OUTPUT handles traffic originating on the host, which skips PREROUTING.
iptables -t nat -A PREROUTING -d 192.168.10.31 -p tcp --dport 8080 -j DNAT --to-destination 10.6.0.2:80
iptables -t nat -A OUTPUT -d 192.168.10.31 -p tcp --dport 8080 -j DNAT --to-destination 10.6.0.2:80
curl -s http://192.168.10.31:8080/
hello from c6a
Send a request from another machine and watch only packets with SYN on the bridge in the second terminal.
tcpdump -i lab6 -n -t -c 2 'tcp port 80 and tcp[tcpflags] & tcp-syn != 0' 2> /dev/null
On 192.168.10.50, run the following.
curl -s http://192.168.10.31:8080/
hello from c6a
Here are the tcpdump output and server log.
IP 192.168.10.50.56172 > 10.6.0.2.80: Flags [S], seq 324380109, win 65535, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
IP 10.6.0.2.80 > 192.168.10.50.56172: Flags [S.], seq 3192464766, ack 324380110, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 10], length 0
cat /srv/c6/http.log
10.6.0.1 - - [10/Nov/2026 20:28:23] "GET / HTTP/1.1" 200 -
192.168.10.31 - - [10/Nov/2026 20:28:23] "GET / HTTP/1.1" 200 -
192.168.10.50 - - [10/Nov/2026 20:28:36] "GET / HTTP/1.1" 200 -
The bridge sees the start of the handshake: [S] from the client and [S.] (SYN and ACK) from the server. The destination is already 10.6.0.2:80, while the source is still the client’s actual address, 192.168.10.50. The server can log that address. The first request came from the bridge address because the host sent curl http://10.6.0.2/ through lab6.
The portmap plugin in Part 1 did the same thing and added rules for requests to localhost:8080 and from inside the container subnet.
Connect the container from Part 3
Our container uses unshare to create an unnamed network namespace. ip link ... netns also accepts a PID and uses that process’s namespace. Get the Alpine root and write resolv.conf before launch, since the root will be read-only. Start container.sh in the background. The container’s PID 1 is a child of unshare.
img=docker.io/library/alpine:3.24
mkdir -p /srv/c6/rootfs
id=$(nerdctl create $img)
nerdctl export $id | tar -C /srv/c6/rootfs -xf -
nerdctl rm $id > /dev/null
cp /etc/netns/c6a/resolv.conf /srv/c6/rootfs/etc/resolv.conf
./container.sh /srv/c6/rootfs sleep 100000 &
sleep 2
pid=$(pgrep -P $(pgrep -n -x unshare))
nsenter -t $pid -n ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
Connect it as we did c6a, using nsenter -n in place of ip netns exec.
ip link add veth-c6c type veth peer name eth0 netns $pid
ip link set veth-c6c master lab6
ip link set veth-c6c up
nsenter -t $pid -n ip addr add 10.6.0.4/24 dev eth0
nsenter -t $pid -n ip link set eth0 up
nsenter -t $pid -n ip route add default via 10.6.0.1
nsenter -t $pid -a sh -c 'cat /etc/alpine-release; ip -4 a show eth0; wget -qO- http://10.6.0.2/; ping -c 1 1.1.1.1 | head -2; nslookup -type=a cloudflare.com'
3.24.4
2: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP qlen 1000
inet 10.6.0.4/24 scope global eth0
valid_lft forever preferred_lft forever
hello from c6a
PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: seq=0 ttl=54 time=9.434 ms
Server: 192.168.10.1
Address: 192.168.10.1:53
Non-authoritative answer:
Name: cloudflare.com
Address: 104.16.133.229
Name: cloudflare.com
Address: 104.16.132.229
The last command joins all the container’s namespaces, so it runs Alpine’s programs. The container can reach its bridge neighbor, get to the internet, and resolve names.
When the container process exits, its network namespace disappears. The kernel removes the veth interface inside it and therefore the entire pair.
bridge link | awk '{print $2, $6, $7}'
kill -KILL $pid
sleep 1
bridge link | awk '{print $2, $6, $7}'
veth-c6a@eth0: master lab6
veth-c6b@eth0: master lab6
veth-c6c@eth0: master lab6
veth-c6a@eth0: master lab6
veth-c6b@eth0: master lab6
veth-c6c disappeared from the bridge on its own. The iptables rules have a separate lifetime. We saw them accumulate with ctr in Part 1.
Clean up
ip netns pids c6a | xargs -r kill
ip netns del c6a
ip netns del c6b
ip link del lab6
iptables -t nat -D POSTROUTING -s 10.6.0.0/24 ! -o lab6 -j MASQUERADE
iptables -t nat -D PREROUTING -d 192.168.10.31 -p tcp --dport 8080 -j DNAT --to-destination 10.6.0.2:80
iptables -t nat -D OUTPUT -d 192.168.10.31 -p tcp --dport 8080 -j DNAT --to-destination 10.6.0.2:80
sysctl -w net.ipv4.ip_forward=0
rm -rf /etc/netns/c6a /srv/c6
nerdctl rmi $img > /dev/null
ip -br link
iptables -t nat -S
net.ipv4.ip_forward = 0
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 00:15:5d:02:a9:07 <BROADCAST,MULTICAST,UP,LOWER_UP>
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
ip netns del unmounts the namespace file, destroying the namespace and its veth pairs. The machine is back where it started.
Connect a namespace with a script
connect.sh connects a namespace to lab6 by ip netns name or process PID. On the first run it creates the bridge, enables forwarding, and installs the MASQUERADE rule.
./connect.sh c6a 2
./connect.sh $pid 4
./connect.sh --down
c6a: eth0 10.6.0.2/24, gateway 10.6.0.1
--down removes the bridge and NAT rule. It leaves forwarding enabled because other programs may rely on it.
References
- Named namespaces and
/etc/netns/<name>/. ip-netns(8) - veth pairs and bridges. veth(4) and bridge(8)
MASQUERADEandDNATtargets. iptables-extensions(8)net.ipv4.ip_forward. IP Sysctl- Protocol specifications. ARP, RFC 826, ICMP, RFC 792, and TCP, RFC 9293
- The plugins used in Part 1. CNI bridge and CNI portmap
Next, we decide which root capabilities to keep, filter system calls with seccomp, and use a user namespace to make root inside the container an ordinary user on the host.
Only registered users can leave a comment.
Sign in or register to leave a comment.