Skip to content
Linux networking · Chapter 7 of 20 Day 5: Connections: ss, nc and conntrack

Day 5: Connections: ss, nc and conntrack

Goal: See who listens on what and which connections are in which state. Build connections by hand with nc, check ports and push files. And watch the router remember every connection until its table is full · Duration: approx. 80 min · Prerequisite: Days 2 and 4

Briefing

  • ss (socket statistics) shows the sockets of a namespace: listening services, open connections, their TCP states and internals such as RTT and window. It replaces netstat and is considerably faster.
  • nc (netcat, here the OpenBSD variant shipped by Ubuntu) is a Swiss Army knife for TCP and UDP: listen, connect, pass data through, check ports.
  • conntrack is the kernel’s memory for connections that run through a router or a firewall. NAT needs it, because the reply has to go back to the right original address (Day 2, Drill 5). The table has a fixed size, and when it is full, the kernel drops new connections.
   ┌─────┐ 10.70.1.0/24  ┌──────────────┐ 10.70.2.0/24  ┌──────────────────────────────┐
   │ cli ├───────────────┤ eth1  r  eth2├───────────────┤ srv: 7000 nc, 8080 http,     │
   │ .10 │               │ NAT from Drill 6             │ 9000 http only 127.0.0.1,    │
   └─────┘               └──────────────┘               │ 5353/udp nc                  │
                                                        └──────────────────────────────┘

Drill 1: Lab and Services

The same pattern as on Day 4: one router, all cables straight out of r.

# on netz
sudo ip netns add r
sudo ip netns add cli
sudo ip netns add srv
sudo ip -n r link set lo up
sudo ip -n cli link set lo up
sudo ip -n srv link set lo up
sudo ip -n r link add eth1 type veth peer name eth0 netns cli
sudo ip -n r link add eth2 type veth peer name eth0 netns srv
sudo ip -n r addr add 10.70.1.1/24 dev eth1
sudo ip -n r addr add 10.70.2.1/24 dev eth2
sudo ip -n cli addr add 10.70.1.10/24 dev eth0
sudo ip -n srv addr add 10.70.2.20/24 dev eth0
sudo ip -n r link set eth1 up
sudo ip -n r link set eth2 up
sudo ip -n cli link set eth0 up
sudo ip -n srv link set eth0 up
sudo ip -n cli route add default via 10.70.1.1
sudo ip -n srv route add default via 10.70.2.1
sudo ip netns exec r sysctl -qw net.ipv4.ip_forward=1
sudo ip netns exec cli ping -c1 -W1 10.70.2.20 | grep -o '1 received'

Expected: 1 received

Four services start in srv, each as user ubuntu and in the background:

# on netz
sudo ip netns exec srv sudo -u ubuntu nc -lk 10.70.2.20 7000 >/tmp/nc7000.log 2>&1 &
sudo ip netns exec srv sudo -u ubuntu python3 -m http.server 8080 --bind 0.0.0.0 --directory /tmp >/dev/null 2>&1 &
sudo ip netns exec srv sudo -u ubuntu python3 -m http.server 9000 --bind 127.0.0.1 --directory /tmp >/dev/null 2>&1 &
sudo ip netns exec srv sudo -u ubuntu nc -lku 5353 >/tmp/nc5353.log 2>&1 &
sleep 1
sudo ip netns exec srv ss -tulpn

Expected:

Netid State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
udp   UNCONN 0      0            0.0.0.0:5353      0.0.0.0:*    users:(("nc",pid=6450,fd=3))
tcp   LISTEN 0      1         10.70.2.20:7000      0.0.0.0:*    users:(("nc",pid=6445,fd=3))
tcp   LISTEN 0      5            0.0.0.0:8080      0.0.0.0:*    users:(("python3",pid=6451,fd=3))
tcp   LISTEN 0      5          127.0.0.1:9000      0.0.0.0:*    users:(("python3",pid=6449,fd=3))

-tulpn is the shorthand worth memorizing: tcp, udp, listening, processes, numeric (no name resolution). The Local Address column decides who can reach the service:

