# 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, following Linux network diagnostics practices documented in [**Vultr Docs**](https://docs.vultr.com/how-to-use-the-netstat-command-in-linux).

---

## Install `netstat`

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

**1\. Install** `net-tools`**:**

On Debian/Ubuntu:

```console
sudo apt install net-tools -y
```

On RHEL distributions:

```console
sudo dnf install net-tools -y
```

On Arch Linux:

```console
sudo pacman -Sy net-tools
```

On SUSE Linux:

```console
sudo zypper install net-tools -y
```

On Alpine Linux:

```console
sudo apk add net-tools
```

**2\. Verify the installation:**

```console
netstat --version
```

Output:

```plaintext
net-tools 2.10
...
```

## `netstat` Command Syntax

```console
netstat [OPTIONS]
```

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:**

```console
netstat
```

Output:

```plaintext
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
```

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

```console
sudo netstat -p
```

Output:

```plaintext
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
```

### View Open Ports

```console
sudo netstat -ltup
```

Output:

```plaintext
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
```

### Display the Routing Table

```console
netstat -r
```

Output:

```plaintext
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
...
```

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

```console
netstat -i
```

Output:

```plaintext
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
```

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

```console
sudo netstat -apn
```

Output:

```plaintext
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
```

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

### Display Active TCP Connections

```console
sudo netstat -aptn
```

Output:

```plaintext
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
```

### Display Active UDP Connections

```console
sudo netstat -apun
```

Output:

```plaintext
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
```

### Display Network Timer Data

```console
netstat -ano
```

Output:

```plaintext
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
...
```

The `Timer` column displays timer data for the socket.

### Display Listening Sockets

```console
netstat -tuln
```

Output:

```plaintext
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:*                          
```

## 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**](https://docs.vultr.com/how-to-use-the-netstat-command-in-linux).
