DEV Community

Cover image for Using the Netstat Command in Linux
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Using the Netstat Command in Linux

netstat, short for network statistics, is a command-line tool for diagnosing network issues and gathering network statistics. It displays active connections and listening sockets for TCP, UDP, and UNIX domain sockets, lists associated ports, shows basic network interface statistics, and prints the kernel's routing table. This guide covers installing netstat and using it to monitor connections, identify open ports, and troubleshoot network issues. By the end, you'll be able to read netstat output and use its most common options to monitor and diagnose networking on a Linux system.


Install netstat

The netstat command is part of the net-tools package.

1. Install net-tools:

On Debian/Ubuntu:

$ sudo apt install net-tools -y
Enter fullscreen mode Exit fullscreen mode

On RHEL distributions:

$ sudo dnf install net-tools -y
Enter fullscreen mode Exit fullscreen mode

On Arch Linux:

$ sudo pacman -Sy net-tools
Enter fullscreen mode Exit fullscreen mode

On SUSE Linux:

$ sudo zypper install net-tools -y
Enter fullscreen mode Exit fullscreen mode

On Alpine Linux:

$ sudo apk add net-tools
Enter fullscreen mode Exit fullscreen mode

2. Verify the installation:

$ netstat --version
Enter fullscreen mode Exit fullscreen mode

Output:

net-tools 2.10
...
Enter fullscreen mode Exit fullscreen mode

netstat Command Syntax