Address Reachable
0.0.0.0:8080 via any address of the namespace
10.70.2.20:7000 only via this one address
127.0.0.1:9000 only from inside, from srv itself

For LISTEN, by the way, Send-Q does not mean send buffer, but the size of the queue for connections not yet accepted (the “backlog”): 1 for nc, 5 for Python’s http.server.

Save your progress? Sign up for free

Drill 2: Connections and Their States

One long-lived connection to port 7000 (it lives for 60 seconds) and three short HTTP requests:

# on netz
(sleep 60) | sudo ip netns exec cli nc 10.70.2.20 7000 &
sleep 1
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec srv ss -tanp
sudo ip netns exec cli ss -tan

Expected:

State     Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN    0      1         10.70.2.20:7000      0.0.0.0:*     users:(("nc",pid=6445,fd=3))
LISTEN    0      5            0.0.0.0:8080      0.0.0.0:*     users:(("python3",pid=6451,fd=3))
LISTEN    0      5          127.0.0.1:9000      0.0.0.0:*     users:(("python3",pid=6449,fd=3))
TIME-WAIT 0      0         10.70.2.20:8080   10.70.1.10:39226
ESTAB     0      0         10.70.2.20:7000   10.70.1.10:58054 users:(("nc",pid=6445,fd=4))
TIME-WAIT 0      0         10.70.2.20:8080   10.70.1.10:39244
TIME-WAIT 0      0         10.70.2.20:8080   10.70.1.10:39238
State Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
ESTAB 0      0         10.70.1.10:58054   10.70.2.20:7000

-a shows all sockets instead of only the connected ones. The three HTTP connections are in TIME-WAIT, and on the server. TIME-WAIT always ends up on the side that closes the connection first, and Python’s simple web server (HTTP/1.0) closes by itself after every response. A socket without a process: it no longer belongs to any program and only waits to see whether late packets still arrive (here 60 seconds).

State Meaning
LISTEN Waiting for new connections
ESTAB Connection is up, data flows
TIME-WAIT This side closed first and waits for stragglers. Harmless, even in large numbers
FIN-WAIT-2 This side has closed and waits for the other side to close as well
CLOSE-WAIT The other side has closed, the own program has not yet. If this persists, it is a bug in the program (Challenge)

Save your progress? Sign up for free

Drill 3: Filtering and Looking Inside

On a real server, thousands of sockets are in the list. ss filters by itself, by state and by port:

# on netz
sudo ip netns exec srv ss -tn state established
sudo ip netns exec cli ss -tn 'dport = :7000'
sudo ip netns exec srv ss -tn 'sport = :7000'

Expected:

Recv-Q Send-Q Local Address:Port Peer Address:Port Process
0      0         10.70.2.20:7000   10.70.1.10:58054
State Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
ESTAB 0      0         10.70.1.10:58054   10.70.2.20:7000
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0      0         10.70.2.20:7000   10.70.1.10:58054

dport is the port of the other side, sport your own. With state, the State column is dropped, since it is fixed. -i shows what TCP knows about the connection, -m the memory:

# on netz
sudo ip netns exec cli ss -tni 'dport = :7000'
sudo ip netns exec cli ss -tnm 'dport = :7000'

Expected:

State Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
ESTAB 0      0         10.70.1.10:58054   10.70.2.20:7000
     cubic wscale:7,7 rto:201 rtt:0.231/0.115 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:10 bytes_acked:1 segs_out:2 segs_in:1 send 501471861bps lastsnd:1418 lastrcv:1418 lastack:1418 pacing_rate 1002943720bps delivered:1 app_limited rcv_space:14480 rcv_ssthresh:64088 minrtt:0.231 snd_wnd:65160
State Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
ESTAB 0      0         10.70.1.10:58054   10.70.2.20:7000
     skmem:(r0,rb131072,t0,tb87040,f0,w0,o0,bl0,d0)
Field Meaning
cubic Congestion control algorithm
rtt:0.231/0.115 Measured round-trip time and variance in ms
rto:201 After this many ms without acknowledgment, data is retransmitted
cwnd:10 Congestion window in segments: this much may be in flight unacknowledged
mss:1448 Largest payload per segment
rb131072, tb87040 Size of receive and send buffers in bytes

