Capturing Packets with Tcpdump
Tcpdump is a powerful network debugging tool that can be used for intercepting and displaying packets on a network interface. An important feature of tcpdump is a filter that allows you to display only the packets you want to see.
In this example we are using Ubuntu 8.10; however, the installation steps are similar for other Linux distributions. The following command will install tcpdump under Ubuntu:
sudo apt-get install tcpdump
sudo tcpdump [options] [filter expression]
By default, tcpdump will capture packets on eth0. We can specify a different interface using the '-i' command line flag. This command will capture all packets on the eth1 interface:
sudo tcpdump -i eth1
In the following example we will listen to all UDP connections:
sudo tcpdump udp
Use this command to capture packets for a specific port:
sudo tcpdump port 80
Our command is returning all packets which have port 80 as their destination or source port.
Now let’s be more specific and capture only packets with destination port 80. If you have a web server on your cloud, then you can use the command below to see incoming packets.
sudo tcpdump dst port 80
You can also capture packets for a specific host. This command will catch packets coming only from IP 18.104.22.168:
sudo tcpdump src host 22.214.171.124
Tcpdump can take logical arguments such as ‘and,’ as well as ‘or.’ You can use logical statements in a tcpdump command. For example, to catch all the SSH packets going from an SSH server to a client with IP 126.96.36.199:
sudo tcpdump "src port 22" and "dst host 188.8.131.52"
Raw packets can be conveniently saved to a file using the '-w' option:
tcpdump host 184.108.40.206 -w /home/users/demo/demo.dump
Let's read the saved file:
tcpdump -r /home/users/demo/demo.dump
Tcpdump is a powerful packet sniffer and a common tool used by system administrators to solve network problems and investigate traffic. It can be used with Boolean expressions to capture only those packets you're interested.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER