Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
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.

Bash Count connections per state

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

Terminal
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 -tulpn is ss -tulpn; netstat -rn is ip route; netstat -i is ip -s link; netstat -s is ss -s (and nstat for counters).
  • Always add -n. Without it, netstat tries 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 -p hides processes belonging to others; run it with sudo.
  • Slower on large servers because it parses /proc/net/* text files, whereas ss uses a netlink interface.
  • Installing it: the package is net-tools on Debian, Ubuntu and Red Hat families.
  • Different on BSD and macOS. Flags such as -p mean "protocol" there, and -l is not available; scripts that must run on both need care.
  • Reading the state column: LISTEN is a waiting server, ESTABLISHED an active connection, TIME_WAIT a recently closed one waiting out stray packets, CLOSE_WAIT a peer closed but the local program has not.