netstat: print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships (net-tools)
netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e] [--timers|-o] [--program|-p] [--wide|-W] [--continuous|-c]
netstat {--route|-r} | {--interfaces|-i} | {--statistics|-s}
What it does
Prints information about the Linux networking subsystem. By default, netstat displays a list of open sockets. This program is mostly obsolete: the replacement for netstat is ss, for netstat -r ip route, for netstat -i ip -s link. It is still what many container images carry: in busybox images netstat is the busybox applet, with the short options [-ral] [-tuwx] [-enWp].
Options you will use
-t, --tcp- TCP sockets.
-u, --udp- UDP sockets.
-w, --raw- Raw sockets.
-x, --unix- Unix domain sockets.
-l, --listening- Show only listening sockets. (These are omitted by default.)
-a, --all- Show both listening and non-listening sockets.
-n, --numeric- Show numerical addresses instead of trying to determine symbolic host, port or user names.
-p, --program- Show the PID and name of the program to which each socket belongs. A hyphen is shown if the user has no permission to inspect the other process.
-e, --extend- Display additional information. Use this option twice for maximum detail.
-o, --timers- Include information related to networking timers.
-W, --wide- Do not truncate addresses by using output as wide as needed.
-c, --continuous- Print the selected information every second continuously.
-r, --route- Display the kernel routing tables.
-i, --interfaces- Display a table of all network interfaces and their counters.
-s, --statistics- Display summary (SNMP) statistics for each protocol.
Examples
$ netstat -tnpTCP connections, numeric, with the owning program
$ sudo netstat -tlnplistening TCP ports and their programs (ss -tlnp does the same)
$ kubectl exec POD -- netstat -tnthe connections inside a pod (busybox netstat)
Gotchas
- State column: ESTABLISHED = the connection is up; SYN_SENT = actively attempting to establish a connection (no answer yet: a firewall or a policy dropping the packets looks like this); TIME_WAIT = closed, waiting to handle packets still in the network; CLOSE_WAIT = the remote end has shut down, waiting for the local socket to close.
- Send-Q growing on an ESTABLISHED connection = bytes the peer has not acknowledged: the path stopped delivering.
- Without root (or without the same user), -p shows "-" for other users' sockets.
Try netstat in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.