Networking Fundamentals

TCP, UDP and Ports

30 мин

Ports

An IP address identifies a machine; a port (0 to 65535) identifies a program on it. A connection is defined by four values: source IP, source port, destination IP and destination port.

Port Service
22 SSH
25, 587 SMTP (email sending)
53 DNS
80 HTTP
443 HTTPS
5432 PostgreSQL
6379 Redis
3000, 5000, 8000 common development servers

Ports below 1024 need root to bind on Linux, which is one reason apps listen on 8000 behind Nginx on 80/443.

TCP vs UDP

TCP UDP
Connection yes (handshake) no
Delivery guaranteed, in order best effort
Used by HTTP/1.1, HTTP/2, SSH, databases DNS, video calls, games, HTTP/3 (QUIC)

Seeing connections and listeners

ss -tulpn                  # listening TCP/UDP ports and their programs
ss -tn state established   # current connections
sudo lsof -i :8000         # what is using port 8000?

"Address already in use" means another process holds the port. Find it with the commands above, then stop it or choose another port.

Testing whether a port is reachable

nc -zv db.internal 5432        # TCP connect test
curl -v telnet://host:6379     # alternative when nc is missing
Result Meaning
succeeded / connected something is listening and reachable
connection refused the host is reachable but nothing listens on that port
timed out a firewall is silently dropping packets, or the host is unreachable

"Refused" and "timed out" point to different fixes: start the service, or open the firewall.

Check yourself

  • Why do apps commonly run on 8000 behind Nginx?
  • What does "connection refused" tell you versus a timeout?
  • Which command shows which program listens on each port?

Ваш прогресс сохраняется только в этом браузере.

Отправить на проверку (10 баллов)