跳到主要内容

Best Practices of tcpdump and netstat

· 阅读需 4 分钟

Introduction​

Network analysis is a critical skill for system administrators and network engineers. Tools like tcpdump, netstat, ping, traceroute, nmap, and iftop provide powerful capabilities for monitoring, diagnosing, and troubleshooting network issues. This article provides a comprehensive guide to the best practices of using these commands, covering various scenarios such as capturing and filtering network traffic, displaying network statistics, and diagnosing network issues.


tcpdump​

Basic Usage​

To start capturing network traffic, use the following command:

tcpdump

This command captures all traffic on the default network interface.

Filtering Traffic​

To filter traffic by protocol, use the -i option:

tcpdump -i eth0

To filter traffic by IP address, use the host keyword:

tcpdump host 192.168.1.1

To filter traffic by port, use the port keyword:

tcpdump port 80

Saving Captures​

To save captured traffic to a file, use the -w option:

tcpdump -w capture.pcap

Reading Captures​

To read a saved capture file, use the -r option:

tcpdump -r capture.pcap

netstat​

Basic Usage​

To display all active connections, use the following command:

netstat -a

Displaying Active Connections​

To display active TCP connections, use the -t option:

netstat -at

Displaying Listening Ports​

To display listening ports, use the -l option:

netstat -lt

Displaying Network Statistics​

To display network statistics, use the -s option:

netstat -s

Other Network Analysis Commands​

ping​

To check connectivity to a host, use the ping command:

ping google.com

traceroute​

To trace the route to a host, use the traceroute command:

traceroute google.com

nmap​

To scan for open ports on a host, use the nmap command:

nmap 192.168.1.1

iftop​

To monitor network traffic in real-time, use the iftop command:

iftop -i eth0

Advanced Scenarios​

Analyzing HTTP Traffic​

To capture and analyze HTTP traffic, use tcpdump with port filtering:

tcpdump -i eth0 port 80

Monitoring Real-Time Traffic​

To monitor real-time traffic on a specific interface, use iftop:

iftop -i eth0

Diagnosing Network Issues​

To diagnose network issues, use a combination of ping, traceroute, and tcpdump:

ping google.com
traceroute google.com
tcpdump -i eth0 host google.com

Conclusion​

Network analysis is a critical skill for maintaining and troubleshooting network infrastructure. By mastering the various options and scenarios covered in this article, you can effectively use tools like tcpdump, netstat, ping, traceroute, nmap, and iftop to monitor, diagnose, and troubleshoot network issues.


References​

  1. tcpdump Man Page
  2. netstat Man Page
  3. ping Man Page
  4. traceroute Man Page
  5. nmap Man Page
  6. iftop Man Page

By following these best practices, you can leverage these network analysis tools to their full potential and efficiently manage your network monitoring and troubleshooting tasks.