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

156 Comments

  1. Jeff Winner says:

    Thanks, much easier than going through all of those man pages!

  2. Alexander Sandler says:

    Jeff, I am glad you found it helpful. Please come visit my web-site again :D

  3. How about some nice layer 2 examples.

    Thanks
    Scott.

  4. Raseel says:

    Thanks ! This was a good quick-start guide

  5. Alexander Sandler says:

    Thanks Raseel. Feel free to visit my web-site anytime :D

  6. Alexander,

    Great article, I twittered it immediately. I know a lot of people who can use stuff like this and other stuff on your site.

    Will be checking regularly to see what you come up with next.

    Cheers…Kishore

  7. Alexander Sandler says:

    Thanks a lot! You are most welcome :D

  8. Sitaram says:

    tcpdump without options listens on only one interface, not all of them.

    Typically it chooses the “first” interface.

  9. Alexander Sandler says:

    Thanks for the note. I’ll fix the article.

  10. Vatsala says:

    Very informative aarticle and a really nice website too. I bookmarked it on delicious the moment i saw it. I also bookmarked your resume, its really gives me great ideas to plan my future! I will be a regular visitor to your site!

  11. nitin says:

    Thanks ! nice article.

    Can you plz tell how to use it for detecting Dos /DDoS attacks.

  12. Alexander Sandler says:

    @Vatsala: Thanks for visiting and for a warm comment. I’ll be happy to serve you in the future :D

  13. Grzegorz Witkowski says:

    Very interesting and useful.
    Thank you.

  14. Alexander Sandler says:

    Grzegorz, Thank you visiting. Please visit again.

  15. melzer says:

    Nice article, a good quick start guide

  16. Alexander Sandler says:

    Melzer, thanks for visiting. Please come again :D

  17. […] reading his blog couple of weeks ago I went down and did something I never did before. I posted my most popular article on the most popular social bookmarking services. Guess what. Nearly 1500 visitors has visited […]

  18. aeonmike says:

    very nice howto!!

  19. @aeonmike: Thanks a lot for the comment and for visiting. Please come again!

  20. Jeffrey says:

    Hi there,

    You mentioned tcpdump’s manual page on several occasions.
    Where is this? Can you send me the link to it?

    Thanx,

    Jeff.

  21. Daniel Raharjo says:

    Thanks !!
    quick but helpful

  22. soul_krasty says:

    hye..i got a switch which have snmp enable.can i tcpdump one of the switch port?

  23. @Daniel Raharjo
    Thanks for visiting and for a nice comment :D Please visit again.

  24. @soul_krasty
    tcpdump allows you to monitor packets coming in and out of machine you are running it on. If you are connected to the switch itself and have command line prompt on it, then yes you can. Use -i option with the right interface name. Otherwise, you cannot.

  25. Amit says:

    very nice article..
    good for beginers..

  26. usman says:

    Thanx dear its too easy too learn from ur artical.

  27. usman says:

    telecom engineer working on asterisk based billing system. i realy need this type of text for time saving.

  28. @Amit
    Thanks.
    @usman
    Thanks a lot for a warm comment and please come again :-)

  29. sparc86 says:

    Hi!

    Excellent tutorial! In fact, I have been using tcpdump since quite long time, however, I’m having problems using the expressions. For example, if I use “tcpdump -i eth0 port 80”, it doesn’t firlters anything, always 0 packets. If I use “tcpdump -i eth0 not port 80”, it still shows traffic from the port 80.

    It’s really really weird, since I have used tcpdump in a client a month ago without such problem.

    Do you have any idea about what can be happening?

    Thank you very much!

  30. sparc86 says:

    @sparc86 – Never mind, I already solved it :-)

  31. sparc86 says:

    @Alexander Sandler – Yes, sorry, my mistake was that I was using the wrong interface (eth0 instead of ppp0). I directly connect to my ISP by my linux box, therefore I should be using the ppp0 interface instead of eth0.

    In time, I want to congratulate you for the great job with this blog, plus all the quick answers you do here. Nobody pays you for that and, yet, you’re ready to help people with your knowledge. People like you are those who make the Open Source community alive strong.

    By the way, I have been working and studying linux sysadmin for a long time, at the moment I wanna learn more about C programming to, as soon as possible, start helping with kernel driver modules development.

    • @sparc86
      Thanks a lot for a warm comment and for congratulations. This is one of those things that makes you want to write more :-) It’s a good thing you’ve chosen to learn programming. It is very demanding, but very satisfying as well :-) And programming in kernel is the bleeding edge of all programming ;-)

  32. Zainab says:

    hi alexander,

    I am writing a course work on how to use tcpdump and was wondering if you could help me with removing broadcast and multicast packets.look forward to ur reply, your dummy-trainer is really good

  33. Zainab says:

    by removing I meant filtering.

  34. @Zainab
    Sorry, but I could not find any solution for you. For filtering out broadcast packets you can use ip address based filtering – e.g filter out 192.168.0.255.
    Here’s something more useful. Try capturing all traffic and then filter irrelevant packets in wireshark. This is what I would do.

  35. Praveen says:

    A truely complete and informative information

  36. seegod says:

    excellent tutorial.keep it up!

  37. Thank you very much!

    “-s0” was very useful for Anna, and “-e” is that’s need for Ilya! :-))

  38. Wade says:

    Alex,

    What is the exact syntax for seeing vlan tags on packet headers?

    Regards,

    Wade

  39. @Wade
    Wade, -e shows you VLAN tags. Have fun! :-)

  40. PaulFM says:

    Best how-to I’ve ever read. Write some networking books. Email me when they are available.

    Keep up the good work, thanks!

    Paul

  41. @PaulFM
    Wow. That’s one of the nicest comments I got, ever :-) Thanks a lot and please visit my web-site again :-)

  42. SECL says:

    Hi Alex,

    Is there a way to limit the number of files generated after being created by the -c or -C option ? – similar to the tethereal command option -b which wraps the number of files captured. I want to leave a continual trace running, and then take captured files as and when I need them.

    Thanks for any help…

  43. @SECL
    Hi.

    tcpdump itself doesn’t have an option to limit number of files it creates. What you can do is write a small script that deletes old files. Here’s python script that does this.

    #!/usr/bin/python
    
    import glob
    import time
    import os
    
    while True:
        d = glob.glob('cap*')
        d.sort()
        while len(d) > 10:
            os.remove(d[0])
        time.sleep(60)
    

    It assumes that you’re running tcpdump with “-w cap” switch. It permits 10 files and checks for new files every minute.

    Hope it helps :-)

  44. SECL says:

    @Alexander Sandler – Hey Alex…..Spot on !

    I’ll give it a try…..

    Thanks Very Much…..have a cyber Beer on me !!

  45. Thank you very much
    it was very useful session .

Leave a Reply

Prove you are not a computer or die *