tcpdump for Dummies

Table of contents

IntroductionBACK TO TOC

In this article  I would like to talk about one of the most useful tools in my networking toolbox and that is tcpdump. Unfortunately mastering this tool completely is not an easy task. Yet stuff you do the most is relatively simple and may become a good springboard when diving into more complex topics.

tcpdump usesBACK TO TOC

tcpdump is a packet sniffer. It is able to capture traffic that passes through a machine. It operates on a packet level, meaning that it captures the actual packets that fly in and out of your computer. It can save the packets into a file. You can save whole packets or only the headers. Later you can “play” recorded file and apply different filters on the packets, telling tcpdump to ignore packets that you are not interested to see.

Under the hood, tcpdump understands protocols and host names. It will do all in its power to see what host sent each packet and will tell you its name instead of the IP address.

It is exceptionally useful tool for debugging what might have caused certain networking related problem. It is an excellent tool to learn new things.

InvocationBACK TO TOC

Invoking tcpdump is easy. First thing that you have to remember is that you should either be logged in as root or  be a sudoer on the computer – sudoer is someone who is entitled to gain administrator rights on computer for short period of time using sudo command.

Running tcpdump without any arguments makes it capture packets on first network interface (excluding lo) and print short description of each packet to output. This may cause a bit of a headache in case you are using network to connect to the machine. If you are connected with SSH or telnet (rlogin?), running tcpdump will produce a line of text for each incoming or outgoing packet. This line of text will cause SSH daemon to send a packet with this line, thus causing tcpdump to produce another line of text. And this will not stop until you do something about it.

Simple filteringBACK TO TOC

So first thing that we will learn about tcpdump is how to filter out SSH and telnet packets. We will study the basics of tcpdump filtering later in this guide, but for now just remember this syntax.

# tcpdump not port 22

“not port 22″ is a filter specification that tells tcpdump to filter out packets with IP source or destination port 22. As you know port 22 is SSH port. Basically, when you tell tcpdump something like this, it will make tcpdump ignore all SSH packets – exactly what we needed.

Telnet on the other hand, uses port 23. So if you are connecting via telnet, you can filter that out with:

# tcpdump not port 23

Clear and simple!

Reading tcpdump‘s outputBACK TO TOC

By default tcpdump produces one line of text per every packet it intercepts. Each line starts with a time stamp. It tells you very precise time when packet arrived.

Next comes protocol name. Unfortunately, tcpdump understands very limited number of protocols. It won’t tell you the difference between packets belonging to HTTP and for instance FTP stream. Instead, it will mark such packets as IP packets. It does have some limited understanding of TCP. For instance it identifies TCP synchronization packets such as SYN, ACK, FIN and others. This information printed after source and destination IP addresses (if it IP packet).

Source and destination addresses follow protocol name. For IP packets, these are IP addresses. For other protocols, tcpdump does not print any identifiers unless explicitly asked to do so (see -e command line switch below).

Finally, tcpdump prints some information about the packet. For instance, it prints TCP sequence numbers, flags, ARP/ICMP commands, etc.

Here’s an example of typical tcpdump output.

17:50:03.089893 IP 69.61.72.101.www > 212.150.66.73.48777: P 1366488174:1366488582
(408) ack 2337505545 win 7240 <nop,nop,timestamp 1491222906 477679143>

This packet is part of HTTP data stream. You can see meaning of each and every field in the packet description in tcpdump’s manual page.

Here’s another example

17:50:00.718266 arp who-has 69.61.72.185 tell 69.61.72.1

This is ARP packet. It’s slightly more self explanatory than TCP packets. Again, to see exact meaning of each field in the packet description see tcpdump’s manual page.

Invocation continuedBACK TO TOC

Now, when we know how to invoke tcpdump even when connecting to the computer over some net, let’s see what command line switches are available for us.

Choosing an interfaceBACK TO TOC

We’ll start with a simple one. How to dump packets that arrived and sent through a certain network interface. -i command line argument does exactly this.

# tcpdump -i eth1

Will cause tcpdump to capture packets from network interface eth1. Or, considering our SSH/telnet experience:

# tcpdump -i eth1 not port 22

Finally, you can specify any as interface name, to tell tcpdump to listen to all interfaces.

# tcpdump -i any not port 22

Turning off name resolutionBACK TO TOC

As we debug networking issues, we may encounter a problem with how tcpdump works out of the box. The problem is that it tries to resolve every single IP address that it meets. I.e. when it sees an IP packet it asks DNS server for names of the computers behind IP address. It works flawlessly most of the time. However, there are two problems.

