Sub topic 17
Non-standard ports and why port numbers are not proof of protocol
This chapter assumes you know what a port is, the three port ranges, how TCP and UDP differ, and how to run ss and nc. It also assumes you can read short Python scripts. The chapter covers why services run on ports other than their defaults, what that changes in practice, and why a port number is a hint about a protocol and not proof of one.
A port number is a convention
IANA assigns default ports so that clients know where to look. HTTPS is on 443 and SSH is on 22. Nothing in TCP or UDP enforces this. The transport layer delivers bytes to whichever process opened the socket. It never checks what those bytes mean.
So a port number is a convention between two parties. If both agree, any protocol can run on any port. The only hard rules are these:
- Only one process can listen on a given address, port and protocol at a time, unless the operating system is told to share it.
- On most Unix-like systems, binding to a port below 1024 requires elevated privileges or a specific capability.
- Firewalls and middleboxes may apply their own rules to a port, whatever runs on it.
Why people use non-standard ports
Reasons are practical, and few of them are about security.
| Reason | Example |
|---|---|
| Two services of the same kind on one host | A production web server on 80 and a test instance on 8080 |
| No privilege to bind low ports | A developer runs an app on 8000 or 3000 without root |
| Reverse proxy or container mapping | A container listens on 8080 inside and is published as 443 outside |
| Reducing log noise | SSH moved to 2222 to cut automated login attempts |
| Getting through restrictive networks | A VPN or SSH service on 443 to pass a firewall that only allows web traffic |
| Product defaults | Tools such as Elasticsearch (9200) or Prometheus (9090) pick their own ports |
Web development shows this most often. Ports 8080, 8000 and 8443 are common alternates for HTTP and HTTPS. Browsers need the port in the URL, as in http://localhost:8080/. When the URL has no port, the browser uses the scheme's default.
Port numbers are not proof of protocol
Suppose you see traffic on port 443. You can reasonably guess it is TLS carrying HTTP. But you cannot be certain, for several reasons:
- Services move. SSH on 443 is legal and is sometimes used on purpose.
- Protocols tunnel inside others. HTTPS can carry VPN traffic, DNS queries (DNS over HTTPS) or arbitrary proxied streams.
- Defaults get reused. Port 8080 might be an HTTP proxy, a web app or an unrelated admin console.
- A listener may speak nothing useful. The port could be open and answer with garbage or close at once.
The reverse also holds. A closed port 22 does not mean the host has no SSH, because it may be on another port. An open port 80 does not mean a trustworthy web server.
The same rule applies to security tools. A scanner that reports "port 2222: unknown" has told you only that something accepts connections. To learn more, you must talk to the service.
Identifying a service by its behavior
Many protocols announce themselves. SSH servers send a banner line starting with SSH- as soon as you connect. SMTP servers send a 220 greeting. HTTP servers stay silent until the client speaks. The script below connects to any host and port you own and reports what it sees.
import socket
import sys
def probe(host, port, timeout=3.0):
"""Connect, wait briefly for a greeting, and guess the protocol."""
with socket.create_connection((host, port), timeout=timeout) as s:
s.settimeout(timeout)
try:
data = s.recv(256)
except socket.timeout:
data = b""
if data.startswith(b"SSH-"):
return "ssh", data
if data.startswith(b"220"):
return "smtp or ftp", data
if data:
return "unknown, sent a greeting", data
# No greeting: try a plain HTTP request.
s.sendall(b"HEAD / HTTP/1.0\r\n\r\n")
try:
reply = s.recv(256)
except socket.timeout:
reply = b""
if reply.startswith(b"HTTP/"):
return "http", reply
return "silent or unknown", reply
if __name__ == "__main__":
host = sys.argv[1]
port = int(sys.argv[2])
name, sample = probe(host, port)
print(f"{host}:{port} looks like: {name}")
print(sample[:80])Run it as python3 probe.py 127.0.0.1 22 against a machine you control. If you move sshd to port 2222, the script still reports ssh, which shows that the behavior identifies the protocol and the port number does not.
A TLS-wrapped service will not match any of these checks, because the first bytes sent are a TLS handshake. For those you need a TLS client, such as openssl s_client -connect host:port, which reveals the certificate and negotiated protocol.
Moving a service: what changes
Take SSH as the example. In /etc/ssh/sshd_config you set:
# /etc/ssh/sshd_config (excerpt)
Port 2222After reloading the daemon, check that it is listening and that the old port is closed:
ss -tlnp | grep -E ':(22|2222)\b'Then fix everything that assumed port 22:
- Clients. Use
ssh -p 2222 user@host, or setPort 2222for the host in~/.ssh/config. Tools built on SSH have their own flags, such asscp -P 2222(capital P) andsftp -P 2222. - Firewall rules. Allow 2222 before you close 22, or you can lock yourself out of a remote machine.
- Security policy modules. Systems with SELinux may refuse a daemon on an unlabeled port until you register it, for example with
semanage port. - Monitoring and scripts. Health checks and automation that hard-code 22 will fail.
The same applies to any service: change the listener, the firewall and the clients together, and keep a second login session open while you test.
Security through obscurity
Moving SSH off port 22 reduces automated noise in your logs, because many bots only try default ports. It does not stop a determined attacker. A port scan of all 65,535 TCP ports takes minutes on a fast link, and banner grabbing then identifies SSH on 2222 immediately.
Treat a non-standard port as a convenience and not as a control. The real protections are key-based authentication, patching, restricted source addresses and firewall rules that expose only what is needed.
Effects on firewalls and middleboxes
Many firewalls decide by port. That creates two common problems.
- Blocked legitimate use. A corporate firewall that allows only 80 and 443 blocks SSH on 2222 and any other custom service. Teams sometimes move services to 443 to pass, which weakens the intent of the policy.
- Hidden traffic. A rule that "allows 443" admits anything that connects there, whether it is a browser, a proxy or a tunnel.
Stricter environments use deep packet inspection or application-aware proxies. These look at the traffic itself and not just the port. They can flag SSH on 443, but they add cost and cannot see inside TLS unless they terminate it.
For your own rules, state the reason for each port, name the source addresses allowed, and default to denying everything else. When a service uses a custom port, the rule must name that exact port.
Common mistakes
- Assuming
https://host:8443andhttps://hostreach the same service. They are different sockets. - Forgetting the protocol. TCP 53 and UDP 53 are separate sockets, and a firewall may allow one but not the other.
- Opening a high port range "just in case". Ephemeral ranges are for client sockets, and a broad allow rule exposes any service that later binds there.
- Trusting a service name from
/etc/servicesor fromsocket.getservbyport. These tables map numbers to default names. They do not read the network, so they will label a custom SSH on port 25 as "smtp".
You can see that last point yourself:
import socket
# The services database maps numbers to default names only.
print(socket.getservbyport(25, "tcp")) # smtp, whatever is actually listening
print(socket.getservbyname("ssh", "tcp")) # 22Summary of decision criteria
- Use the default port when the service is public and clients expect it.
- Use a non-standard port when you need to run several instances, avoid privileged ports or sit behind a proxy.
- Never rely on the port alone for security or for identifying traffic. Confirm with banners, handshakes and authentication.
- Document every non-standard port and keep the firewall, client configuration and monitoring in step.
Exercise
A host accepts connections on TCP port 2222. A script connects and the first bytes received are "SSH-2.0-OpenSSH_9.6". What does this tell you, and what does it not tell you?
Show answer
It tells you the service on 2222 speaks the SSH protocol, because SSH servers send an identification banner starting with "SSH-" on connect. It does not tell you the service is secure, which software exposes it beyond the banner, or that SSH is the only service on the host. It also shows that the port number 2222 alone was not enough to identify the protocol.
Exercise
You move sshd from port 22 to 2222 on a remote server. List the order of steps that avoids locking yourself out.
Show answer
Add a firewall rule allowing TCP 2222. Change Port in sshd_config and reload sshd while keeping your current session open. Confirm with ss that 2222 is listening. Open a second session with ssh -p 2222 and confirm it works. Update ~/.ssh/config, scripts and monitoring. Only then remove the rule for port 22 and, if desired, stop listening on 22.
Exercise
A firewall allows only TCP 80 and 443 outbound. Why is it wrong to conclude that only web traffic leaves the network?
Show answer
The firewall checks port numbers, not protocols. Any program can use port 443, including SSH servers, VPNs and proxies, and those can carry arbitrary traffic. Only inspection of the traffic itself, or controlling which destinations are reachable, can restrict the protocol.