What a port is
A port is a 16-bit number (0-65,535) in the TCP or UDP header (chapters 15 and 18) that identifies which program a packet is for. If the IP address is a building's street address, the ports are the numbered doors inside it.
Nothing listens on 5432 on kade-api (PostgreSQL runs on kade-db), so that packet gets an RST for TCP or ICMP "port unreachable" for UDP (chapters 15-16). TCP and UDP have separate port spaces: TCP 53 and UDP 53 are different doors.
Wide by design - scroll it sideways.
Port ranges
| Well-known | Standard services (HTTP 80, HTTPS 443). On Linux and macOS, listening on them needs admin rights ("privileged ports"). |
| Registered | Listed with IANA for specific software (PostgreSQL 5432), but any program can use them. |
| Ephemeral | Temporary ports the OS picks as the source port for your outgoing connections. Linux actually uses 32768-60999 by default. Asking for port 0 means "OS, pick a free one for me". |
CAP_NET_BIND_SERVICE capability instead of running it as root.Well-known ports worth knowing
| Port | Protocol | Service | Notes |
|---|---|---|---|
| Web | |||
| 80 | TCP | HTTP | Mostly just redirects to 443 now (chapter 25's HSTS) |
| 443 | TCP + UDP | HTTPS; HTTP/3 over QUIC | UDP 443 for HTTP/3 (chapter 24) |
| 3000, 5173, 8000, 8080 | TCP | Dev servers, app servers behind proxies | Conventions only; nothing official |
| Remote access | |||
| 22 | TCP | SSH, SFTP, scp | Constantly scanned by bots (chapter 29) |
| 23 | TCP | Telnet | Plain text: never use; still found on old devices |
| 3389 | TCP + UDP | RDP (Windows Remote Desktop) | A favourite target for ransomware; never expose it |
| Network services | |||
| 53 | UDP + TCP | DNS | Chapter 22 |
| 853 | TCP | DNS over TLS | Chapter 22 |
| 67 / 68 | UDP | DHCP server / client | Chapter 27 |
| 123 | UDP | NTP | Chapter 27 |
| 161 / 162 | UDP | SNMP (device monitoring) / traps | Printers and routers; old versions send passwords in clear |
| 389 / 636 | TCP | LDAP / LDAPS | Company directories (user accounts) |
| 25 | TCP | SMTP between mail servers | Chapter 23. GCP blocks outgoing 25 from VMs, so use a mail provider's API or 587. |
| 587 / 465 | TCP | SMTP submission (apps and mail clients sending) | With login and TLS |
| 993 / 995 | TCP | IMAPS / POP3S (reading mail) | Encrypted versions of 143 / 110 |
| Databases and queues | |||
| 5432 | TCP | PostgreSQL | kade-db |
| 3306 | TCP | MySQL / MariaDB | Never reachable from the internet |
| 6379 | TCP | Redis | Often has no password by default |
| 27017 | TCP | MongoDB | Many data leaks came from exposed default setups |
| 5672 | TCP | RabbitMQ (AMQP) | |
| 9200 | TCP | Elasticsearch / OpenSearch | Another frequent leak source when exposed |
| VPNs | |||
| 51820 | UDP | WireGuard | |
| 500 / 4500 | UDP | IPsec (IKE / NAT traversal) | GCP Cloud VPN uses these |
| 1194 | UDP or TCP | OpenVPN |
/etc/services. A port number is only a convention: a service can run on any port, and anything can run on 443. Firewalls that "allow only 443" do not guarantee the traffic is HTTPS.Sockets: IP + port
A socket address is an IP address plus a port, e.g. 10.10.1.10:443: the exact endpoint of communication. When code "opens a socket" (chapter 21), it claims one of these endpoints to send or receive on. IPv6 socket addresses use brackets: [2001:db8::1]:443 (chapter 30).
The bind address decides who can reach you
A server listens on a port and an address. Which address you bind to decides who can connect, a detail behind many "works on my machine" problems:
| # | Listen on | Reachable from | Typical use |
|---|---|---|---|
| 1 | 127.0.0.1:3000 (or ::1) | Only programs on the same machine | Dev servers, local admin tools, a database only local apps use |
| 2 | 192.168.1.23:3000 | Anything that can reach that one interface | Rare: when a machine has several networks and should serve only one |
| 3 | 0.0.0.0:3000 (or [::]:3000) | Every interface: other devices on the LAN, and the internet if a firewall allows | Real servers |
Try the combinations: your laptop runs the Kadé frontend dev server on port 3000.
| Who connects | Result | Why |
|---|---|---|
| Browser on the laptop → localhost:3000 | ok | 127.0.0.1 is covered by this bind address |
| Browser on the laptop → 192.168.1.23:3000 | refused | The server only listens on 127.0.0.1 |
| Your phone on the same WiFi → 192.168.1.23:3000 | timed out | The laptop firewall silently drops it before the OS sees it |
| A friend on the internet → 203.0.113.45:3000 | timed out | The router has no NAT row or forward for it: dropped (chapter 10) |
Connections: the 4-tuple
A TCP connection is identified by two socket addresses: (source IP, source port, destination IP, destination port), the 4-tuple from chapter 1. Add the protocol and it is the 5-tuple used by NAT and firewalls (chapter 10). Only the whole tuple has to be unique. That is why:
| # | Source | Destination | What it is |
|---|---|---|---|
| 1 | 192.168.1.23 : 52814 | 34.87.120.15 : 443 | Browser tab 1 → kade-api |
| 2 | 192.168.1.23 : 52815 | 34.87.120.15 : 443 | Browser tab 2 → kade-api (same server, new source port) |
| 3 | 192.168.1.23 : 52816 | 142.250.4.100 : 443 | Google search |
| 4 | 192.168.1.40 : 52814 | 34.87.120.15 : 443 | Your phone. Same source port as row 1 is fine: different source IP. |
Tying it back to NAT
Inside the home network, devices use private IPs and pick their own source ports. The router's NAT (chapter 10) rewrites the private socket address into its one public IP with a port it chooses, and keeps a table to reverse it. Ports are what make sharing one public IP possible:
| Inside (private) | Outside (after NAT) | |
|---|---|---|
| 192.168.1.23 : 52814 | → | 203.0.113.45 : 40001 |
| 192.168.1.23 : 52815 | → | 203.0.113.45 : 40002 |
| 192.168.1.40 : 52814 | → | 203.0.113.45 : 40003 |
Port scanning: open, closed, filtered
Attackers (and you, checking your own servers) find services by scanning: sending SYNs to many ports and reading the replies. The three answers map directly to chapter 16:
| # | State | Reply to the SYN | Means |
|---|---|---|---|
| 1 | open | SYN-ACK | A program is listening and reachable |
| 2 | closed | RST | The machine is reachable; nothing listens there |
| 3 | filtered | nothing (or ICMP unreachable) | A firewall dropped it: the best answer for anything not meant to be public |
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | Port | 16-bit number picking the program; TCP and UDP ports are separate | port |
| 2 | Ranges | 0-1023 well-known (privileged), 1024-49151 registered, 49152+ ephemeral | privileged port, ephemeral port |
| 3 | Well-known | 22, 53, 80, 443, 5432 … are conventions; never expose databases | |
| 4-5 | Socket address | IP + port; the bind address (127.0.0.1, one IP, 0.0.0.0) decides who can connect | socket address, bind address |
| 6 | 4-tuple | Only the whole tuple must be unique: many connections to one server | 4-tuple, 5-tuple |
| 7 | NAT | Ports let many private sockets share one public IP | |
| 8 | Scanning | open = SYN-ACK, closed = RST, filtered = silence | port scan |
Try it yourself
Every listening socket and its program
- What to look for:
127.0.0.1:5432(local only) vs*:3000(all interfaces). - Runs on: Linux: ss -tlnp
- From section: 5. The bind address decides who can reach you
lsof -iTCP -sTCP:LISTEN -n -PYour open connections
- What to look for: Many source ports going to the same destination :443.
- Runs on: Linux: ss -tnp
- From section: 6. Connections: the 4-tuple
lsof -iTCP -sTCP:ESTABLISHED -n -PThe OS's list of port names
- What to look for: ssh, domain, https, postgresql.
- From section: 3. Well-known ports worth knowing
grep -w -E '22|53|443|5432' /etc/servicesThe ephemeral range in use
- What to look for:
32768 60999. - Runs on: Linux
- From section: 2. Port ranges
cat /proc/sys/net/ipv4/ip_local_port_rangeWhat the internet sees
- What to look for: Only 80/443 open; 22 and 5432 filtered.
- Runs on: only on your own systems
- From section: 8. Port scanning: open, closed, filtered
nmap -Pn -p 22,80,443,5432 YOUR_PUBLIC_IPWhether something listens
- What to look for: "succeeded" vs "refused".
- From section: 1. What a port is
nc -vz localhost 3000