Sub topic 2
Well-known, registered and dynamic port ranges
This chapter assumes you know what an IP address is, understand the client-server model, can run commands in a terminal, and can read a short Python script. It also assumes you know that a port number identifies a service on a host, which the previous chapter covered. Here you will learn how the 65,536 possible port numbers are divided into three ranges, who controls each range, and what goes wrong when the ranges are misused.
The 16-bit port space
A port number is stored in a 16-bit field in the TCP and UDP headers. That gives 2^16 = 65,536 values, from 0 to 65535. TCP and UDP each keep their own port space. TCP port 53 and UDP port 53 are separate endpoints, and a program can listen on one without the other.
The Internet Assigned Numbers Authority (IANA) divides the space into three ranges.
| Range | Numbers | Common names | Who assigns |
|---|---|---|---|
| System | 0 to 1023 | Well-known ports | IANA, through strict review |
| User | 1024 to 49151 | Registered ports | IANA, through a lighter process |
| Dynamic | 49152 to 65535 | Private, ephemeral | Nobody. Free for local use |
The boundaries come from RFC 6335, which defines the IANA procedures for the port registry. The sections below look at each range.
Well-known ports: 0 to 1023
Well-known ports are reserved for core, widely deployed protocols. These are the ports you will meet most often in this tutorial:
- 22 for SSH
- 25 for SMTP
- 53 for DNS
- 80 for HTTP
- 443 for HTTPS
Assigning a number in this range requires IANA review and, for most protocols, a published specification. That is why the list is short and stable.
Privileged ports on Unix-like systems
Traditionally, Unix-like systems let only a privileged process bind to a port below 1024. "Bind" means asking the operating system to attach a socket to a specific port so that it can receive traffic there. The idea was that a client connecting to port 22 on a multi-user machine could trust that the listener had been started by an administrator, not by an ordinary user.
This is a weak guarantee today, for three reasons:
- It is an operating system convention, not a network rule. A remote client has no way to verify who started the listener.
- Linux lets you change the threshold with the
net.ipv4.ip_unprivileged_port_startsysctl setting. Some container platforms lower it to 0. - Windows has no such restriction.
You can see the restriction on Linux by trying to bind as an ordinary user:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("127.0.0.1", 80))
print("bound to port 80")
except PermissionError as e:
print("cannot bind to port 80:", e)
finally:
s.close()Run as a normal user on a default Linux system, this prints cannot bind to port 80: [Errno 13] Permission denied. Change 80 to 8080 and it succeeds. Run as root, or on a system where the threshold has been lowered, port 80 can also succeed. If another process is already listening on the port, you get OSError with "Address already in use" instead.
Registered ports: 1024 to 49151
Registered ports are assigned to specific applications on request. A vendor or project submits an application to IANA, and the assignment is recorded in the registry. Examples include:
- 3306 for MySQL
- 5432 for PostgreSQL
- 3389 for the Remote Desktop Protocol
- 8080, commonly used for alternative HTTP
Two points are easy to get wrong.
First, registration does not give the owner exclusive use. IANA records the intended use. It does not stop any other program on your machine from binding that port. If two programs both want 8080, the second to start fails with "Address already in use."
Second, the registry is incomplete. Many popular services use a registered-range port without ever having registered it, and some registered numbers are used by software that has nothing to do with the registered name. The registry tells you what the number was assigned for. It does not tell you what is listening there right now. Chapter 17 returns to this point.
Dynamic ports: 49152 to 65535
The dynamic range, also called private or ephemeral, is never assigned by IANA. Operating systems use it for the source port of an outgoing connection. When your browser connects to a web server on port 443, the OS picks a free port from this range as the local end. That temporary port is the ephemeral port, and the next chapters cover it in detail.
The IANA range is a recommendation. Real systems choose their own:
| System | Default ephemeral range |
|---|---|
| IANA recommendation | 49152 to 65535 |
| Linux | 32768 to 60999 |
| Windows (Vista and later) | 49152 to 65535 |
| macOS and FreeBSD | 49152 to 65535 |
On Linux you can read the active range directly:
cat /proc/sys/net/ipv4/ip_local_port_rangeA typical output is 32768 60999. The Linux default starts inside the registered range. That means a Linux client may use a source port such as 33060 or 40000, which might also be a number some server software wants to listen on.
Collisions between servers and ephemeral ports
Because the ephemeral range on Linux overlaps registered ports, a server can fail to start with "Address already in use" even though nothing is listening on its port. Some outgoing connection may have been given that port as its local end. The usual defenses are:
- Pick server ports below the ephemeral range, which on Linux means below 32768.
- Reserve specific ports with the
net.ipv4.ip_local_reserved_portssysctl setting so the kernel never hands them out to clients. - Bind servers early in the boot process, before many outgoing connections exist.
Port 0
Port 0 is reserved and is not used for ordinary traffic. In the sockets API it has a special meaning when you bind. It tells the OS "choose any free port for me." This code shows it:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
s.listen()
print("OS chose port", s.getsockname()[1])Because the OS picks from the ephemeral range, the printed number lies in your system's dynamic range. This technique is common in automated tests, where hard-coding a port would cause collisions between parallel test runs.
Checking which range a port belongs to
A small function makes the boundaries concrete. The version below follows the IANA classification:
def port_range(port: int) -> str:
if not 0 <= port <= 65535:
raise ValueError("ports are 16-bit: 0 to 65535")
if port <= 1023:
return "well-known"
if port <= 49151:
return "registered"
return "dynamic"
for p in (22, 443, 1023, 1024, 5432, 49151, 49152, 65535):
print(p, port_range(p))The output marks 22, 443 and 1023 as well-known, 1024, 5432 and 49151 as registered, and 49152 and 65535 as dynamic. Note that this classifies by IANA policy. On Linux, port 40000 is "registered" by this function but may still be handed out as an ephemeral port.
Common mistakes
- Treating a port number as proof of the protocol. A web server can run on 2222 and an SSH server on 443. The range tells you who may assign the number, not what runs there.
- Assuming TCP and UDP share a number space. A firewall rule for TCP 53 does not open UDP 53.
- Hard-coding ports in the ephemeral range for servers. This produces intermittent "Address already in use" failures.
- Assuming ephemeral ports always start at 49152. Linux does not, and firewall rules or documentation that assume it can be wrong.
- Relying on privileged-port rules for security. They are a local convention, not authentication.
Summary
Port numbers 0 to 65535 are split into well-known (0 to 1023), registered (1024 to 49151) and dynamic (49152 to 65535) ranges. IANA controls assignment in the first two. The third is left to operating systems, though Linux uses a different default range. Registration records an intended use and does not enforce it, and the privilege check on low ports is a local OS rule.
Exercise
A Linux server's database fails to start with "Address already in use" on port 40000. ss -tlnp shows no listener on 40000. Give one likely cause and one fix.
Show answer
The kernel can hand out port 40000 as an ephemeral source port, because the Linux default range is 32768 to 60999. An outgoing connection from this host may be using 40000 as its local port, so the bind fails. Fixes: move the database to a port below 32768, or add 40000 to net.ipv4.iplocalreserved_ports so the kernel never assigns it to clients. You can confirm the cause with ss -tn and look for a connection whose local port is 40000.
Exercise
Which IANA range does each port belong to: 25, 8080, 51000? Which of them can an unprivileged user normally bind to on a default Linux system?
Show answer
Port 25 is well-known, 8080 is registered, and 51000 is dynamic. An unprivileged user can normally bind to 8080 and 51000. Port 25 is below 1024, so binding needs root or a lowered ipunprivilegedport_start setting.