Linux Command Reference ยท Networking
Linux netstat command
Reference for the Linux netstat command: list connections, listening ports, routes and interface counters, and the modern ss and ip commands that replace it.
netstat prints network connections, listening ports, routing tables and interface statistics. It belongs to the old net-tools package, which also holds ifconfig, route and arp. These tools are deprecated: current distributions often do not install them, and ss and ip do the same work faster.
You still need to read netstat commands in old documentation, scripts and on legacy systems, and its options remain widespread on BSD and macOS.
netstat -tulpn # listening sockets with processes netstat -rn # routing table netstat -i # interface counters
Try it
netstat is not installed in our sandbox, so the command is read-only. The example processes a saved netstat -tan listing, counting connections per state, which is a classic one-liner.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
netstat [OPTIONS]
| Option | Description |
|---|---|
-t, -u, -x |
TCP, UDP, Unix sockets. |
-l |
Listening sockets only. |
-a |
All sockets. |
-n |
Numeric addresses and ports, no DNS lookups. |
-p |
Show PID and program name (root for all). |
-r |
Routing table. |
-i |
Interface table; -ie is like ifconfig. |
-s |
Protocol statistics. |
-c |
Repeat every second. |
-e |
Extended information (user, inode). |
Returns: 0 on success; non-zero for invalid options.
Usage notes
- Equivalents:
netstat -tulpnisss -tulpn;netstat -rnisip route;netstat -iisip -s link;netstat -sisss -s(andnstatfor counters). - Always add
-n. Without it,netstattries to resolve each address and port to a name, which can take minutes when DNS is slow or unreachable. - Process names need privileges. As a normal user
-phides processes belonging to others; run it withsudo. - Slower on large servers because it parses
/proc/net/*text files, whereasssuses a netlink interface. - Installing it: the package is
net-toolson Debian, Ubuntu and Red Hat families. - Different on BSD and macOS. Flags such as
-pmean "protocol" there, and-lis not available; scripts that must run on both need care. - Reading the state column:
LISTENis a waiting server,ESTABLISHEDan active connection,TIME_WAITa recently closed one waiting out stray packets,CLOSE_WAITa peer closed but the local program has not.
Related
ss,ip,nc,nmap- Lessons: Network Troubleshooting, Networking Basics