123 lines
3.4 KiB
Markdown
123 lines
3.4 KiB
Markdown
|
|
# Network Troubleshooting (netstat)
|
||
|
|
|
||
|
|
Netstat (Network Statistics) is a powerful command-line tool used for monitoring network connections, routing tables, interface statistics, and more. It's an essential utility for network administrators and system troubleshooters.
|
||
|
|
|
||
|
|
## 1. Basic Usage:
|
||
|
|
The basic syntax for netstat is:
|
||
|
|
```
|
||
|
|
netstat [options]
|
||
|
|
```
|
||
|
|
|
||
|
|
## 2. Common Options:
|
||
|
|
|
||
|
|
- a) -a: Show all sockets (listening and non-listening)
|
||
|
|
- b) -t: Display TCP connections
|
||
|
|
- c) -u: Display UDP connections
|
||
|
|
- d) -n: Show numerical addresses and port numbers instead of hostnames
|
||
|
|
- e) -p: Show the PID and name of the program to which each socket belongs
|
||
|
|
- f) -r: Display the routing table
|
||
|
|
- g) -i: Show network interface statistics
|
||
|
|
- h) -s: Display summary statistics for each protocol
|
||
|
|
|
||
|
|
## 3. Useful Combinations:
|
||
|
|
|
||
|
|
### a) View all TCP connections:
|
||
|
|
```
|
||
|
|
netstat -at
|
||
|
|
```
|
||
|
|
|
||
|
|
### b) View all UDP connections:
|
||
|
|
```
|
||
|
|
netstat -au
|
||
|
|
```
|
||
|
|
|
||
|
|
### c) View all listening ports:
|
||
|
|
```
|
||
|
|
netstat -l
|
||
|
|
```
|
||
|
|
|
||
|
|
### d) View all established connections:
|
||
|
|
```
|
||
|
|
netstat -tun
|
||
|
|
```
|
||
|
|
|
||
|
|
### e) View processes using network connections:
|
||
|
|
```
|
||
|
|
sudo netstat -tunapl
|
||
|
|
```
|
||
|
|
|
||
|
|
## 4. Interpreting Output:
|
||
|
|
|
||
|
|
The output of netstat typically includes the following columns:
|
||
|
|
|
||
|
|
- Proto: The protocol used (TCP, UDP, etc.)
|
||
|
|
- Recv-Q: The count of bytes not copied by the user program connected to this socket
|
||
|
|
- Send-Q: The count of bytes not acknowledged by the remote host
|
||
|
|
- Local Address: The local end of the socket
|
||
|
|
- Foreign Address: The remote end of the socket
|
||
|
|
- State: The state of the socket (e.g., ESTABLISHED, LISTEN, TIME_WAIT)
|
||
|
|
|
||
|
|
## 5. Advanced Usage:
|
||
|
|
|
||
|
|
### a) Continuous monitoring:
|
||
|
|
```
|
||
|
|
netstat -c
|
||
|
|
```
|
||
|
|
This will display the output every second.
|
||
|
|
|
||
|
|
### b) Display statistics for specific protocols:
|
||
|
|
```
|
||
|
|
netstat -s --tcp
|
||
|
|
netstat -s --udp
|
||
|
|
```
|
||
|
|
|
||
|
|
### c) Show network interface statistics:
|
||
|
|
```
|
||
|
|
netstat -i
|
||
|
|
```
|
||
|
|
|
||
|
|
### d) Display the routing table:
|
||
|
|
```
|
||
|
|
netstat -r
|
||
|
|
```
|
||
|
|
|
||
|
|
## 6. Troubleshooting Scenarios:
|
||
|
|
|
||
|
|
- a) Identifying port conflicts:
|
||
|
|
Use `netstat -tuln` to see which ports are in use and by which processes.
|
||
|
|
|
||
|
|
- b) Checking for listening services:
|
||
|
|
`netstat -tln` shows all TCP ports in LISTEN state.
|
||
|
|
|
||
|
|
- c) Investigating high network usage:
|
||
|
|
Use `netstat -i` to check interface statistics and identify interfaces with high traffic.
|
||
|
|
|
||
|
|
- d) Analyzing connection states:
|
||
|
|
`netstat -ant | awk '{print $6}' | sort | uniq -c | sort -n` gives a count of connections in each state.
|
||
|
|
|
||
|
|
- e) Finding which process is using a specific port:
|
||
|
|
```
|
||
|
|
sudo netstat -tulnp | grep :80
|
||
|
|
```
|
||
|
|
This example checks which process is using port 80.
|
||
|
|
|
||
|
|
## 7. Alternatives to netstat:
|
||
|
|
|
||
|
|
While netstat is powerful, some modern Linux distributions are moving towards newer tools:
|
||
|
|
|
||
|
|
- ss: A more performant replacement for netstat
|
||
|
|
- ip: A replacement for several networking tools, including some netstat functionality
|
||
|
|
|
||
|
|
## 8. Security Considerations:
|
||
|
|
|
||
|
|
- Running netstat with sudo privileges can reveal sensitive information about running processes and open ports.
|
||
|
|
- Be cautious when sharing netstat output, as it may contain information useful to potential attackers.
|
||
|
|
|
||
|
|
## 9. Tips for Effective Use:
|
||
|
|
|
||
|
|
- Combine netstat with other tools like grep, awk, and sort for more powerful analysis.
|
||
|
|
- Use the -c option for real-time monitoring during troubleshooting.
|
||
|
|
- Familiarize yourself with normal network patterns on your system to more easily spot anomalies.
|
||
|
|
|
||
|
|
Understanding and effectively using netstat can significantly enhance your ability to troubleshoot network issues, monitor system performance, and maintain network security on Linux systems.
|