First, it slows down packet interception. It’s not a big deal when there are only few packets, but when there are thousands and tens of thousands it introduces a delay into the process. Amount of delay can be different, depending on the traffic.

Another, much more serious problem occurs when there is no DNS server around or when DNS server is not working properly. If this is the case, tcpdump spends few seconds trying to figure out two hostnames for each IP packet. This means virtually stopping intercepting the traffic.

Luckily there is a way around. There is an option that causes tcpdump to stop detecting hostnames and that is -n.

# tcpdump -n

And here are few variations of how you can use this option in conjunction with options that we have learned already.

# tcpdump -n -i eth1
# tcpdump -ni eth1 not port 22

Limiting number of packets to interceptBACK TO TOC

Here are few more useful options. Sometimes amount of traffic that goes in and out of your computer is very high, while all you want to see is just few packets. Often you want to see who sends you the traffic, but when you try to capture anything with tcpdump it dumps so many packets that you cannot understand anything. This is the case when -c command line switch becomes handy.

It tells tcpdump to limit number of packets it intercepts. You specify number of packets you want to see. tcpdump will capture that number of packets and exit. This is how you use it.

# tcpdump -c 10

Or with options that we’ve learned before.

# tcpdump -ni eth1 -c 10 not port 22

This will limit number of packets that tcpdump will receive to 10. Once received 10 packets, tcpdump will exit.

Saving captured dataBACK TO TOC

One of the most useful tcpdump features allows capturing incoming and outgoing packets into a file and then playing this file back. By the way, you can play this file not only with tcpdump, but also with WireShark (former Ethereal), the graphical packet analyzer.

You can do this with -w command line switch. It should be followed by the name of the file that will contain the packets. Like this:

# tcpdump -w file.cap

Or adding options that we’ve already seen

# tcpdump -ni eth1 -w file.cap not port 22

Changing packet size in the capture fileBACK TO TOC

By default, when capturing packets into a file, it will save only 68 bytes of the data from each packet. Rest of the information will be thrown away.

One of the things I do often when capturing traffic into a file, is to change the saved packet size. The thing is that disk space that is required to save the those few bytes is very cheap and available most of the time. Spending few spare megabytes of your disk space on capture isn’t too painful. On the other hand, loosing valuable portion of packets might be very critical.

So, what I usually do when capturing into a file is running tcpdump with -s command line switch. It tells tcpdump how many bytes for each packet to save. Specifying 0 as a packet’s snapshot length tells tcpdump to save whole packet. Here how it works:

# tcpdump -w file.cap -s 0

And with conjunction with options that we already saw:

# tcpdump -ni eth1 -w file.cap -s 0 -c 1000 not port 22

Obviously you can save as much data as you want. Specifying 1000 bytes will do the job for you. Just keep in mind that there are so called jumbo frames those size can be as big as 8Kb.

Reading from capture fileBACK TO TOC

Now, when we have captured some traffic into a file, we would like to play it back. -r command like switch tells tcpdump that it should read the data from a file, instead of capturing packets from interfaces. This is how it works.

# tcpdump -r file.cap

With capture file, we can easily analyze the packets and understand what’s inside. tcpdump introduces several options that will help us with this task. Lets see few of them.

Looking into packetsBACK TO TOC

There are several options that allow one to see more information about the packet. There is a problem though. tcpdump in general isn’t giving you too much information about packets. It doesn’t understand different protocols.

If you want to see packet’s content, it is better to use tools like Wireshark. It does understand protocols, analyzes them and allows you to see different fields, not only in TCP header, but in layer 7 protocols headers.

tcpdump is a command line tool and as most of the command line tools, its ability to present information is quiet limited. Yet, it still has few options that control the way packets presented.

Seeing Ethernet header for each packetBACK TO TOC

-e command line switch, causes tcpdump to present Ethernet (link level protocol) header for each printed packet. Lets see an example.

# tcpdump -e -n not port 22

Controlling time stampBACK TO TOC

There are four command line switches that control the way how tcpdump prints time stamp. First, there is -t option. It makes tcpdump not to print time stamps. Next comes -tt. It causes tcpdump to print time stamp as number of seconds since Jan. 1st 1970 and a fraction of a second. -ttt prints the delta between this line and a previous one. Finally, -tttt causes tcpdump to print time stamp in it’s regular format preceeded by date.

Controlling verbosityBACK TO TOC

