basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Ports and sockets: 5-tuple, ephemeral ports, 0.0.0.0 vs 127.0.0.1, and ss -tulnp

    An IP address gets a packet to the host; a port number (16 bits, 0–65535) tells the kernel which process receives it. Covers the connection 5-tuple, client ephemeral ports, binding to 0.0.0.0 vs 127.0.0.1, and reading ss -tulnp output.

    What you’ll get

    Run sudo ss -tulnp to see which TCP and UDP ports are open on a server, which addresses they listen on, and which processes own them:

    sudo ss -tulnp
    # Netid State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
    # udp   UNCONN 0      0            0.0.0.0:5353      0.0.0.0:*    users:(("python3",pid=6687,fd=5))
    # tcp   LISTEN 0      5          127.0.0.1:5432      0.0.0.0:*    users:(("python3",pid=6687,fd=4))
    # tcp   LISTEN 0      10           0.0.0.0:8080      0.0.0.0:*    users:(("python3",pid=6687,fd=3))

    The flags break down as -t for TCP, -u for UDP, -l for listening sockets only, -n for numeric ports and IPs without DNS lookups, and -p for process name and PID (requires sudo). Each line answers three questions: the protocol, which addresses it accepts from, and which process holds the port:

    sudo ss -tulnp · Pick a segment
    If a port listens on 127.0.0.1:5432 instead of 0.0.0.0:5432, external clients get Connection refused before the application sees anything.

    Why ports exist and how the 5-tuple works

    One server can run SSH, a web server, a database, and a monitoring agent all on the same IP address 10.0.10.10. L3 (Routing and LPM) gets the packet to the network interface, and L4 uses the port number (a 16-bit integer from 0 to 65535) to decide which program’s socket receives the data.

    A socket in Linux is a kernel network endpoint that a program reads and writes like a file descriptor (fd=3 in ss output).

    Thousands of clients can connect to port 8080 on the same server simultaneously without mixing up replies because the kernel identifies each connection by a 5-tuple, not by the destination port alone:

    (Protocol, Source IP, Source Port, Destination IP, Destination Port)

    When client 10.0.10.20 opens two parallel connections to 10.0.10.10:8080, its kernel assigns each one a different free source port:

    ss -tnp
    # State      Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
    # ESTAB      0      0         10.0.10.10:8080    10.0.10.20:40780 users:(("python3",pid=6687,fd=6))
    # ESTAB      0      0         10.0.10.10:8080    10.0.10.20:40786 users:(("python3",pid=6687,fd=7))

    The 5-tuples (TCP, 10.0.10.20, 40780, 10.0.10.10, 8080) and (TCP, 10.0.10.20, 40786, 10.0.10.10, 8080) differ, so both streams run independently. Similarly, port 8080 can be bound on 127.0.0.1 and 10.0.10.10 at the same time (different local IPs), whereas binding two sockets to the exact same 0.0.0.0:8080 fails unless both use SO_REUSEPORT.

    Port ranges and client ephemeral ports

    Under RFC 6335 and Linux kernel defaults, the 65,536 ports split into three ranges:

    Range Name Typical use Linux behavior
    0–1023 Well-known (system) 22 SSH, 53 DNS, 80 HTTP, 443 HTTPS Blocked for regular users (ip_unprivileged_port_start = 1024)
    1024–49151 Registered 5432 PostgreSQL, 6379 Redis, 8080 HTTP backends Available without root
    32768–60999 (Linux) Ephemeral (dynamic) Outbound client connections (curl, browsers, proxies) Assigned automatically by the kernel from net.ipv4.ip_local_port_range

    IANA’s dynamic range starts at 49152, but Linux uses its own default interval — inspect both limits with sysctl:

    sysctl net.ipv4.ip_local_port_range net.ipv4.ip_unprivileged_port_start
    # net.ipv4.ip_local_port_range = 32768 60999
    # net.ipv4.ip_unprivileged_port_start = 1024

    This sets a practical ceiling for proxies and busy clients: from one local IP to a single remote IP:port, the kernel can hold at most 60999 − 32768 + 1 = 28,232 concurrent outbound connections until ports free up. The inbound side has no such cap: a server can accept hundreds of thousands of connections on port 443 as long as memory and file descriptor limits (ulimit -n) allow.

    Binding a socket: 0.0.0.0, 127.0.0.1, [::], and a specific IP

    When a service starts, it passes a port and a bind address to the kernel:

    • 127.0.0.1 (or [::1] for IPv6) — the lo loopback interface only. Only programs on the same host can connect; the socket stays unreachable from the network even if firewall rules are misconfigured. Use this for databases or local web UIs sitting behind a reverse proxy on the same machine.
    • A specific interface IP (such as 10.0.10.10 or a WireGuard IP) — the service answers only on that interface and stays unreachable on the server’s public IP.
    • 0.0.0.0 — all IPv4 interfaces on the host at once: lo, LAN, and the external interface.
    • [::] — all IPv6 interfaces, plus all IPv4 interfaces when net.ipv6.bindv6only = 0 (the Linux default).
    1. Client 10.0.10.20✓ request to :8080
    2. Kernel 10.0.10.10✓ socket 0.0.0.0:8080
    3. Application✓ accept() -> fd=6
    4. Response✓ HTTP/1.1 200 OK
    $ sudo ss -tulnp 'sport = :8080'
    tcp   LISTEN 0      10     0.0.0.0:8080    0.0.0.0:*    users:(("python3",pid=6687,fd=3))
    $ curl -s http://10.0.10.10:8080/
    ok
    What happened

    The socket listens on every IPv4 interface on the server, so the incoming connection to 10.0.10.10:8080 succeeds.

    Check with

    sudo ss -tulnp and an external check with nc -zv 10.0.10.10 8080

    1. Client 10.0.10.20✓ SYN to 10.0.10.10:5432
    2. Kernel 10.0.10.10✕ nothing listening on eth0
    3. Kernel reply✕ immediate RST
    4. curl (exit 7)✕ Connection refused
    # On the server, the socket is bound to loopback only:
    tcp   LISTEN 0      5    127.0.0.1:5432    0.0.0.0:*    users:(("python3",pid=6687,fd=4))
    # From another machine (10.0.10.20):
    $ curl -s http://10.0.10.10:5432/   # exit 7: Connection refused
    Cause

    The service listens only on 127.0.0.1. Local connections on the server work, while packets to 10.0.10.10 are rejected immediately by the kernel.

    Check with

    sudo ss -tulnp | grep 5432

    1. New service✓ bind(0.0.0.0:8080)
    2. Linux kernel✕ pair held by pid 6687
    3. Startup✕ EADDRINUSE
    OSError: [Errno 98] Address already in use
    $ sudo ss -tulnp 'sport = :8080'
    tcp   LISTEN 0      10     0.0.0.0:8080    0.0.0.0:*    users:(("python3",pid=6687,fd=3))
    Cause

    Process PID 6687 already holds the same address and port.

    Check with

    sudo ss -tulnp 'sport = :8080'

    Useful ss filters

    ss (the replacement for netstat, part of iproute2) filters sockets inside the kernel without piping through grep:

    # Check what is listening on a specific port:
    sudo ss -tulnp 'sport = :8080'
    
    # List all active TCP connections with process names:
    sudo ss -tnp state established
    
    # Summary of socket counts by type and state:
    ss -s
    A web server listens on `10.0.10.10:443`. Two browsers on the same client machine `10.0.10.20` download files at the same time. How does the kernel keep their packets separate?
    ss -tnp 'sport = :443'
    A web server listens on `10.0.10.10:443`. Two browsers on the same client machine `10.0.10.20` download files at the same time. How does the kernel keep their packets separate?
    Answer:
    10.0.10.10:443 <-> 10.0.10.20:40780 and 10.0.10.10:443 <-> 10.0.10.20:40786

    The kernel tracks each connection by its full 5-tuple (protocol, src IP, src port, dst IP, dst port). Different client ephemeral ports produce two independent sockets on the server's single port 443.

    A database runs on `10.0.10.10`, port `5432` is open in `nftables`, and `nc -zv 127.0.0.1 5432` succeeds locally. From `10.0.10.20`, you get an immediate `Connection refused`. What is wrong?
    sudo ss -tulnp 'sport = :5432'
    # tcp   LISTEN 0      5     127.0.0.1:5432     0.0.0.0:*
    A database runs on `10.0.10.10`, port `5432` is open in `nftables`, and `nc -zv 127.0.0.1 5432` succeeds locally. From `10.0.10.20`, you get an immediate `Connection refused`. What is wrong?
    Answer:
    tcp LISTEN 0 5 127.0.0.1:5432 0.0.0.0:*

    A firewall drop rule causes a timeout, not an instant Connection refused. An immediate refusal combined with a working local connection on 127.0.0.1 points straight to a loopback-only bind.

    If it doesn’t work

    A service is running and curl http://127.0.0.1:5432/ answers on the server itself, but another machine gets Connection refused

    Cause
    The socket is bound only to the loopback interface 127.0.0.1 (or [::1]): the kernel accepts connections only from the same host, even if the firewall allows the port.
    Fix
    Check the Local Address:Port column in sudo ss -tulnp. To expose the service to the network, change its bind address to 0.0.0.0 (or a specific interface IP) and restart it.

    A service fails to start with Address already in use (EADDRINUSE)

    Cause
    Another process already holds the same address, port, and protocol — for example, an older instance of the program or a Docker container.
    Fix
    Find the port owner with sudo ss -tulnp 'sport = :8080', then stop the conflicting process or move the service to a different port.

    ss -tulnp lists open ports, but the last Process column is empty

    Cause
    An unprivileged user cannot read the process names and PIDs owned by root or other accounts.
    Fix
    Run the command as root: sudo ss -tulnp.

    A program running as a regular user crashes with Permission denied when binding to port 80 or 443

    Cause
    Ports below 1024 are privileged in Linux (net.ipv4.ip_unprivileged_port_start = 1024), so an unprivileged process cannot bind to them.
    Fix
    Put a reverse proxy like Traefik or nginx on 80/443, grant the binary CAP_NET_BIND_SERVICE (AmbientCapabilities=CAP_NET_BIND_SERVICE in systemd), or listen on a high port.

    ss -tulnp shows a service on [::]:443 with no 0.0.0.0:443 line, yet IPv4 connections still arrive

    Cause
    Linux defaults to dual-stack mode (net.ipv6.bindv6only = 0): a socket bound to [::] accepts both IPv6 and IPv4 (as IPv4-mapped addresses ::ffff:203.0.113.10).
    Fix
    This is standard kernel behavior. To disable IPv4 on that socket, set IPV6_V6ONLY explicitly in the application.

    Also in this chapter