Skip to content
Sign inGet all access
Chapter 2·8 minFree

Addresses and Ports

Which machine, which program, and the four numbers that name a connection.

An address picks a machine. A port picks a program on it.

An IP address

It belongs to an interface, not the machine. This container has two:

ip -4 -o addr show | sed -E 's/ +/ /g' | cut -d' ' -f2,4
Outputlo 127.0.0.1/8eth0 172.17.0.4/16

The /8 and /16 say how much of the address names the network. /16 makes the first two octets the network, so 172.17.*.* are all neighbours.

lo is always 127.0.0.1. The second address is whatever Docker assigned, so yours differs.

Loopback

127.0.0.1 never leaves the machine. Capture on eth0 and nothing appears:

cap "-i eth0 -n -c 1 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"

The same command on lo caught three packets in chapter 1 — the kernel routes loopback without a network card.

Private ranges

Three blocks are reserved for networks that are not the internet:

Range Size Where you see it
10.0.0.0/8 16 million corporate networks, Kubernetes
172.16.0.0/12 1 million Docker’s default
192.168.0.0/16 65 thousand home routers

The container’s address is in the second. A router rewrites these on the way out, which is why your laptop’s address is not what a server sees.

A port

A 16-bit number in the TCP header — 1 to 65535, nothing else. It follows the address in every capture:

cap "-i lo -n -c 1 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0

What is listening

ss -tln shows the sockets waiting for a connection:

ss -tln | grep 9000
OutputLISTEN 0 1 0.0.0.0:9000 0.0.0.0:*

0.0.0.0 means every address on this machine, so both of its addresses reach it.

Bound to one address

A second listener, bound only to loopback:

terminal
nc -l -s 127.0.0.1 -p 9200 -k &
Output$ nc -l -s 127.0.0.1 -p 9200 -k &$ ss -tln | grep 9200LISTEN 0 1 127.0.0.1:9200 0.0.0.0:*

From loopback it answers:

nc -z -w 1 127.0.0.1 9200 2>/dev/null && echo open || echo closed
Outputopen

From the machine’s other address it does not:

nc -z -w 1 172.17.0.4 9200 2>/dev/null && echo open || echo closed
Outputclosed

The 0.0.0.0 listener answers on both:

nc -z -w 1 172.17.0.4 9000 2>/dev/null && echo open || echo closed
Outputopen

Ephemeral ports

The client never picks its port — the kernel takes one from a range:

cat /proc/sys/net/ipv4/ip_local_port_range
Output32768 60999

That is the CLIENT the captures hide — a number from this range, different each time.

28,000 ports is the ceiling on simultaneous outbound connections to one destination — a load generator hits it, an application rarely does.

A connection is four numbers

Source address, source port, destination address, destination port. Two connections differ only in the second:

terminal
(nc -w 3 127.0.0.1 9000 </dev/null >/dev/null 2>&1 &)
(nc -w 3 127.0.0.1 9000 </dev/null >/dev/null 2>&1 &)
sleep 1
ss -tn 'dport = :9000' | tail -2
OutputESTAB 0 0 127.0.0.1:CLIENT 127.0.0.1:9000ESTAB 0 0 127.0.0.1:CLIENT 127.0.0.1:9000

Same destination, same server port, two connections. Only the client port differs.