Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

netstat Command Guide for the Classic Network Snapshot

netstat (“network statistics”) prints connection tables, routing tables, and interface counters — the long-time standard network tool that ss and ip modernized.


What is netstat, and Why Use It?

netstat takes a full snapshot of a machine’s network stack: every open connection, every listening port, the routing table, interface traffic counters, and masquerade connections. For decades it was the answer to “what is this machine doing on the network?”

It is being superseded by ss and ip — those tools are faster and still maintained — but netstat remains valuable for three reasons:

  1. It is everywhere. Every old server, tutorial, and troubleshooting guide uses netstat, and understanding its output is required reading.
  2. It sometimes shows things newer tools hide — notably per-interface traffic statistics and multicast group membership.
  3. Habit and speed — on a machine without iproute2, netstat is often the only option.

Why Learn netstat?

  • You will meet it in the wild — admin guides, forums, and incident writeups assume you recognize netstat -tulpn.
  • It teaches the concepts — its output format is what ss inherited; learn one and you know the other.
  • Some output has no modern twin — interface statistics and multicast memberships are still clearest here.

netstat ships in the deprecated net-tools package on many distributions — install it when you need it, but prefer ss for new work.


1. Seeing Connections

The default invocation lists all connections:

netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp       0      0  192.168.1.20:52144     93.184.216.34:443       ESTABLISHED
tcp       0      0  192.168.1.20:52100     151.101.1.69:443       TIME_WAIT

Each row: protocol, queue sizes, local and foreign addresses, and connection state. The default view hides listening ports; the useful flags are next.

2. The Flags You Will Actually Use

The classic combinations:

netstat -t          # TCP connections
netstat -u          # UDP
netstat -l          # listening sockets (no remote side)
netstat -a          # everything — listening and connected
netstat -n          # numeric addresses, no DNS lookups (fast)

The famous one combines them all:

netstat -tulpn
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      541/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      541/sshd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           412/dhclient

What each letter adds:

  • -t / -u — TCP / UDP sockets.
  • -l — listening sockets only.
  • -p — the PID and program name owning each socket (needs root for other users).
  • -n — skip DNS resolution; addresses and ports stay numeric.

netstat -tulpn is the command for “what is this machine offering on the network?” — the same job ss -tulpn does.

3. Routing and Interfaces

Beyond sockets, netstat shows the rest of the network stack:

netstat -r          # the kernel routing table
netstat -rn         # routing table, numeric (fast)
netstat -i          # per-interface statistics: packets, errors, collisions
netstat -g          # multicast group memberships
netstat -I          # interface configuration (older flag styles vary)

netstat -i is where netstat still wins:

Iface  MTU  RX-OK   RX-ERR RX-DRP  TX-OK  TX-ERR
lo      0    15234    0      0     15234    0
eth0     0   98321    0      0     54321    0

Receive/transmit packet counts and errors per interface — quick answers to “is this interface dropping packets?”.

4. Counting Connections

To see what is hammering a machine by protocol and state:

netstat -t | awk '{print $6}' | sort | uniq -c | sort -rn
  312 TIME_WAIT
   12 ESTABLISHED
    8 CLOSE_WAIT

This counts connection states for TCP — a rising TIME_WAIT count means many short-lived connections (normal for web servers), while growing CLOSE_WAIT means the local program is not closing its sockets (a bug worth chasing).

5. The Modern Equivalents

netstat is deprecated in favor of its replacements. The mapping:

Old commandModern replacement
netstat -tulpnss -tulpn
netstat -rip route
netstat -iip -s link
netstat -snstat or ip -s

When a tutorial says netstat, mentally substitute ss — the output is nearly identical, and ss is faster on busy machines.


Common Pitfalls

  • It may not be installed — netstat lives in the deprecated net-tools package; on newer systems you may need sudo apt install net-tools (or the dnf/pacman equivalent) before it exists at all.
  • -p needs root to see everything — without sudo, process names for other users’ sockets are blank.
  • Slow output without -n — DNS lookups for every address can hang the command; add -n whenever speed matters.
  • Confusing netstat -i errors with problems — RX-DRP and TX-ERR counts are cumulative since boot; only increasing counts indicate a live issue.
PageRelationship
ssThe modern replacement — prefer it for new work
ipModern replacement for interface and routing views
pingTests whether a listed address is reachable
lsofShows which files and sockets a process has open

netstat Cheatsheet

CommandWhat it does
netstatList all connections
netstat -tTCP connections
netstat -tulnListening TCP/UDP ports, numeric
netstat -tulpnListening ports with owning process (as root)
netstat -rKernel routing table
netstat -rnRouting table, numeric
netstat -iPer-interface packet and error statistics
netstat -gMulticast group memberships
netstat -t | awk '{print $6}' | sort | uniq -cCount connection states