-v causes tcpdump to print more information about each packet. With -vv tcpdump prints even more information. As you could guess, -vvv produces even more information. Finally -vvvv will produce an error message telling you there is no such option :D

Printing content of the packetBACK TO TOC

-x command line switch will make tcpdump to print each packet in hexadecimal format. Number of bytes that will be printed remains somewhat a mystery. As is, it will print first 82 bytes of the packet, excluding ethernet header. You can control number of bytes printed using -s command line switch.

In case you want to see ethernet header as well, use -xx. It will cause tcpdump to print extra 14 bytes for ethernet header.

Similarily -X and -XX will print contents of packet in hexadecimal and ASCII formats. The later will cause tcpdump to include ethernet header into printout.

Packet filteringBACK TO TOC

We already saw a simple filter. It causes tcpdump to ignore SSH packets, allowing us to run tcpdump from remote. Now lets try to understand the language that tcpdump uses to evaluate filter expressions.

Packet matchingBACK TO TOC

We should understand that tcpdump applies our filter on every single incoming and outgoing packet. If packet matches the filter, tcpdump aknownledges the packet and depending on command line switches either saves it to file or dumps it to the screen. Otherwise, tcpdump will ignore the packet and account it only when telling how many packets received, dropped and filtered out when it exits.

To demostrate this, lets go back to not port 22 expression. tcpdump ignores packets that either sourced or destined to port 22. When such packet arrives, tcpdump applies filter on it and since the result is false, it will drop the packet.

More qualifiersBACK TO TOC

So, from what we’ve seen so far, we can conclude that tcpdump understands a word port and understands expression negation with not. Actually, negating an expression is part of complex expressions syntax and we will talk about complex expressions a little later. In the meantime, lets see few more packet qualifiers that we can use in tcpdump expressions.

We’ve seen that port qualifier specifies either source or destination port number. In case we want to specify only the source port or only the destination port we can use src port or dst port. For instance, using following expression we can see all outgoing HTTP packets.

# tcpdump -n dst port 80

We can also specify ranges of ports. portrange, src portrange and dst portrange qualifiers do exactly this. For instance, lets see a command that captures all telnet and SSH packets.

# tcpdump -n portrange 22-23

Specifying addressesBACK TO TOC

Using dst host, src host and host qualifiers you can specify source, destination or any of them IP addresses. For example

# tcpdump src host alexandersandler.net

Will print all packets originating from alexandersandler.net computer.

You can also specify Ethernet addresses. You do that with ether src, ether dst and ether host qualifiers. Each should be followed by MAC address of either source, destination or source or destination machines.

You can specify networks as well. The net, src net and dst net qualifiers do exactly this. Their syntax however slighly more complex than those of a single host. This is due to a netmask that has to be specified.

You can use two basic forms of network specifications. One using netmask and the other so called CIDR notation. Here are few examples.

# tcpdump src net 67.207.148.0 mask 255.255.255.0

Or same command using CIDR notation.

# tcpdump src net 67.207.148.0/24

Note the word mask that does the job of specifying the network in first example. Second example is much shorter.

Other qualifiersBACK TO TOC

There are several useful qualifiers that don’t fall under any of the categories I already covered.

For instance, you can specify that you are interested in packets with specific length. length qualifier does this. less and greater qualifiers tell tcpdump that you are interested in packets whose length is less or greater than value you specified.

Here’s an example that demonstrates these qualifiers in use.

# tcpdump -ni eth1 greater 1000

Will capture only packets whose size is greater than 1000 bytes.

Complex filter expressionsBACK TO TOC

As we already saw we can build more complex filter expressions using tcpdump filters language. Actually, tcpdump allows exceptionally complex filtering expressions.

We’ve seen not port 22 expression. Applying this expression on certain packet will produce logical true for packets that are not sourced or destined to port 22. Or in two words, not negates the expression.

In addition to expression negation, we can build more complex expressions combining two smaller expression into one large using and and or keywords. In addition, you can use brackets to group several expressions together.

For example, lets see a tcpdump filter that causes tcpdump to capture packets larger then 100 bytes originating from google.com or from microsoft.com.

# tcpdump -XX greater 100 and \(src host google.com or src host microsoft.com\)

and and or keywords in tcpdump filter language have same precedence and evaluated left to right. This means that without brackets, tcpdump could have captured packets from microsoft.com disregarding packet size. With brackets, tcpdump first makes sure that all packets are greater than 100 bytes and only then checks their origin.

