netstat: Usage, Options & Common CI Errors
netstat lists network connections, listening ports, and routes (the legacy tool).
netstat is the classic way to see listening ports, but it is part of the deprecated net-tools package and is absent from most modern and slim images - where ss is the replacement. Knowing both matters because tutorials still use netstat.
What it does
netstat reports network connections, listening sockets, the routing table, and interface statistics. Its most common CI use is netstat -tlnp to confirm a service is listening on a port - a role now filled by ss, which reads the same kernel data faster.
Common usage
netstat -tlnp # TCP listening sockets with PID
netstat -tulnp # TCP + UDP listening, numeric
netstat -rn # routing table, numeric
netstat -i # interface statistics
netstat -an | grep LISTEN # all listening socketsOptions
| Flag | What it does |
|---|---|
| -t / -u | TCP / UDP sockets |
| -l | Only listening sockets |
| -n | Numeric (no name resolution) |
| -p | Show owning PID/program (needs privilege) |
| -r | Routing table |
| -i | Interface statistics |
Common errors in CI
"netstat: command not found" is the norm on Alpine and distroless images - net-tools is deprecated; use ss -tlnp instead (same info, in iproute2). -p shows blank program names without privilege. netstat only sees its own network namespace, so it cannot observe ports inside a separate container. If a script must support both old and new images, prefer ss and fall back to netstat, or parse /proc/net/tcp directly, which needs no package.