What a Port Is, and Why 65535 of Them
A machine runs web, database and SSH at once. The OS hands each packet to the right app via port numbers: the destination port in the TCP/UDP header decides ownership.
TCP header: [source port][destination port][sequence]…
destination port = which app on this machine handles this connection
0–65535 gives 65536 numbers: 1–1023 are well-known ports (often requiring privileged binding), 1024–49151 registered, and 49152–65535 dynamic/private.
What an Open Port Actually Exposes
"Only open 80/443" is repeated not because those two are safe, but because every extra open port adds a service a scanner can find. Risk is a product of three factors:
Reachability × service weakness × data value. A port that answers is only the first factor — a necessary condition, not proof of compromise. So whether a port should be open is not decided by its number; it is decided by three things:
- Which address it binds to. Listening on
127.0.0.1admits only the local machine;0.0.0.0admits every reachable interface. TheLocal Addresscolumn inss -tlnpis the answer, and many people believe a service is internal when it actually binds to0.0.0.0. - Whether the service authenticates. Databases and caches are the usual victims: a publicly reachable 6379 (Redis) or 27017 (MongoDB) with no auth is close to handing over the data directory; exposed 3306 and 5432 move straight into the range of weak-credential brute force.
- Whether the management plane is naked. 3389 (RDP) and 22 (SSH) are prime targets for credential stuffing and brute force; backup admin ports such as 8443 are often opened temporarily and never closed.
Two further misjudgements:
- 443 alone is not safety. Application-layer bugs — injection, broken access control, deserialisation — still hit 443. Minimising exposure removes the "unknown service" layer, not application vulnerabilities.
- A private network or NAT is not a boundary. Once an attacker owns one internal host, lateral movement rides precisely on those "internal only" ports; cloud security groups and host firewalls must agree in both layers, and configuring one layer equals configuring none.
Port scanning rests on this: a service answers on the port it listens on, so probing tells you whether a live service exists on an IP. Self-audits and reconnaissance use the same trick — the only difference is who is scanning.
Common Ports by Purpose
Web and Encryption
| Port | Service | Notes |
|---|---|---|
| 80 | HTTP | often 301 → 443 |
| 443 | HTTPS | encrypted, a must-open |
| 8443 | HTTPS alt | web admin / backup; do not leave public |
Remote Admin and File Transfer
| Port | Service | Notes |
|---|---|---|
| 22 | SSH / SFTP / SCP | change default, keys only, source-IP limits |
| 3389 | RDP (Windows) | strongly discourage public exposure |
| 21 | FTP | plaintext; prefer SFTP/SCP |
| 990 | FTPS (implicit) | encrypted FTP |
Databases
| Port | Service | Notes |
|---|---|---|
| 3306 | MySQL/MariaDB | usually internal only |
| 5432 | PostgreSQL | internal only |
| 6379 | Redis | never expose publicly |
| 27017 | MongoDB | never expose publicly |
Dev and Debug
| Port | Service | Notes |
|---|---|---|
| 3000 | Node/Vite dev | local binding is fine |
| 3300 | common dev service | local use |
| 5000 / 8000 | Flask / generic | bind to localhost |
| 53 | DNS | UDP, service-private |
Mail and More
| Port | Service | Notes |
|---|---|---|
| 25 / 587 | SMTP / submission | 25 often blocked by clouds |
| 110 / 143 | POP3 / IMAP | plaintext; prefer encrypted |
| 993 / 995 | IMAPS / POP3S | encrypted, recommended |
Source vs Destination Port
- Destination port = the service's "doorplate" the client connects to;
- Source port = a random high port the client picks when initiating, so the server can reply to the right flow.
Outbound source ports are random and rarely need opening; only inbound "which destination port to expose" matters. The usual rule: inbound allows the destination ports you need, outbound allows ephemeral high ports.
The Security Baseline, Ranked by Risk
- Pull databases and caches inward first: 3306, 5432, 6379 and 27017 bind to the internal network or localhost only, closed by both the security group and the host firewall;
- Never leave management naked: 22 and 3389 must have at least one of source-IP limits, a changed port, or key-only auth; put 3389 behind a VPN or jump host;
- Web exposes 80/443 only, with 80 permanently 301-ing to 443, and backup ports such as 8443 closed when done;
- Rules live in both layers: the cloud security group and the host firewall must agree; never "open everything and fix later";
- Scan your own public IP regularly: compare against the listening addresses in
ss -tlnpand confirm no debug or admin port was left open.
After the last step, ask once: "does this port really need to be reachable from the internet?" Usually the answer is no, and removing one port removes one attack surface.
Reproducible Results
Do not memorise the table — ask the OS. Python's standard library ships the IANA services database:
import socket
socket.getservbyname("ssh", "tcp") # → '22'
socket.getservbyname("http", "tcp") # → '80'
socket.getservbyname("https", "tcp") # → '443'
The reverse lookup is socket.getservbyport(22). For the full cross-reference (including UDP and middleware ports) see the common ports reference, and generate high ports outside the privileged range with the random port generator.