Sub topic 5
Where Port Assignments Come From
This chapter assumes you know what a port number identifies and the three port ranges (well-known, registered and dynamic). It explains who decides which service gets which number, and how your system learns those numbers.
IANA and the service name registry
The Internet Assigned Numbers Authority (IANA) maintains the Service Name and Transport Protocol Port Number Registry. Each entry links a service name, a port number and a transport protocol (TCP, UDP, SCTP or DCCP) to a description and a contact for the assignment. The rules for requesting entries are in RFC 6335.
The registry only covers the first two ranges:
| Range | Numbers | How an entry is obtained |
|---|---|---|
| Well-known (system) | 0 to 1023 | Stricter review by IANA (IETF Review or IESG Approval) |
| Registered (user) | 1024 to 49151 | Application to IANA, reviewed by experts |
| Dynamic (ephemeral) | 49152 to 65535 | Never assigned |
An entry is made per transport. A service that uses both TCP and UDP usually gets the same number for each, as DNS does with port 53. The registry lists the pair separately, and a protocol that is only assigned for TCP does not own the matching UDP number in practice.
The local services database
Your operating system keeps a small copy of this registry so programs can translate between names and numbers. On Linux and macOS it is /etc/services. On Windows it is C:\Windows\System32\drivers\etc\services. Each line has a name, a port and protocol, and optional aliases:
ssh 22/tcp
smtp 25/tcp mail
domain 53/udp
domain 53/tcp
https 443/tcpTools such as ss -tulnp use this file to show ssh instead of 22 unless you pass -n. The file is a convenience and is often a trimmed copy. It can be out of date or edited locally, so it is not an authority.
Looking up names and numbers in Python
Python's socket module reads that same database.
import socket
for name in ("http", "https", "ssh", "smtp", "domain"):
print(name, socket.getservbyname(name, "tcp"))
print(socket.getservbyport(443, "tcp"))getservbyname takes a service name and a protocol and returns the port. getservbyport does the reverse. Both raise OSError when the database has no matching entry, so wrap them in try and except if the input is not guaranteed to be a known service.
Registered is not the same as enforced
IANA assignment is a coordination mechanism. Nothing in the network stops a program from listening on a number assigned to something else, and nothing guarantees that a registered number is in use. Some software uses unregistered numbers, and several registered numbers are claimed by more than one product in practice. The last chapter of this tutorial covers why a port number is not proof of which protocol runs behind it.
Exercise
A script calls socket.getservbyname("imaps", "tcp") on a minimal container and gets an OSError, although IANA lists imaps. What is the most likely cause, and how can you check it?
Show answer
The container's local services file probably has no imaps entry, because it is a trimmed copy and not the live IANA registry. Check with grep imaps /etc/services. If the line is missing, use the known port (993) directly or add the entry to the file.