Chapters

Ports & sockets

An IP address gets a packet to the right machine. But kade-api runs Node, an SSH server, a monitoring agent and more, all using the network at once. Ports decide which program gets each packet. This chapter brings together what earlier chapters used piece by piece: port numbers, well-known ports, sockets, bind addresses, and how the 4-tuple keeps every connection apart.

Network building blocks · chapter 31 of 34 · 7 min read

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.

kade-api · one IP: 10.10.1.10OSreads thedestinationport, handsthe data on:443 → Node (kade-api):22 → sshd (chapter 29):9100 → metrics agentArriving packetsdst 10.10.1.10 : 443dst 10.10.1.10 : 22dst 10.10.1.10 : 9100dst 10.10.1.10 : 5432 → none

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

0 - 1023well-known1024 - 49151 · registeredassigned to specific apps, by convention49152 - 65535ephemeral
Well-knownStandard services (HTTP 80, HTTPS 443). On Linux and macOS, listening on them needs admin rights ("privileged ports").
RegisteredListed with IANA for specific software (PostgreSQL 5432), but any program can use them.
EphemeralTemporary 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".
That is why kade-api's Node process usually listens on 3000 or 8080 and something else (a load balancer, or a reverse proxy) takes 443. To let Node bind 443 directly on Linux, grant it the CAP_NET_BIND_SERVICE capability instead of running it as root.

Well-known ports worth knowing

PortProtocolServiceNotes
Web
80TCPHTTPMostly just redirects to 443 now (chapter 25's HSTS)
443TCP + UDPHTTPS; HTTP/3 over QUICUDP 443 for HTTP/3 (chapter 24)
3000, 5173, 8000, 8080TCPDev servers, app servers behind proxiesConventions only; nothing official
Remote access
22TCPSSH, SFTP, scpConstantly scanned by bots (chapter 29)
23TCPTelnetPlain text: never use; still found on old devices
3389TCP + UDPRDP (Windows Remote Desktop)A favourite target for ransomware; never expose it
Network services
53UDP + TCPDNSChapter 22
853TCPDNS over TLSChapter 22
67 / 68UDPDHCP server / clientChapter 27
123UDPNTPChapter 27
161 / 162UDPSNMP (device monitoring) / trapsPrinters and routers; old versions send passwords in clear
389 / 636TCPLDAP / LDAPSCompany directories (user accounts)
Email
25TCPSMTP between mail serversChapter 23. GCP blocks outgoing 25 from VMs, so use a mail provider's API or 587.
587 / 465TCPSMTP submission (apps and mail clients sending)With login and TLS
993 / 995TCPIMAPS / POP3S (reading mail)Encrypted versions of 143 / 110
Databases and queues
5432TCPPostgreSQLkade-db
3306TCPMySQL / MariaDBNever reachable from the internet
6379TCPRedisOften has no password by default
27017TCPMongoDBMany data leaks came from exposed default setups
5672TCPRabbitMQ (AMQP)
9200TCPElasticsearch / OpenSearchAnother frequent leak source when exposed
VPNs
51820UDPWireGuard
500 / 4500UDPIPsec (IKE / NAT traversal)GCP Cloud VPN uses these
1194UDP or TCPOpenVPN
Your OS has a list of these in /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 onReachable fromTypical use
1127.0.0.1:3000 (or ::1)Only programs on the same machineDev servers, local admin tools, a database only local apps use
2192.168.1.23:3000Anything that can reach that one interfaceRare: when a machine has several networks and should serve only one
30.0.0.0:3000 (or [::]:3000)Every interface: other devices on the LAN, and the internet if a firewall allowsReal servers

Try the combinations: your laptop runs the Kadé frontend dev server on port 3000.

The dev server listens on
Who connectsResultWhy
Browser on the laptop → localhost:3000 ok127.0.0.1 is covered by this bind address
Browser on the laptop → 192.168.1.23:3000 refusedThe server only listens on 127.0.0.1
Your phone on the same WiFi → 192.168.1.23:3000 timed outThe laptop firewall silently drops it before the OS sees it
A friend on the internet → 203.0.113.45:3000 timed outThe 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:

#SourceDestinationWhat it is
1192.168.1.23 : 5281434.87.120.15 : 443Browser tab 1 → kade-api
2192.168.1.23 : 5281534.87.120.15 : 443Browser tab 2 → kade-api (same server, new source port)
3192.168.1.23 : 52816142.250.4.100 : 443Google search
4192.168.1.40 : 5281434.87.120.15 : 443Your phone. Same source port as row 1 is fine: different source IP.
On the server, all of these share kade-api's one socket address 34.87.120.15:443; each still gets its own connected socket (chapter 21). The limit that does exist is on the client side: about 28,000 default ephemeral ports per destination IP and port (chapter 21).

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
kade-api sees three connections from 203.0.113.45, told apart only by port. With IPv6 (chapter 30) this rewriting disappears, and each device's own socket address is visible end to end.

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:

#StateReply to the SYNMeans
1openSYN-ACKA program is listening and reachable
2closedRSTThe machine is reachable; nothing listens there
3filterednothing (or ICMP unreachable)A firewall dropped it: the best answer for anything not meant to be public
Scan your own public IPs from outside now and then (only systems you own or are allowed to test). Anything open that you did not expect, like 5432 or 6379, needs fixing today.

Summary

#IdeaIn one lineNew terms
1Port16-bit number picking the program; TCP and UDP ports are separateport
2Ranges0-1023 well-known (privileged), 1024-49151 registered, 49152+ ephemeralprivileged port, ephemeral port
3Well-known22, 53, 80, 443, 5432 … are conventions; never expose databases
4-5Socket addressIP + port; the bind address (127.0.0.1, one IP, 0.0.0.0) decides who can connectsocket address, bind address
64-tupleOnly the whole tuple must be unique: many connections to one server4-tuple, 5-tuple
7NATPorts let many private sockets share one public IP
8Scanningopen = SYN-ACK, closed = RST, filtered = silenceport scan

Try it yourself

Every listening socket and its program

lsof -iTCP -sTCP:LISTEN -n -P

Your 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 -P

The OS's list of port names

grep -w -E '22|53|443|5432' /etc/services

The 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_range

What the internet sees

nmap -Pn -p 22,80,443,5432 YOUR_PUBLIC_IP

Whether something listens

nc -vz localhost 3000

Spotted a mistake, or something unclear? Suggest an edit on GitHub