Sub topic 2
Well-known, registered and dynamic port ranges
This chapter assumes you know that a port number identifies a service on a host, that IP addresses identify hosts, and that you can run short Python scripts and terminal commands.
Why ports are split into ranges
A port number is a 16-bit value, so it runs from 0 to 65535. The Internet Assigned Numbers Authority (IANA) divides that space into three ranges. The split tells you who normally uses a number and how much you can trust it to mean a particular service.
| Range | Name | Typical use |
|---|---|---|
| 0–1023 | Well-known (system) ports | Standard services such as HTTP, SSH and DNS |
| 1024–49151 | Registered (user) ports | Services registered by vendors and projects, such as PostgreSQL on 5432 |
| 49152–65535 | Dynamic (private, ephemeral) ports | Temporary client ports, never assigned to a service |
Well-known ports
Ports 0–1023 hold the core protocols: 22 for SSH, 25 for SMTP, 53 for DNS, 80 for HTTP and 443 for HTTPS. On Unix-like systems, only a privileged process (root, or one with the CAP_NET_BIND_SERVICE capability on Linux) may listen on these ports by default. The rule is a historical safeguard. A client could assume that a service on port 22 had been started by an administrator and not by an ordinary user.
Registered ports
Ports 1024–49151 can be registered with IANA by applications. Examples are 3306 for MySQL, 5432 for PostgreSQL and 3389 for RDP. Any user can listen on them. Registration is a convention, so another program may use the same number on your host. If two programs try to bind the same port and protocol on the same address, the second one fails with an "address already in use" error.
Dynamic ports
Ports 49152–65535 are never assigned. Operating systems hand them to clients as source ports for outgoing connections. Many systems use a different window. Linux defaults to 32768–60999, which you can read from the kernel:
from pathlib import Path
low, high = Path("/proc/sys/net/ipv4/ip_local_port_range").read_text().split()
print(f"Linux ephemeral range: {low}-{high}")
for port in (22, 443, 5432, 50000):
if port <= 1023:
kind = "well-known"
elif port <= 49151:
kind = "registered"
else:
kind = "dynamic"
print(port, kind)This script runs on Linux. On other systems, skip the first two lines.
Common errors
- Treating a port number as proof of the protocol. Any program can listen on any free port, so a port number only suggests a service.
- Starting a server on a port below 1024 as an ordinary user. This fails with "permission denied" on most Unix-like systems.
- Picking a fixed port for your own service inside the operating system's ephemeral range. A client connection may already be using it.
Exercise
A colleague says that a process listening on port 8080 must be a standard HTTP server run by an administrator. Which range is 8080 in, and why is the claim unreliable?
Show answer
Port 8080 is in the registered range (1024–49151). Any user can listen there without special privileges, and the number is only a convention. The process could be any program, so the port does not prove the protocol or who started it.