42 Network Statistics with Netstat

Опубликовано: 02 Ноябрь 2024
на канале: Engineering Academy Online
0

Network Statistics with `netstat`

`netstat` (network statistics) is a command-line tool in Linux and other Unix-like operating systems that provides a wealth of information about network connections, routing tables, interface statistics, and more. Although it's gradually being replaced by `ss` (socket statistics) in many distributions, it remains a useful utility for monitoring and troubleshooting network activity.

Key Features of `netstat`

1. **Display Active Connections**: Shows all active TCP and UDP connections.
2. **View Listening Ports**: Lists ports on which the system is listening for incoming connections.
3. **Show Routing Table**: Displays the current routing table.
4. **Interface Statistics**: Provides statistics for each network interface.

Commonly Used `netstat` Options

Here are some commonly used options with `netstat`:

`-a`: Displays all active connections and listening ports.
`-t`: Displays only TCP connections.
`-u`: Displays only UDP connections.
`-n`: Shows numerical addresses instead of resolving hostnames.
`-l`: Shows only listening sockets.
`-p`: Displays the process ID and name of the program that owns each socket.
`-r`: Displays the kernel routing table.
`-i`: Displays a list of network interfaces and their statistics.

Examples of Using `netstat`

#### *1. Display All Active Connections*

```bash
netstat -a
```

This command will show all active TCP and UDP connections, along with the local and remote addresses and the state of each connection (e.g., ESTABLISHED, LISTENING).

#### *2. Show Listening Ports*

```bash
netstat -l
```

This command lists all listening ports, indicating which services are waiting for incoming connections.

#### *3. Display TCP Connections Only*

```bash
netstat -t
```

This command filters the output to show only TCP connections.

#### *4. Show UDP Connections Only*

```bash
netstat -u
```

This command shows only UDP connections.

#### *5. Show Numerical Addresses*

```bash
netstat -n
```

Using this option prevents `netstat` from resolving hostnames, displaying IP addresses instead. This can speed up output when DNS resolution is slow.

#### *6. Show Process IDs and Program Names*

```bash
netstat -p
```

This command displays the process ID and name for each connection. This is useful for identifying which applications are using specific network connections.

#### *7. Display the Routing Table*

```bash
netstat -r
```

This command shows the kernel routing table, including destination networks, gateways, and interface metrics.

#### *8. Display Network Interface Statistics*

```bash
netstat -i
```

This command provides statistics about each network interface, such as the number of packets received and transmitted, errors, and collisions.

Conclusion

`netstat` is a versatile tool for monitoring and diagnosing network activity in Linux. Despite the rise of `ss`, it remains widely used for its simplicity and effectiveness in providing crucial network statistics. If you have specific questions about using `netstat` or need help with a particular scenario, feel free to ask!