Note the backslash symbol (“\”) before brackets. We have to place them before brackets because of shell. Unix shell has special understanding of what brackets used for. Hence we have to tell shell to leave these particular brackets alone and pass them as they are to tcpdump. Backslash characters do exactly this.

Talking about precedence, we have to keep in mind that in tcpdump’s filter expression language not has higher precedence than and and or. tcpdump’s manual page has very nice example and emphasizes the meaning of this.

not host vs and host ace

and

not (host vs or host ace)

are two different expressions. Because not has higher precedence over and and or, filter from the first example will capture packets not to/from vs, but to/from ace. Second filter example on the other hand will capture packets that are not to/from vs and to/from ace. I.e. first will capture packet from ace to some other host (but not to vs). Yet second example won’t capture this packet.

Repeating qualifiersBACK TO TOC

To conclude this article, I would like to tell you one more thing that may become handy when writing complex tcpdump filter expressions.

Take a look at the following example.

# tcpdump -XX greater 100 and \(src host google.com or microsoft.com\)

We already saw this example, with one little exception. In previous example we had a src host qualifier before microsoft.com and now its gone. The thing is that if we want to use same qualifier two times in a row, we don’t have to specify it twice. Instead we can just write qualifier’s parameter and tcpdump will know what to do.

This makes tcpdump filter expression language much easier to understand and much more readable.

AfterwordBACK TO TOC

I hope you found this article useful. In case you have questions, suggestions or you would like to share your appreciation to the author ;-) , don’t hesitate to mail me to alexander.sandler@gmail.com

Did you know that you can receive periodical updates with the latest articles that I write right into your email box? Alternatively, you subscribe to the RSS feed!

Want to know how? Check out
Subscribe page