If rtt rises sharply or many bytes sit in Send-Q, the problem lies with the network or the other side. You take that apart in more detail on Day 7 and Day 9.

By the way, ss -K can kill a connection selectively, for example ss -K dst 10.70.1.10. That is a tool for emergencies (it needs a kernel with CONFIG_INET_DIAG_DESTROY) and no replacement for cleanly restarting the service.

Save your progress? Sign up for free

Drill 4: nc by Hand

nc -z only establishes the connection and closes it again immediately, a mini port scanner:

# on netz
sudo ip netns exec cli nc -zv -w1 10.70.2.20 7000-7002 8080 9000

Expected:

Connection to 10.70.2.20 7000 port [tcp/bbs] succeeded!
nc: connect to 10.70.2.20 port 7001 (tcp) failed: Connection refused
nc: connect to 10.70.2.20 port 7002 (tcp) failed: Connection refused
Connection to 10.70.2.20 8080 port [tcp/http-alt] succeeded!
nc: connect to 10.70.2.20 port 9000 (tcp) failed: Connection refused

9000 is listening, but only on 127.0.0.1. From outside, this looks exactly like a port on which nothing runs at all. From inside:

# on netz
sudo ip netns exec srv nc -zv -w1 127.0.0.1 9000

Expected: Connection to 127.0.0.1 9000 port [tcp/*] succeeded!

UDP has no connection setup. nc -u simply sends a packet:

# on netz
echo "hallo udp" | sudo ip netns exec cli nc -u -w1 10.70.2.20 5353
cat /tmp/nc5353.log
sudo ip netns exec cli nc -zuv -w1 10.70.2.20 5353 5354

Expected:

hallo udp
Connection to 10.70.2.20 5353 port [udp/mdns] succeeded!

Tried it: For port 5354, where nothing listens, nc -zu prints nothing at all, neither success nor error. A UDP “scan” is unreliable: without a reply you do not know whether the packet arrived, and a “succeeded” only means that no error message came back.

Finally, transfer a file, without scp, without credentials. Receiver in srv, sender in cli, checksum on both sides:

# on netz
head -c 2000000 /dev/urandom > /tmp/daten.bin
sha256sum /tmp/daten.bin | cut -c1-16
sudo ip netns exec srv sudo -u ubuntu sh -c 'nc -l -N 7200 > /tmp/empfangen.bin' &
sleep 1
time sudo ip netns exec cli nc -N 10.70.2.20 7200 < /tmp/daten.bin
ls -l /tmp/empfangen.bin
sha256sum /tmp/empfangen.bin | cut -c1-16

Expected (your checksum will differ, but it is the same on both sides):

a546a81eb56cf7a3

real    0m0.044s
user    0m0.014s
sys 0m0.029s
-rw-rw-r-- 1 ubuntu ubuntu 2000000 Nov 23 20:38 /tmp/empfangen.bin
a546a81eb56cf7a3

-N closes the connection when the input ends. Without -N, the sender hangs and the receiver does not know that the file is complete. This is unencrypted, of course: handy in the lab, never over networks you do not control.

Save your progress? Sign up for free

Drill 5: conntrack in the Router

Now into the router. conntrack -L lists what it remembers:

# on netz
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec r conntrack -L
sudo ip netns exec r conntrack -C

Expected:

conntrack v1.4.8 (conntrack-tools): 0 flow entries have been shown.
0

Empty, even though a connection just went through. The kernel only tracks connections in a namespace once a rule there needs it, NAT for example. So the same masquerade rule as on Day 2, this time toward srv:

# on netz
sudo ip netns exec r nft add table ip labor
sudo ip netns exec r nft add chain ip labor post '{ type nat hook postrouting priority srcnat; }'
sudo ip netns exec r nft add rule ip labor post oifname eth2 masquerade
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec r conntrack -L
sudo ip netns exec srv ss -tn state time-wait | head -2

Expected:

conntrack v1.4.8 (conntrack-tools): 1 flow entries have been shown.
tcp      6 119 TIME_WAIT src=10.70.1.10 dst=10.70.2.20 sport=43002 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=43002 [ASSURED] mark=0 use=1
Recv-Q Send-Q Local Address:Port Peer Address:Port Process
0      0         10.70.2.20:8080     10.70.2.1:43002

One entry, two directions: the original (src=10.70.1.10 dst=10.70.2.20) and the expected reply (src=10.70.2.20 dst=10.70.2.1). Because of the NAT, srv now sees the router as the sender. 119 is the number of seconds the entry still lives; [ASSURED] means: there was traffic in both directions, and the entry will not be sacrificed early when space runs out.

conntrack -E shows events live, here for one request, from SYN to TIME_WAIT:

# on netz
sudo ip netns exec r timeout 3 conntrack -E -p tcp --dport 8080 &
sleep 0.5; sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/; wait

Expected:

    [NEW] tcp      6 120 SYN_SENT src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 [UNREPLIED] src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842
 [UPDATE] tcp      6 60 SYN_RECV src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842
 [UPDATE] tcp      6 432000 ESTABLISHED src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842 [ASSURED]
 [UPDATE] tcp      6 120 FIN_WAIT src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842 [ASSURED]
 [UPDATE] tcp      6 30 LAST_ACK src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842 [ASSURED]
 [UPDATE] tcp      6 120 TIME_WAIT src=10.70.1.10 dst=10.70.2.20 sport=52842 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=52842 [ASSURED]
conntrack v1.4.8 (conntrack-tools): 6 flow events have been shown.

Each state has its own timeout: 120 s during setup, 432000 s (five days!) in state ESTABLISHED, 120 s in TIME_WAIT. The values are in sysctl, UDP included:

# on netz
sudo ip netns exec r sysctl net.netfilter.nf_conntrack_tcp_timeout_established net.netfilter.nf_conntrack_tcp_timeout_time_wait net.netfilter.nf_conntrack_udp_timeout net.netfilter.nf_conntrack_max
echo x | sudo ip netns exec cli nc -u -w1 10.70.2.20 5353
sudo ip netns exec r conntrack -L -p udp

Expected:

net.netfilter.nf_conntrack_tcp_timeout_established = 432000
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_udp_timeout = 30
net.netfilter.nf_conntrack_max = 65536
conntrack v1.4.8 (conntrack-tools): 1 flow entries have been shown.
udp      17 28 src=10.70.1.10 dst=10.70.2.20 sport=52043 dport=5353 [UNREPLIED] src=10.70.2.20 dst=10.70.2.1 sport=5353 dport=52043 mark=0 use=1

UDP gets an entry too, even though it knows no connections: for 30 seconds a reply may come back. [UNREPLIED], because nc -lu does not reply.

Save your progress? Sign up for free

Drill 6: The Table Fills Up

You want to shrink the size of the table, nf_conntrack_max, only in the router. That does not work:

# on netz
sudo ip netns exec r sysctl -w net.netfilter.nf_conntrack_max=20

Expected: sysctl: setting key "net.netfilter.nf_conntrack_max": Operation not permitted

Tried it: nf_conntrack_max exists only once for the whole VM and may only be set in the root namespace. The entries, however, are counted per namespace. Nothing on the host needs conntrack, so it is safe here to set the value to 40 for a moment.

# on netz
sudo sysctl -w net.netfilter.nf_conntrack_max=40
ok=0; fail=0
for i in $(seq 1 60); do if sudo ip netns exec cli curl -s -m1 -o /dev/null http://10.70.2.20:8080/; then ok=$((ok+1)); else fail=$((fail+1)); fi; done
echo "ok=$ok fail=$fail"
sudo ip netns exec r conntrack -C
cat /proc/sys/net/netfilter/nf_conntrack_count
sudo ip netns exec r conntrack -S | head -1
sudo dmesg | grep -i conntrack | tail -3

Expected:

net.netfilter.nf_conntrack_max = 40
ok=38 fail=22
40
1
cpu=0       found=0 invalid=0 insert=0 insert_failed=0 drop=9 early_drop=0 error=0 search_restart=0 clash_resolve=0 chaintoolong=0
[ 5173.242066] nf_conntrack: nf_conntrack: table full, dropping packet
[ 5174.303594] nf_conntrack: nf_conntrack: table full, dropping packet
[ 5175.364231] nf_conntrack: nf_conntrack: table full, dropping packet

38 requests got through (entries from Drill 5 were still alive), then the table was full with 40 TIME_WAIT entries, and every further connection attempt was dropped. The host itself only counts its own entries (1), the router 40. drop=9 and the kernel log are the traces you find in a real incident: the service is healthy, only the path to it is blocked.

early_drop=0: when space runs out, the kernel only sacrifices entries without [ASSURED], and there were none. Shorter timeouts help, but not the way you think:

# on netz
sudo ip netns exec r sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=10
sleep 12
sudo ip netns exec r conntrack -L 2>/dev/null | awk '{print $4}' | sort | uniq -c
sudo ip netns exec cli curl -s -m1 -o /dev/null -w '%{http_code}\n' http://10.70.2.20:8080/

Expected:

net.netfilter.nf_conntrack_tcp_timeout_time_wait = 10
     40 TIME_WAIT
000

Tried it: A new timeout only applies to new entries. The 40 old ones keep running down their 120 seconds, and even after 12 seconds no request gets through (000). Only flushing frees space immediately:

# on netz
sudo ip netns exec r conntrack -F
sudo ip netns exec r conntrack -C
sudo ip netns exec cli curl -s -m1 -o /dev/null -w '%{http_code}\n' http://10.70.2.20:8080/
sudo ip netns exec r conntrack -L 2>/dev/null

Expected:

conntrack v1.4.8 (conntrack-tools): connection tracking table has been emptied.
0
200
tcp      6 9 TIME_WAIT src=10.70.1.10 dst=10.70.2.20 sport=57410 dport=8080 src=10.70.2.20 dst=10.70.2.1 sport=8080 dport=57410 [ASSURED] mark=0 use=1

The new entry has only 9 seconds left. Reset both values:

# on netz
sudo sysctl -w net.netfilter.nf_conntrack_max=65536
sudo ip netns exec r sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=120

Expected:

net.netfilter.nf_conntrack_max = 65536
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

Save your progress? Sign up for free

Exercise: Speed drills

  1. Which process listens on port 8080 in srv? One line, without header. Target time: 20 s
  2. From cli, check with one command whether 8080 and 9000 are reachable. Target time: 20 s
  3. How many entries for port 8080 does the router have after two requests? Target time: 30 s

Solution

# on netz
sudo ip netns exec srv ss -Htlnp 'sport = :8080'
sudo ip netns exec cli nc -zv -w1 10.70.2.20 8080 9000
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sudo ip netns exec r conntrack -L -p tcp --dport 8080 2>/dev/null | wc -l

Expected:

LISTEN 0      5      0.0.0.0:8080 0.0.0.0:* users:(("python3",pid=6451,fd=3))
Connection to 10.70.2.20 8080 port [tcp/http-alt] succeeded!
nc: connect to 10.70.2.20 port 9000 (tcp) failed: Connection refused
2

-H omits the header line, handy for wc and awk.

Save your progress? Sign up for free

Challenge

A colleague has started a small service on port 7100. It accepts connections but reads nothing and never closes, a classic programming error, reproduced here in one line of Python:

# on netz
sudo ip netns exec srv sudo -u ubuntu python3 -c 'import socket,itertools; s=socket.create_server(("0.0.0.0", 7100)); c=[s.accept()[0] for _ in itertools.count()]' &
sleep 1
for i in 1 2 3 4; do sudo ip netns exec cli bash -c 'echo hallo | nc -N -w1 10.70.2.20 7100'; done
echo "rc=$?"

Expected: rc=0

For the clients, everything looks fine. Use ss to find out what is left hanging on both sides, who is to blame and how to get rid of it.

Hint: Which side closed first, and which state remains on the other one?

Solution

# on netz
sudo ip netns exec srv ss -tanp 'sport = :7100'
sudo ip netns exec cli ss -tan 'dport = :7100'
sudo ls /proc/$(pgrep -u ubuntu -f create_server)/fd | wc -l

Expected:

State      Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN     0      128          0.0.0.0:7100      0.0.0.0:*     users:(("python3",pid=7596,fd=3))
CLOSE-WAIT 7      0         10.70.2.20:7100    10.70.2.1:54800 users:(("python3",pid=7596,fd=7))
CLOSE-WAIT 7      0         10.70.2.20:7100    10.70.2.1:54788 users:(("python3",pid=7596,fd=4))
CLOSE-WAIT 7      0         10.70.2.20:7100    10.70.2.1:54792 users:(("python3",pid=7596,fd=6))
CLOSE-WAIT 7      0         10.70.2.20:7100    10.70.2.1:54790 users:(("python3",pid=7596,fd=5))
State      Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
FIN-WAIT-2 0      0         10.70.1.10:54800   10.70.2.20:7100
FIN-WAIT-2 0      0         10.70.1.10:54788   10.70.2.20:7100
FIN-WAIT-2 0      0         10.70.1.10:54790   10.70.2.20:7100
FIN-WAIT-2 0      0         10.70.1.10:54792   10.70.2.20:7100
8

The clients sent and closed (FIN-WAIT-2: they are waiting for the server to close as well). The server accepted every connection but never read (Recv-Q 7: “hallo”, the line ending and the FIN) and never closed: CLOSE-WAIT, with process and file descriptor. Each connection costs it one descriptor (8 = 4 connections, the listening socket and the three standard streams). If this runs long enough, it dies of “Too many open files”. The peer address 10.70.2.1 is, by the way, the NAT from Drill 5.

CLOSE-WAIT cannot be fixed from outside; the program is always to blame. In the short term, only a restart helps:

# on netz
sudo kill $(pgrep -u ubuntu -f create_server)
sleep 1
sudo ip netns exec srv ss -tan 'sport = :7100'

Expected:

State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess

Save your progress? Sign up for free

Checkpoint

# on netz
sudo ip netns exec srv ss -Htln | wc -l

Expected: 3

# on netz
sudo ip netns exec cli nc -z -w1 10.70.2.20 9000; echo "rc=$?"

Expected: rc=1

# on netz
sudo ip netns exec r nft list table ip labor | grep -c masquerade

Expected: 1

# on netz
sysctl -n net.netfilter.nf_conntrack_max

Expected: 65536

# on netz
sudo ip netns exec r sysctl -n net.netfilter.nf_conntrack_tcp_timeout_time_wait

Expected: 120

Clean-up

First the services, then the namespaces. The NAT rule and conntrack entries go with them:

# on netz
pkill -u ubuntu -f 'nc -lk'
pkill -u ubuntu -f 'http.server'
pkill -u ubuntu -f 'nc -lku'
sudo ip netns del r
sudo ip netns del cli
sudo ip netns del srv
rm -f /tmp/nc7000.log /tmp/nc5353.log /tmp/daten.bin /tmp/empfangen.bin
ip netns list | wc -l
pgrep -u ubuntu -af 'nc |http.server|create_server' || echo "keine Labor-Prozesse"
sysctl net.netfilter.nf_conntrack_max net.ipv4.ip_forward

Expected:

0
keine Labor-Prozesse
net.netfilter.nf_conntrack_max = 65536
net.ipv4.ip_forward = 0

Practice note

  • ss -tulpn first. “The service is unreachable” means in half of all cases: it listens on 127.0.0.1 or on the wrong address. From outside, that looks like a dead port.
  • TIME-WAIT is normal, CLOSE-WAIT is not. Many TIME-WAIT sockets only indicate a lot of traffic. Growing CLOSE-WAIT counts are always a bug in the program that does not close its connections, and sooner or later lead to “Too many open files”.
  • A full conntrack table is an outage without a sick service. On NAT gateways, Kubernetes nodes and firewalls, conntrack -C versus nf_conntrack_max belongs in monitoring, and table full, dropping packet in the kernel log belongs in alerting. Lowering timeouts only takes effect for new entries.

Contents

More