Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Install netstat

If netstat answers "command not found", install net-tools:

sudo apt install net-tools      # Ubuntu, Debian
sudo dnf install net-tools      # AlmaLinux, Rocky Linux

The package also brings back ifconfig, route, and arp. net-tools is considered legacy, so for new scripts use ss and ip, which come with iproute2 on current systems; the netstat man page itself calls the program mostly obsolete. netstat stays useful when you follow older documentation or your hands still know its options.

List Listening Ports

sudo netstat -tulpn
  • -t: TCP sockets.
  • -u: UDP sockets.
  • -l: listening sockets only.
  • -p: PID and program name; needs root to show other users' processes.
  • -n: numeric addresses and ports, no name lookups.

Example output:

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   812/sshd
tcp        0      0 127.0.0.1:3306    0.0.0.0:*         LISTEN   990/mariadbd
tcp6       0      0 :::80             :::*              LISTEN   1022/apache2

0.0.0.0 means every IPv4 address, 127.0.0.1 only the server itself, and ::: every IPv6 address. A tcp6 line on ::: often accepts IPv4 connections as well. To find the program on one port, add a filter: sudo netstat -tulpn | grep :80.

All Connections

sudo netstat -tanp

-a shows listening and connected sockets together. The State column reads ESTABLISHED for open connections, TIME_WAIT for recently closed ones, and SYN_RECV for connections still being set up. Count the open TCP connections:

netstat -tan | grep -c ESTABLISHED

Routing Table

netstat -rn

The line with destination 0.0.0.0 and a gateway address shows the default route. See what is a default gateway.

Interface Statistics

netstat -i

Shows packets received and sent per interface, with error and drop counters in the RX-ERR, RX-DRP, TX-ERR, and TX-DRP columns. Rising errors point to a link problem; drops can mean the server is too busy to take packets in.

Protocol Statistics

netstat -s

Prints totals per protocol since boot: segments retransmitted, failed connection attempts, resets sent, and more. The totals alone say little; run it twice a few minutes apart and compare the numbers that grew.

netstat and Its Replacements

netstatReplacement
netstat -tulpnss -tulpn
netstat -tanpss -tanp
netstat -rnip route
netstat -iip -s link
netstat -snstat -az
netstat -gip maddr

ss takes most of the same letters, so ss -tulpn works as a direct swap. It is faster on servers with many connections and has filters by state and port that netstat lacks; see the ss command guide. The ip command is covered in ip command with examples.

Common Problems

  • A dash instead of a program name: run the command with sudo; without root, -p cannot see other users' processes.
  • Slow output: without -n, netstat looks up every address and port by name. Add it.
  • Watch it change: netstat -c repeats the output every second until Ctrl+C; with ss, run watch ss -s for a running summary.
  • A port in use, no program shown: the socket may belong to the kernel or a container; lsof and ss give a second view.
Was this answer helpful?
Loading...