Practical 0 · Your lab: two namespaces and a cable
Goal: a lab container you built yourself with the network tools in it, two “machines” (
redandblue, network namespaces) joined by a veth cable that you can ping across, a cable you unplug on purpose and diagnose, and a script that rebuilds it all.Time: ~40 minutes. Where: files in
~/linux-networking-zero/on your computer; network commands inside the lab container (./on). Needs Docker Engine on your server and a few hundred MB of disk for the image and the tools. Read lesson 1 and lesson 2 first.
Task 1 · Get the lab script
Make the folder everything in this course goes into, download this course’s lab script into it, and run it:
mkdir -p ~/linux-networking-zero
cd ~/linux-networking-zero
curl -fo lab http://localhost:8800/linux-networking-zero/tools/lab
chmod +x lab
./lab setup p0
✓ Docker 29.1.3 is running (linux/aarch64)
✓ the name netlab is free for your lab container
Building the lab is this practical’s topic, so setup only checks that Docker runs and that the name netlab is
free. Your Docker version and architecture may differ. Every command the lab script runs goes to lab.log.
Task 2 · Start your lab machine
Start a container yourself with these requirements:
- name and hostname
netlab, from the imageubuntu:26.04(newest Ubuntu images) - privileged, so you can change network settings inside it
- the label
fromzero.course=linux-networking-zero(./lab resetonly touches a container that has it) - your folder
~/linux-networking-zeromounted at the same path inside, and as its working directory - it runs
sleep infinity(it only needs to stay up), with--initsodocker stopis instant
Then install the tools into it: iproute2 iputils-ping tcpdump iptables nftables dnsutils netcat-openbsd curl procps.
Hint
docker run -d with --name, --hostname, --privileged, --init, --label, -v "$HOME/...:$HOME/..." and -w,
then the image and the command. For the tools: docker exec netlab bash -c 'apt-get update && apt-get install -y ...',
with DEBIAN_FRONTEND=noninteractive so no installer stops to ask a question.
Solution
docker run -d --name netlab --hostname netlab --privileged --init \
--label fromzero.course=linux-networking-zero \
-v "$HOME/linux-networking-zero:$HOME/linux-networking-zero" -w "$HOME/linux-networking-zero" \
ubuntu:26.04 sleep infinity
docker exec netlab bash -c 'apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
iproute2 iputils-ping tcpdump iptables nftables dnsutils netcat-openbsd curl procps'
| Flag | Why |
|---|---|
--privileged | lets you create namespaces and change their settings. It also lets the container change your computer’s kernel, so use it only for this lab |
--init | a tiny first process that passes on docker stop, so docker stop takes 0.156 s instead of 10.3 s |
-v same path, -w | scripts you write in ~/linux-networking-zero run inside under the same name |
Now write ~/linux-networking-zero/on, a 5-line script: ./on alone opens a shell in netlab, and
./on <command> runs one command there. Make it executable.
Solution
#!/bin/sh
# on [command ...]: a shell in your lab container netlab, or one command there
t=; [ -t 0 ] && t=-t
if [ $# -eq 0 ]; then exec docker exec -it netlab bash; fi
exec docker exec -i $t netlab bash -c "$*"
-i passes your input in, -t gives a terminal (only when you have one, so the lab script can use ./on too).
chmod +x on
./on hostname
It prints netlab. From here on, commands in a bash block run inside the lab: open a shell there with ./on
(your prompt becomes root@netlab:...#) and type them; exit leaves it.
Task 3 · Look around
Predict first: how many interfaces does a fresh container have, and what is its default route?
ip -br link
ip -br addr
ip route
What you see
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0@if837 UP 66:f3:e2:83:c1:cf <BROADCAST,MULTICAST,UP,LOWER_UP>
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if837 UP 172.17.0.2/16
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
Two interfaces: lo and eth0, Docker’s cable into the container. Your number after @if, your MAC and your
172.17.x.x address may differ. ip route shows default via 172.17.0.1 dev eth0: everything leaves through Docker’s gateway. This is
lesson 2’s container stack: you’re looking at its interfaces, addresses and routes tables.
Task 4 · Two machines and a cable
Build, inside the lab:
- two network namespaces,
redandblue - a veth pair with ends
veth-red(inred) andveth-blue(inblue) 10.0.0.1/24onveth-red,10.0.0.2/24onveth-blue
Don’t bring anything up yet.
Hint
ip netns add, ip link add ... type veth peer name ..., ip link set <dev> netns <ns>, then
ip -n <ns> addr add <cidr> dev <dev>. Lesson 2 has each one.
Predict first: with every interface still down, what does
ip netns exec red ping -c1 -W2 10.0.0.2print? And with onlyveth-redup?
ip netns exec red ping -c1 -W2 10.0.0.2
ip -n red link set veth-red up
ip netns exec red ping -c1 -W2 10.0.0.2
What you see
All down: ping: connect: Network is unreachable, at once. The address is on a link that’s down, so red has no
route to 10.0.0.0/24 (ip -n red route prints nothing).
Only red’s end up: the route appears, marked linkdown, and the ping waits 2 s and reports
1 packets transmitted, 0 received, 100% packet loss. Red sent into a cable whose other end is off.
Now bring up everything else (lo in both, veth-blue) and ping again.
Solution
ip netns add red
ip netns add blue
ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red
ip link set veth-blue netns blue
ip -n red addr add 10.0.0.1/24 dev veth-red
ip -n blue addr add 10.0.0.2/24 dev veth-blue
ip -n red link set lo up
ip -n red link set veth-red up
ip -n blue link set lo up
ip -n blue link set veth-blue up
ip netns exec red ping -c3 10.0.0.2
ip -n red neigh
Replies, and red’s neighbour table holds blue’s MAC: compare it with ip -n blue link show veth-blue.
The first ping may say From 10.0.0.1 icmp_seq=1 Destination Host Unreachable; the next ones get replies. That’s
left over from the predict step: its ping asked for blue’s MAC (ARP) while nobody could answer, and red keeps asking
for about 3 s (INCOMPLETE in ip -n red neigh). A ping sent near the end waits on that attempt, which gives up
and drops it. Wait 3 s after a failed ping and the first ping gets a reply.
Task 5 · Break it: unplug the cable
Switch off blue’s end, as if someone pulled the cable out of blue:
ip -n blue link set veth-blue down
ip netns exec red ping -c2 -W1 10.0.0.2
The ping only says 100% packet loss. Find out why from red’s side only, reading lesson 2’s tables starting at the cable:
- Interfaces:
ip -n red -br link. Look forNO-CARRIERin red’s flags: your end is up, nothing is on the other end. The state column may sayDOWNhere, orLOWERLAYERDOWNas in lesson 2; the flag is what counts. - Routes:
ip -n red route. The10.0.0.0/24route is still there, markedlinkdown. - Neighbours:
ip -n red neigh. Red’s entry for 10.0.0.2 no longer shows a MAC.
The first “no” (step 1) is the answer: the cable. Fix it with ip -n blue link set veth-blue up and ping again. The
very first ping after the fix may still report Destination Host Unreachable, as in task 4: within about 3 s of
the failed ping, red’s unanswered ARP attempt (INCOMPLETE) is still running, gives up, and drops the new ping
with it. The next ones get replies.
Task 6 · Put it in a script
Predict first: you restart the container (
docker restart netlab). Areredandbluestill there?
docker restart netlab
./on ip netns exec red ip -br link
Run these on your computer, not inside the lab (leave the lab shell with exit first), and the ones below too. You get
setting the network namespace "red" failed: Invalid argument. The namespaces died with the container’s processes,
and only their names in /run/netns are left (lesson 2, Go deeper).
Write ~/linux-networking-zero/p0.sh yourself:
- builds task 4’s lab from nothing, with everything up
- safe to run again: it first deletes
redandblueif they exist, alive or dead, without printing errors - stops at the first command that fails
Hint
set -e stops at the first failure. ip netns del red 2>/dev/null || true deletes a namespace without failing when
there isn’t one.
Solution
#!/bin/bash
# p0.sh: two namespaces, red and blue, joined by a veth cable (Practical 0)
set -e
ip netns del red 2>/dev/null || true
ip netns del blue 2>/dev/null || true
ip netns add red
ip netns add blue
ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red
ip link set veth-blue netns blue
ip -n red addr add 10.0.0.1/24 dev veth-red
ip -n blue addr add 10.0.0.2/24 dev veth-blue
ip -n red link set lo up
ip -n red link set veth-red up
ip -n blue link set lo up
ip -n blue link set veth-blue up
./on bash p0.sh
./on bash p0.sh
./on ip netns exec red ping -c1 10.0.0.2
Running it twice proves it’s safe to run again; the ping proves the lab is back. After any restart of netlab,
./on bash p0.sh is all you need.
Check your work
./lab check p0
All 26 checks should pass.
Go deeper: what "privileged" changes here
In an ordinary container, the very first command of task 4 fails:
mount --make-shared /run/netns failed: Operation not permitted
Adding just the capabilities NET_ADMIN and SYS_ADMIN still isn’t enough (the same line, ending
Permission denied): Docker restricts containers in more ways than capabilities. Privileged lifts all of
them. Network settings belong to the namespace they’re set in, so changing one in red (lessons 4 and 7 set
net.ipv4.ip_forward) changes nothing on your computer.
The kernel, though, is your computer’s one kernel. When iptables or nft in the lab needs a part of the kernel
that isn’t loaded yet (a module: nf_tables, nf_nat, nf_conntrack…), it is loaded there, and it stays loaded
after the rule and the namespace are gone (grep nf_ /proc/modules, on your computer or in the lab, shows the same
list).
Privileged also gives the container every other power over the kernel, which is why it is for this lab and nothing else. To start over at any
time: docker rm -f netlab, then task 2.
Next: lesson 3 · IP addresses and subnets, then Practical 1 · A router between two networks.