$ netstat [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

The options modify the command's behavior. Without any options, netstat displays a list of open sockets.

netstat Command Output Anatomy

1. View all active connections on your system:

$ netstat
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address        State
tcp        0      0 hostname:49922          example.com:https      ESTABLISHED
tcp        0      0 hostname:44568          cdn-1-0-0-127:https    ESTABLISHED
...
...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         STREAM     CONNECTED     7931     /run/user/1050/bus
unix  3      [ ]         STREAM     CONNECTED     7835     /run/dbus/system_bus_socket
Enter fullscreen mode Exit fullscreen mode

The output has two sections:

  • Active Internet Connections: TCP and UDP connections between your localhost hostname and remote hosts such as example.com
  • Active UNIX domain sockets: Local UNIX sockets that are open

Column reference:

  • Proto: The active network protocol — tcp, udp, or unix
  • Recv-Q: Bytes of data received by the kernel but not yet read by the application
  • Send-Q: Bytes queued by the application to send that haven't been acknowledged by the target
  • Local Address: The system hostname, IP address, and port
  • Foreign Address: The reverse lookup of the remote host
  • State: The connection state (ESTABLISHED denotes an established connection)
  • RefCnt: How many users or connections are connected to the socket
  • Flags: Flags related to UNIX sockets
  • Type: The socket type, such as STREAM, DGRAM, SEQPACKET, or RAW
  • I-Node: The inode number associated with the unix socket
  • Path: The filesystem path associated with the unix socket

netstat without any options shows only non-listening sockets.

netstat Options

The most commonly used options:

  • --listening or -l: Displays listening sockets
  • --all or -a: Displays both established and non-established connections
  • --tcp or -t: Displays TCP sockets
  • --udp or -u: Displays UDP sockets
  • --numeric or -n: Displays port numbers and IP addresses instead of names
  • --program or -p: Displays the PID/program name associated with each socket (requires sudo for systemwide processes)

netstat doesn't list listening sockets without the -l or -a option.

Display Open Connections With Process IDs

$ sudo netstat -p
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address      State       PID/Program name
tcp        0      0 hostname:55604          2.0.0.127.in-a:https ESTABLISHED 2320/firefox
tcp        0      0 hostname:40128          3.0.0.127.i:https    ESTABLISHED 33638/app --st
...
...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  3      [ ]         STREAM     CONNECTED     12358    750/pipewire         /run/user/1050/pipewire-0
unix  3      [ ]         STREAM     CONNECTED     31580    3898/speech-dispatc  /run/user/1050/speech-dispatcher/speechd.sock
Enter fullscreen mode Exit fullscreen mode

View Open Ports

$ sudo netstat -ltup
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 localhost:mshvlm        0.0.0.0:*               LISTEN      763/mpd
udp        0      0 0.0.0.0:35069           0.0.0.0:*                           1451/firefox
Enter fullscreen mode Exit fullscreen mode

Display the Routing Table

$ netstat -r
Enter fullscreen mode Exit fullscreen mode

Output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         _gateway        0.0.0.0         UG        0 0          0 enp1s0
192.168.10.2.ch _gateway        255.255.255.255 UGH       0 0          0 enp1s0
192.168.10.1    0.0.0.0         255.255.254.0   U         0 0          0 enp1s0
_gateway        0.0.0.0         255.255.255.255 UH        0 0          0 enp1s0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlp4s0
...
Enter fullscreen mode Exit fullscreen mode

Column reference:

  • Destination: Destination IP or hostname
  • Gateway: The next hop on the route
  • Genmask: The network mask of the route
  • Flags: U (route is up), H (destination is a specific host), G (destination is a gateway)
  • MSS: Maximum segment size — the maximum payload size the socket receives
  • Window: The TCP window size
  • irtt: Initial round trip time of packets for a connection
  • Iface: The network interface of the route

In the example output above, traffic to 0.0.0.0 routes through the wlp4s0 interface, with a default gateway of 192.168.1.1 reached via wlp4s0.

Display Network Interface Statistics

$ netstat -i
Enter fullscreen mode Exit fullscreen mode

Output:

Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo              65536     6111      0      0 0          6111      0      0      0 LRU
wlp4s0           1500  1164542      0      0 0        822802      0     38      0 BMRU
Enter fullscreen mode Exit fullscreen mode

Column reference:

  • Iface: The network interface name
  • MTU: The maximum transmission unit in bytes
  • RX-OK: Count of successfully received packets
  • RX-ERR: Count of error packets received
  • RX-DRP: Count of packets dropped by the interface
  • RX-OVR: Count of data packets the interface couldn't handle
  • TX-OK: Size of successfully transmitted packets
  • TX-ERR: Count of error transmitted packets
  • TX-DRP: Count of packets dropped by the transmit queue
  • TX-OVR: Similar to RX-OVR, but for transmitted data
  • Flg: B (broadcast address set), L (loopback interface), M (promiscuous mode), R (running interface), U (interface is up)

View All Active Network Connections

$ sudo netstat -apn
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:6600          0.0.0.0:*               LISTEN      724/mpd
tcp        0      0 192.168.1.6:49928       172.6.0.4:443           ESTABLISHED 1148/firefox
...
...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  3      [ ]         STREAM     CONNECTED     13452    893/dbus-broker      /run/user/1050/at-spi/bus_0
unix  2      [ ACC ]     STREAM     LISTENING     10355    724/mpd              /home/ndlr/.config/mpd/socket
Enter fullscreen mode Exit fullscreen mode

Where -a shows all connections, -p shows the PID and program name, and -n shows numeric output.

Display Active TCP Connections

$ sudo netstat -aptn
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:6600          0.0.0.0:*               LISTEN      724/mpd
tcp        0      0 192.168.1.6:49928       172.6.0.4:443           ESTABLISHED 1148/firefox
Enter fullscreen mode Exit fullscreen mode

Display Active UDP Connections

$ sudo netstat -apun
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp        0      0 0.0.0.0:47697           0.0.0.0:*                           1148/firefox
udp        0      0 192.168.1.6:68          192.168.1.1:67          ESTABLISHED 601/NetworkManager
Enter fullscreen mode Exit fullscreen mode

Display Network Timer Data

$ netstat -ano
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 127.0.0.54:53           0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      off (0.00/0/0)
...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         STREAM     CONNECTED     8613     /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     8929     /run/dbus/system_bus_socket
...
Enter fullscreen mode Exit fullscreen mode

The Timer column displays timer data for the socket.

Display Listening Sockets

$ netstat -tuln
Enter fullscreen mode Exit fullscreen mode

Output:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.54:53           0.0.0.0:*               LISTEN        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
udp        0      0 127.0.0.54:53           0.0.0.0:*                          
udp        0      0 127.0.0.53:53           0.0.0.0:*                          
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Combine netstat with grep to filter output for a specific port or process
  • Compare netstat output with the newer ss command, which is faster on systems with many connections
  • Set up a monitoring script that polls netstat -tuln to alert on unexpected open ports
  • Run man 8 netstat for the full list of options and flags

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (1)

Collapse
 
dheerajramasahayam profile image
Dheeraj Ramasahayam •

Good reference — the two flags I reach for most on a break-fix ticket are -i and -tuln. When a host reports flapping or drops, RX-ERR and RX-DRP climbing on netstat -i is usually what tells me it's a physical problem and I should go check the optic and the cable before touching anything else. And netstat -tuln is my first check on a 'service is down' page — if nothing is listening on the port, there's no point escalating it as a network problem.