152 Comments

  1. Steven Wright says:

    Thanks for an excellent explanation.

  2. Wael says:

    Thanks you, very neatly done, your explanation is flawless.
    I can tcpdump my cows very easily now

    Thanks again

  3. Eddie J says:

    so the first part of packet output is the timestamp, and the second part is the protcol name. So in this output where is the protocol? : view sourceprint?
    17:50:03.089893 IP 69.61.72.101.www > 212.150.66.73.48777: P 1366488174:1366488582

    (408) ack 2337505545 win 7240

    ..also can you break down exactly what this output is saying??

  4. Khan says:

    Can you tell me where exactly is the tcpdump manual page? I dont find it in here??

  5. Kondrat says:

    very useful article.
    I use a textmode sniffer for pure http traffic monitoring

    justniffer

    Bye

  6. @Eddie J
    As I’ve written you can see what each field means in the tcpdump’s manual page. Moreover, I suggest that you do it because tcpdump’s output changes from version to version. This article is over two years old now and the format has changed. Information that you get from tcpdump’s manual page is most up-to-date information you can get and best matches tcpdump that is installed on your machine.

    In this particular case, first field 17:50:03.089893 stands for time when packet was received. Next comes the layer 3 (network) protocol – IP. Next, there’s source IP address and destination IP address. In both source and destination addresses, fifth number stands for port number. tcpdump tries to give name to port number where it can. So www, means port 80. It probably takes names from /etc/services on your machine.
    Destination address followed by packet flags. P stands for TCP’s push flag. Finally you have TCP’s sequence numbers.

  7. @Khan
    Oh, you just enter ‘man tcpdump’ on your Linux machine. Alternatively you can google for ‘man tcpdump’ and enter first result.

  8. Ye Myo Thu says:

    Thanks you so much, Very Very useful article for me

  9. mohammed says:

    how do you read the central portion of captured file, please some one tell me the first 20 bytes of the following captured ip packet

    0×0000: 45c0 00c0 ff1c 0000 4011 d746 c800 090a E…….@..F….
    0×0010: c800 09ff 0208 0208 00ac f94e 0202 0000 ………..N….
    0×0020: 0002 0000 c800 0500 ffff ff00 0000 0000 …………….
    0×0030: 0000 0001 0002 0000 c800 0700 ffff ff00 …………….
    0×0040: 0000 0000 0000 0001 0002 0000 c800 0300 …………….
    0×0050: ffff ..

  10. Edward says:

    Hello, i just wanna ask how i can get traffic on port 80 only, like received and transmitted data in bytes.

    Thank you, Ed.

  11. Rishikesh says:

    Hello, I have downloaded tcpdump data set from the MIT Lincolon lab. I have analysed the most of the packets. But some packets looks as given below. I have searched a lot but I have got no information about how to analyse these data. I think it includes ethernet address. Thanks in advance….

    17:34:49.055471 08:00:09:61:aa:c9 (oui Hewlett-Packard) NetBeui > 08:00:09:61:aa:c9 (oui Hewlett-Packard) Null Unnumbered, test, Flags [Command, Poll], length 40
    17:34:50.055662 08:00:09:61:aa:c9 (oui Hewlett-Packard) NetBeui > 08:00:09:61:aa:c9 (oui Hewlett-Packard) Null Unnumbered, test, Flags [Command, Poll], length 40
    17:34:51.054990 08:00:09:61:aa:c9 (oui Hewlett-Packard) NetBeui > 08:00:09:61:aa:c9 (oui Hewlett-Packard) Null Unnumbered, test, Flags [Command, Poll], length 40
    17:34:52.055437 08:00:09:61:aa:c9 (oui Hewlett-Packard) NetBeui > 08:00:09:61:aa:c9 (oui Hewlett-Packard) Null Unnumbered, test, Flags [Command, Poll], length 40

  12. richa says:

    I want to generate a random-number generating program using only the
    traffic patterns captured by tcpdump.

  13. @Rishikesh
    Its NetBeui. I am not sure about the details, but this protocol used in Windows to communicate various Windows network components. tcpdump won’t show you the contents of these packets. Use wireshark to analyze them.

  14. Tesse M says:

    I am trying to analyze a file containing packets captured by tcpdump. I first want to categorize the packets into flows using the 5-tuple property. Then I need to get the size of each packet in the flow and the inter-arrival time between two consecutive packets within the flow. Can you suggest any tool or script?

    Thank you in advance

  15. dave says:

    Hi,
    very nice article!thanks.
    I have a question: if you capture a packet with tcpdump, are you able to play it back on the net, maybe changing some of its pieces? (ie. changing the sender IP address with a different one)

    Thanks,

  16. odoggz says:

    Excellent writeup!!

  17. Rakesh says:

    Good artical & very helpfull.
    i was looking for a option in tcpdump to capture any packetcs if it contain’s a specific word or string. how can that be given for eg.
    if a packet contains some word like “love / hate badword.. so on ” then on\y it should capture

  18. jamuna says:

    Is there a way to get the size of a packet on the network with tcpdump (or other program)? thanks in advance…

  19. Did you know your IP address is exposed every time you visit a website?

  20. Dan Sichel says:

    Thank you for taking the time to present useful material in a succinct but accessible manner. Your lucidity and clarity are rare and much appreciated. Again, thank you.

    Dan Sichel

  21. Claude Vanderm says:

    Really interesting article. Thank you!

  22. achyutbaruah says:

    Is there any way to capture only the timestamp excluding other informaation using Tcpdump?????

  23. بررسی نقاط ضعف امنیتی نرم افزار فیلترشکن (رستم) | yoou says:

    [...] “tcpdump is a packet sniffer. It is able to capture traffic that passes through a machine. It operates on a packet level, meaning that it captures the actual packets that fly in and out of your computer. It can save the packets into a file. You can save whole packets or only the headers. Later you can “play” recorded file and apply different filters on the packets, telling tcpdump to ignore packets that you are not interested to see.” Source:  http://www.alexonlinux.com/tcpdump-for-dummies [...]

  24. […] recommend checking that SO question and becoming familiar with Wireshark or TCPDump, as these tools will help you figure out what’s happening in the server <—> client […]

  25. When I originally commented I clicked the “Notify me when new comments are added” checkbox
    and now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove people from that service?
    Many thanks!

  26. ann says:

    hi, is it possible to use tcpdump in wireless network , I wanted to measure bandwidth, RRT, jitter, delay measures. Is it possible to calculate those information using TCPDUMP

    • I don’t think it is possible to measure these parameters of the network with tcpdump. Tcpdump captures packets. So it assumes to work at data link layer and above. What you are trying to do is on top of physical layer.

      Tcpdump might tell you a little about delays though – you can have accurate time-stamp of every packet’s arrival so you can measure things.

  27. Kristopher says:

    Marketing is the first skill because it doesn’t matter if
    you know how to run a company or not, if you know how
    to market the business, you can get customers.
    Even changing a tire can be dangerous if you are not focused.
    “The whole purpose of this portion of the ordinance is to protect consumers from predatory towing – where the towing company driver decides which vehicle are parked illegally, etc.

  28. shoes says:

    shoes

    tcpdump for Dummies – Alex on Linux

Leave a Reply


one + = 5