Sub topic 5
Where Port Assignments Come From
This chapter assumes you know what a port number identifies, the three port ranges (well-known, registered and dynamic), and the difference between TCP and UDP. It explains who decides that HTTP uses 80 and SSH uses 22, how you can look those assignments up, and where the assignments stop being binding.
Ports are conventions, not laws
No part of TCP or UDP forces a service onto a particular port. A server can listen on any free port the operating system allows. Port 22 means "SSH" only because clients, servers, firewalls and documentation have all agreed on it.
The agreement is maintained by a central registry so that two protocols do not claim the same number. Without one, a client could not know where to find a mail server, and two products could collide on the same port.
IANA and the 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 to the document that defines the service, usually an RFC.
A registry entry holds these fields:
| Field | Example | Meaning |
|---|---|---|
| Service name | `ssh` | Short, lowercase name for the service |
| Port number | `22` | The assigned port |
| Transport protocol | `tcp` | Each protocol (TCP, UDP, SCTP, DCCP) is listed separately |
| Description | The Secure Shell (SSH) Protocol | Human-readable summary |
| Reference | RFC 4251 | Document that defines the service |
Two details matter in practice:
- Assignments are per transport protocol. A number is usually reserved for both TCP and UDP, even when only one is used. DNS has entries for
53/tcpand53/udp. Some services use only one, and the registry does not make the unused one available to a different service. - Ranges are handled differently. The registry follows the ranges you already know, with different rules for each.
How the ranges are assigned
The process in RFC 6335 sets a different level of review for each range.
| Range | Numbers | Assignment process |
|---|---|---|
| System (well-known) | 0 to 1023 | Strict review. Needs "IETF Review" or "IESG Approval", meaning a standards-track document and approval by the Internet Engineering Steering Group |
| User (registered) | 1024 to 49151 | "Expert Review". An application is submitted and a designated expert checks it for completeness and conflicts |
| Dynamic (private, ephemeral) | 49152 to 65535 | Never assigned. Free for temporary use |
The result is that well-known ports are rare and carefully guarded, which is why the core protocols you will memorize all sit there. Registered ports are easier to obtain, so vendors use them for products such as databases and management tools. For example, MySQL uses 3306 and PostgreSQL uses 5432. Both are registered numbers.
Registration is not the same as ownership. IANA lists the number, but nothing stops other software from binding it if it is free on that host.
Registered versus actually used
Many popular ports are widely used without ever being registered, or are registered to a different service. The registry records what was requested and approved. It does not track what runs on the internet.
Two consequences follow:
- A port can be in heavy use and absent from the registry, so a lookup returns nothing for it.
- A port can be registered to one service and used by another in practice. Older registrations in particular are sometimes quirks of history rather than a guide to current use.
Treat the registry as the authoritative answer to "what was this number assigned for?", and not as a description of "what is on this port right now?". The last chapter of this tutorial returns to that second question.
The local services database
Operating systems carry a small, local copy of common assignments so that programs and tools can translate between names and numbers without contacting IANA. On Linux and macOS it is the file /etc/services. On Windows it is C:\Windows\System32\drivers\etc\services.
Each line has the format name port/protocol [aliases]:
ssh 22/tcp
smtp 25/tcp mail
domain 53/tcp
domain 53/udp
http 80/tcp www
https 443/tcpNotice that the DNS service is named domain, not dns, because that is the registered service name. Tools such as ss, netstat and nc use this file when they print service names instead of numbers.
The file is a convenience, and it has limits:
- It is a subset of the full registry. It is also installed once with the system, so it can be out of date.
- Editing it changes only how local tools display names. It does not move any service to another port.
- Some distributions trim it, and some container images omit it, so a name lookup can fail even for a well-known service.
Looking up assignments programmatically
Python's socket module reads the same database through getservbyname and getservbyport. The first translates a service name into a port number, and the second goes the other way.
import socket
for name in ("http", "https", "ssh", "smtp", "domain"):
print(name, socket.getservbyname(name, "tcp"))
print(socket.getservbyport(443, "tcp"))On a typical Linux system the output is:
http 80
https 443
ssh 22
smtp 25
domain 53
httpsIf a name or number is not in the local file, Python raises OSError with the message "service/proto not found". Code that depends on a lookup should handle that case. A script that must work on minimal systems should keep its own port constants.
import socket
def port_for(name, proto="tcp", default=None):
try:
return socket.getservbyname(name, proto)
except OSError:
return default
print(port_for("https"))
print(port_for("no-such-service", default=-1))The second call prints -1, because the made-up name is not in the database.
Also check the protocol argument. socket.getservbyname("domain", "udp") and socket.getservbyname("domain", "tcp") both return 53, but a service that exists only over TCP raises an error when you ask for UDP, depending on what the local file lists.
Looking up assignments on the command line
You can search the local database with ordinary text tools:
# Which service is assigned to port 443?
grep -w 443 /etc/services
# Which ports does the "domain" service use?
grep -w '^domain' /etc/services
# Resolve names through the system resolver, including other databases
getent services ssh
getent services 53/udpgetent is more reliable than grep on systems where the services database is also provided by sources other than the plain file. For the authoritative answer, consult the IANA registry online and search by number or by name.
A worked example: tracing an assignment
Suppose you see traffic to TCP port 5432 and want to know what it is.
- Run
grep -w 5432 /etc/services. The local file reportspostgresql 5432/tcp. - Check the IANA registry. It lists 5432 as
postgresql, a registered-range port assigned through expert review. - Conclude that the number was assigned to PostgreSQL. This does not prove that PostgreSQL is listening, only that this is the expected service.
- To confirm, run
ss -tlnpon the host and read which process owns the socket.
Steps 1 to 3 tell you what the number is meant for. Step 4 tells you what is there.
The diagram shows how a name and a number are resolved, and where each source fits.
Common mistakes
- Treating a missing entry as proof of nothing. A port absent from
/etc/servicesmay still be an important service. The file is a subset. - Assuming the registry describes your network. It records assignments, not deployments.
- Confusing the name with the protocol. The name
domainis DNS. The namewwwis an alias for HTTP. - Forgetting the transport.
53/tcpand53/udpare separate entries, and a firewall rule or a lookup must name the right one. - Hard-coding trust in numbers. An administrator can run SSH on 2222 or a web server on 22. The port tells you what is expected, not what is certain.
Summary
IANA assigns port numbers through a documented process. It applies strict review to the well-known range and lighter review to the registered range, and it never assigns the dynamic range. Your operating system keeps a local, partial copy of those assignments in the services database, which tools and programs such as Python's socket module use to translate between names and numbers. The registry tells you what a number was intended for. To learn what is actually running, you must inspect the host.
Exercise
Port 3306/tcp appears in your firewall logs. (a) Which range does it belong to, and how is that range assigned? (b) Give two ways to find the intended service on a Linux host. (c) Why can you not conclude from this lookup that MySQL is running on that connection?
Show answer
(a) 3306 is in the registered (user) range, 1024 to 49151. These numbers are assigned through Expert Review, where a designated expert checks the request. (b) Run grep -w 3306 /etc/services or getent services 3306/tcp, or call socket.getservbyport(3306, "tcp") in Python. You can also search the IANA registry online. (c) A lookup shows only what the number was assigned for. Any program can listen on any free port, so another service could be using 3306, or MySQL could be running elsewhere. Run ss -tlnp on the host to see which process owns the socket.
Exercise
Write a Python function that returns the port for a service name and protocol, and returns a given default instead of raising an error when the name is not in the local services database. Why is the default useful?
Show answer
Use a try/except around the lookup: import socket def port_for(name, proto="tcp", default=None): try: return socket.getservbyname(name, proto) except OSError: return default The default is useful because /etc/services is a partial, system-specific copy. Minimal systems and container images may lack entries or the whole file, so a script that relies on the lookup alone could fail where the service is perfectly valid. A default (or built-in constants) keeps the script working.