Linux › Section I · Your network and its configuration › Topic I.9

The transport layer: TCP, UDP, and services

IP gets data to the right machine; port numbers get it to the right program. This topic shows how to list the connections and listening services on a Linux host with ss, how to tell a client from a server at a glance, and why some ports need root.

From hosts to programs

So far this section has answered where data goes. The transport layer answers which program receives it. Many processes on one host use the network at once, a browser, a mail client and an SSH session, and the kernel keeps their traffic apart with port numbers, 16-bit values from 0 to 65,535 carried in every TCP and UDP header. An IP address is like a building's street address and a port like a flat number inside it. The Networking topics Multiplexing and demultiplexing, Connectionless transport: UDP and Connection-oriented transport: TCP explain the protocols themselves. On Linux both run inside the kernel.

TCP connections and UDP sockets

With TCP, a client process opens a connection from a port on its own machine to a port on a server, and both sides then read and write a reliable, ordered stream almost as if it were a file. A connection is identified by four values: local address and port, remote address and port. UDP has ports but no connections: a program sends single messages from its port to another, and lost messages are not resent. That suits short exchanges such as DNS queries and time requests to an NTP server, and live audio or video, where a late packet is useless anyway. Programs reach both through sockets.

Listing sockets with ss

ss, from iproute2, lists the kernel's sockets. ss -t shows TCP connections, ss -u UDP sockets, -l only listening ones, -a both, -n numbers instead of names, -p the owning process and -4 or -6 one protocol family. So ss -tn lists TCP connections numerically and ss -tuln lists every TCP and UDP port waiting for traffic. ss -s prints a summary of counts. Filters narrow the list: ss -t state established, or ss -tn 'dport = :443' for connections to HTTPS servers. Without root, -p shows only your own processes. The legacy tool netstat takes similar options, such as netstat -tln.

Reading a connection line

ss -tn on a laptop might print (illustrative): ESTAB 0 0 192.168.1.20:51334 203.0.113.80:443. The columns are state, Recv-Q, Send-Q, local address:port and peer address:port. ESTAB means the connection is established. Recv-Q and Send-Q count bytes waiting to be read by the program or acknowledged by the peer; numbers that stay large hint at a slow program or a congested path. Other states you will see are SYN-SENT while connecting, TIME-WAIT shortly after closing, and CLOSE-WAIT when the peer has closed but the local program has not, which, if it lingers, suggests a program bug.

Client or server?

A client almost always lets the kernel pick an unused local port, called an ephemeral port, and connects to a server's fixed, well-known port. On Linux the ephemeral range is set in /proc/sys/net/ipv4/ip_local_port_range, by default 32768 to 60999. So in a connection line, a high local port and a well-known remote port, such as 443 for HTTPS, mean this machine is the client. A well-known local port, such as 22, and a high remote port mean a remote client has connected to a server running here. IANA's official ranges, described in Multiplexing and demultiplexing, differ slightly from Linux's default.

Worked example: who started each connection?

ss -tn on a server prints two illustrative lines. A: ESTAB 0 0 192.0.2.150:22 198.51.100.7:50122. B: ESTAB 0 0 192.0.2.150:41876 203.0.113.25:5432. 1. In A the local port is 22, the SSH service, and the remote port 50122 is high, so 198.51.100.7 connected in to the local SSH server. 2. In B the local port 41876 is in Linux's ephemeral range, 32768 to 60999. 3. The remote port 5432 is PostgreSQL's registered port, so this host is a client of a database at 203.0.113.25. 4. getent services 5432 would confirm the name if the file lists it. Check: each line has exactly one well-known or registered port, on the server's side.

Listening sockets and exposure

ss -tln lists services waiting for connections, and the local address shows who can reach them. 0.0.0.0:22 accepts IPv4 connections on every interface, and [::]:22 every IPv6 interface; on Linux an IPv6 socket may also accept IPv4 unless the program asks for IPv6 only. 127.0.0.1:5432 accepts only connections from the machine itself, and 127.0.0.53%lo:53 is systemd-resolved on loopback. UDP sockets have no listening state, so ss -uln shows them as UNCONN. Add -p, as root, to see which process owns each port, or use lsof, covered in Diagnostic tools and remote procedure calls.

Worked example: auditing a server

An administrator runs ss -tuln on a new web server and sees (illustrative) tcp LISTEN 0.0.0.0:22, tcp LISTEN [::]:443, tcp LISTEN 0.0.0.0:3306 and udp UNCONN 127.0.0.53%lo:53. 1. Port 22 is SSH, open to all IPv4 interfaces: expected for administration. 2. Port 443 is HTTPS on all IPv6 interfaces, and probably IPv4 too: the web service. 3. Port 3306 is MySQL listening on every interface, so the database is reachable from outside; it should listen on 127.0.0.1 or be blocked by a firewall. 4. Port 53 on loopback is the local resolver and reaches no one else. Check: only one finding, port 3306, needs action.

Service names and privileged ports

/etc/services is a plain text table mapping well-known port numbers to names, with lines such as ssh 22/tcp and domain 53/udp; ss and netstat use it when they print names, and getent services ssh looks a name up. It is only a convenience: any program may use any port. On Linux, binding to ports below 1024 normally needs root or the CAP_NET_BIND_SERVICE capability, which is why web servers start as root or are granted it. The limit is the sysctl net.ipv4.ip_unprivileged_port_start, 1024 by default. Ordinary users may listen on and connect from higher ports.

Common mistakes to avoid

First, running ss without -l and concluding nothing is listening; by default ss omits listening sockets. Second, reading 127.0.0.1:5432 as exposed to the network; only local programs can reach it. Third, treating /etc/services as a guarantee; a program can run anything on any port. Fourth, expecting a UDP socket to show LISTEN; it shows UNCONN. Fifth, assuming -p will name every process without root. Sixth, deciding who is the client by the larger address instead of the ports: the side with the well-known port is the server.

Key takeaways

Ports, 0 to 65,535, deliver data to the right program; TCP connections are identified by local and remote address and port, while UDP sends single messages without connections. Both run in the Linux kernel. ss lists sockets: -t TCP, -u UDP, -l listening, -n numeric, -p processes, -s summary, with state and port filters; netstat is legacy. A high local port usually means a client; Linux's ephemeral range defaults to 32768 to 60999. 0.0.0.0 and [::] mean every interface, 127.0.0.1 local only. /etc/services names ports, and ports below 1024 need root or CAP_NET_BIND_SERVICE.

Flashcards

Flashcard

Port number

16-bit value, 0 to 65,535, that picks the program on a host

Carried in TCP and UDP headers.

Test your understanding

Question 1 of 5

ss -tn shows ESTAB 0 0 192.168.1.20:51334 203.0.113.80:443. What is happening?

Question 2 of 5

Which command lists every listening TCP and UDP socket with numeric ports?

Question 3 of 5

ss -tln shows LISTEN on 127.0.0.1:5432. Who can connect to this service?

Question 4 of 5

A non-root user starts a web server that tries to listen on port 80, and it fails with a permission error. Why?

Question 5 of 5

Why does ss -uln show a DNS resolver's UDP socket as UNCONN rather than LISTEN?

← Previous topicNext topic →