{"id":7084,"date":"2018-10-17T12:18:06","date_gmt":"2018-10-17T09:18:06","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-tcpdump-command\/"},"modified":"2018-10-17T12:18:06","modified_gmt":"2018-10-17T09:18:06","slug":"linux-tcpdump-command-tutorial-for-beginners-8-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-tcpdump-command-tutorial-for-beginners-8-examples\/","title":{"rendered":"Linux tcpdump Command Tutorial for Beginners (8 Examples)"},"content":{"rendered":"<p>Everytime you open a webpage on your computer, data packets are sent and received on your network interface. Sometimes, analyzing these packets becomes important for many reasons. Thankfully, Linux offers a command line utility that dumps information related to these data packets in output.<\/p>\n<p>In this article, we will discuss the basics of the tool in question &#8211; <strong>tcpdump<\/strong>. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-tcpdump-command\">Linux tcpdump command<\/h2>\n<p>The tcpdump command in Linux lets you dump traffic on a network. Following is its syntax in short:<\/p>\n<p class=\"command\">tcpdump [OPTIONS]<\/p>\n<p>Here&#8217;s the detailed syntax:<\/p>\n<p class=\"command\">tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -c count ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -C file_size ] [ -G rotate_seconds ] [ -F file ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ &#8211;number ] [ -Q in|out|inout ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -W filecount ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -E <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"7506051c351c0514111107\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a> algo:secret,&#8230;\u00a0 ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ &#8211;time-stamp-precision=tstamp_precision ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ &#8211;immediate-mode ] [ &#8211;version ]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [ expression ]<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre readability=\"15\">Tcpdump prints out a description of the contents of packets on a network interface that match the <br\/>boolean expression; the description is preceded by a time stamp, printed, by default, as hours, <br\/>minutes, seconds, and fractions of a second\u00a0 since\u00a0 midnight.\u00a0\u00a0 <p>It can\u00a0 also\u00a0 be\u00a0 run with the -w flag, which causes it to save the packet data to a file for <br\/>later analysis, and\/or with the -r flag, which causes it to read from a saved packet file rather <br\/>than to read packets from a network interface.\u00a0 It can also be run with the -V\u00a0 flag,\u00a0 which causes<br\/>it to read a list of saved packet files. <\/p><p>In all cases, only packets that match expression will be processed by tcpdump.<\/p><\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a better idea on how the tcpdump command works.<\/p>\n<h2 id=\"q-how-to-use-tcpdump\">Q1. How to use tcpdump?<\/h2>\n<p>Before using tcpdump to sniff data packets, you should ideally know which network interface you want the tool to work on. For a list of network interfaces available on the system, use the -D command line option with tcpdump.<\/p>\n<p class=\"command\">tcpdump -D<\/p>\n<p>Here&#8217;s how the man page explains this option:<\/p>\n<pre readability=\"13\">Print the list of the network interfaces available on the system and on which tcpdump can capture <br\/>packets. For each network interface, a number and an interface name, possibly followed by a text <br\/>description of the interface, is printed. The interface name or the number can be supplied to the <br\/>-i flag to specify an interface on which to capture.<p>This can be useful on systems that don't have a command to list them (e.g., Windows systems, or <br\/>UNIX systems lacking\u00a0 ifconfig -a); the number can be useful on Windows 2000 and later systems, <br\/>where the interface name is a somewhat complex string.<\/p><p>The\u00a0 -D\u00a0 flag\u00a0 will not be supported if tcpdump was built with an older version of libpcap that <br\/>lacks the pcap_findalldevs() function.<\/p><\/pre>\n<p>For example, in my case, the following output was produced:<\/p>\n<pre>1.wlx18a6f713679b [Up, Running]<br\/>2.any (Pseudo-device that captures on all interfaces) [Up, Running]<br\/>3.lo [Up, Running, Loopback]<br\/>4.enp3s0 [Up]<br\/>5.nflog (Linux netfilter log (NFLOG) interface)<br\/>6.nfqueue (Linux netfilter queue (NFQUEUE) interface)<br\/>7.usbmon1 (USB bus number 1)<br\/>8.usbmon2 (USB bus number 2)<br\/>9.usbmon3 (USB bus number 3)<br\/>10.usbmon4 (USB bus number 4)<\/pre>\n<p>Now that you have a list of interfaces, you can choose one, and pass its name as input to the -i command line option of tcpdump. For example:<\/p>\n<p class=\"command\">tcpdump -i wlx18a6f713679b<\/p>\n<p>Following is a part of output produced by this command in my case:<\/p>\n<pre>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br\/><strong>listening on wlx18a6f713679b<\/strong>, link-type EN10MB (Ethernet), capture size 262144 bytes<br\/>11:53:11.408964 IP 192.168.2.132.52898 &gt; 239.255.255.250.3702: UDP, length 656<br\/>11:53:11.409492 IP himanshu.57383 &gt; one.one.one.one.domain: 8572+ [1au] PTR? 250.255.255.239.in-addr.arpa. (57)<br\/>11:53:11.409588 IP 192.168.2.140.49690 &gt; 239.255.255.250.1900: UDP, length 174<br\/>11:53:11.409954 IP 192.168.2.147.mdns &gt; 224.0.0.251.mdns: 1 [2q] PTR (QU)? _%9E5E7C8F47989526C9BCD95D24084F6F0B27C5ED._sub._googlecast._tcp.local. PTR (QU)? _googlecast._tcp.local. (94)<br\/>11:53:11.499220 IP one.one.one.one.domain &gt; himanshu.57383: 8572 NXDomain 0\/1\/1 (114)<br\/>11:53:11.499950 IP himanshu.34201 &gt; one.one.one.one.domain: 12068+ [1au] PTR? 132.2.168.192.in-addr.arpa. (55)<br\/>11:53:11.591291 IP one.one.one.one.domain &gt; himanshu.34201: 12068 NXDomain* 0\/1\/2 (278)<br\/>11:53:11.592195 IP himanshu.55501 &gt; one.one.one.one.domain: 31630+ [1au] PTR? 1.1.1.1.in-addr.arpa. (49)<br\/>11:53:11.842603 IP himanshu.57253 &gt; one.one.one.one.domain: 16905+ [1au] PTR? 140.2.168.192.in-addr.arpa. (55)<br\/>11:53:11.951226 IP himanshu.52662 &gt; one.one.one.one.domain: 52377+ [1au] PTR? 251.0.0.224.in-addr.arpa. (53)<br\/>11:53:12.428931 IP 192.168.2.140.49690 &gt; 239.255.255.250.1900: UDP, length 174<br\/>11:53:12.437905 IP 192.168.2.147.mdns &gt; 224.0.0.251.mdns: 2 [2q] PTR (QM)? _%9E5E7C8F47989526C9BCD95D24084F6F0B27C5ED._sub._googlecast._tcp.local. PTR (QM)? _googlecast._tcp.local. (94)<br\/>11:53:12.438730 IP6 fe80::eccc:59f2:fc78:9e07.52899 &gt; ff02::c.3702: UDP, length 656<br\/>11:53:12.439017 IP himanshu.33194 &gt; one.one.one.one.domain: 15679+ [1au] PTR? c.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa. (101)<br\/>11:53:12.572386 IP one.one.one.one.domain &gt; himanshu.33194: 15679 NXDomain 0\/1\/1 (165)<br\/>11:53:12.642129 IP 192.168.2.11.mdns &gt; 224.0.0.251.mdns: 0*- [0q] 1\/0\/3 PTR Google-Home-Mini-e3a3ae72a03e3d1c60d24653d4666ca6._googlecast._tcp.local. (356)<br\/>...<br\/>...<\/pre>\n<h2 id=\"q-how-to-make-tcpdump-exit-after-receiving-set-number-of-packets\">Q2. How to make tcpdump exit after receiving set number of packets?<\/h2>\n<p>This can be achieved using the -c command line option. For example, if you want tcpdump to only display information related to 10 packets, then you can do that in the following way:<\/p>\n<p class=\"command\">tcpdump -c 10<\/p>\n<p>For example, in my case, I executed the following command:<\/p>\n<p class=\"command\">tcpdump -c 10 -i wlx18a6f713679b<\/p>\n<p>Following is the output that was produced:<\/p>\n<p><a class=\"fancybox\" id=\"img-tcpdump-c-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tcpdump-c-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/tcpdump-c-option.png\" alt=\"How to make tcpdump exit after receiving set number of packets\" width=\"500\" height=\"335\" title=\"\"><\/a><\/p>\n<p>So you can see 10 packets were captured.<\/p>\n<h2 id=\"q-how-to-make-tcpdump-display-linklevel-header-in-output\">Q3. How to make tcpdump display link-level header in output?<\/h2>\n<p>This can be done using the -e command line option. For example:<\/p>\n<p class=\"command\">tcpdump -e -i wlx18a6f713679b<\/p>\n<p>And here&#8217;s the output produced:<\/p>\n<pre>listening on wlx18a6f713679b, link-type EN10MB (Ethernet), capture size 262144 bytes<br\/>12:14:07.951396 3c:a8:2a:a7:7b:c1 (oui Unknown) &gt; Broadcast, ethertype IPv4 (0x0800), length 197: 192.168.2.55.17500 &gt; 255.255.255.255.17500: UDP, length 155<br\/>12:14:07.952111 3c:a8:2a:a7:7b:c1 (oui Unknown) &gt; Broadcast, ethertype IPv4 (0x0800), length 197: 192.168.2.55.17500 &gt; 192.168.2.255.17500: UDP, length 155<br\/>...<br\/>...<\/pre>\n<p>So you can see that link level headers were produced in the output.<\/p>\n<h2 id=\"q-how-to-make-tcpdump-displaynbspforeign-ip-addresses-numerically\">Q4. How to make tcpdump display\u00a0foreign IP addresses numerically?<\/h2>\n<p>This can be achieved using the -f command line option.<\/p>\n<p class=\"command\">tcpdump -f -i [INTERFACE]<\/p>\n<p>Making tcpdump display `foreign&#8217;\u00a0 IPv4\u00a0 addresses\u00a0 numerically rather than symbolically has its advantages in certain situations. One such example is mentioned in the tool&#8217;s man page:<\/p>\n<pre>this option is intended to get around serious brain damage in Sun's NIS server \u2014 usually it hangs <br\/>forever translating non-local internet numbers<\/pre>\n<h2 id=\"q-how-to-make-tcpdump-produce-packet-numbers-in-output\">Q5. How to make tcpdump produce packet numbers in output?<\/h2>\n<p>To make tcpdump produce packet numbers in output, use the <strong>&#8211;number<\/strong> command line option.<\/p>\n<p>For example, I executed the following command:<\/p>\n<p class=\"command\">tcpdump &#8211;number -i wlx18a6f713679b<\/p>\n<p>And here&#8217;s part of the output that was produced:<\/p>\n<pre>listening on wlx18a6f713679b, link-type EN10MB (Ethernet), capture size 262144 bytes<br\/>\u00a0\u00a0\u00a0 1\u00a0 12:38:35.522944 ARP, Request who-has 192.168.2.196 tell _gateway, length 46<br\/>\u00a0\u00a0\u00a0 2\u00a0 12:38:35.523265 IP6 fe80::8ab4:a6ff:fe9d:a6bb &gt; ff02::16: HBH ICMP6, multicast listener report v2, 1 group record(s), length 28<br\/>\u00a0\u00a0\u00a0 3\u00a0 12:38:35.523578 IP6 fe80::8ab4:a6ff:fe9d:a6bb &gt; ip6-allrouters: ICMP6, router solicitation, length 16<br\/>\u00a0\u00a0\u00a0 4\u00a0 12:38:35.523861 ARP, Request who-has 192.168.2.15 tell 192.168.2.170, length 28<br\/>...<br\/>...<br\/>...<\/pre>\n<p>So you can see each line now begins with a number.<\/p>\n<h2 id=\"q-how-to-make-tcpdump-print-shorter-output\">Q6. How to make tcpdump print shorter output?<\/h2>\n<p>This can be done using the -q command line option. Here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre>Quick (quiet?) output.\u00a0 Print less protocol information so output lines are shorter.<\/pre>\n<p>Following is an example of this option:<\/p>\n<p><a class=\"fancybox\" id=\"img-tcpdump-q-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tcpdump-q-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/tcpdump-q-option.png\" alt=\"How to make tcpdump print shorter output\" width=\"500\" height=\"142\" title=\"\"><\/a><\/p>\n<p>So you can see less information was produced in output this time.<\/p>\n<h2 id=\"q-how-to-omit-timestamp-info-from-tcpdump-output\">Q7. How to omit timestamp info from tcpdump output?<\/h2>\n<p>Use the -t command line option for this. Here&#8217;s an example command:<\/p>\n<p class=\"command\">tcpdump -t -i wlx18a6f713679b<\/p>\n<p>And following is its output:<\/p>\n<pre>listening on wlx18a6f713679b, link-type EN10MB (Ethernet), capture size 262144 bytes<br\/>IP himanshu.56992 &gt; mails11.telegram.org.https: Flags [.], ack 3090054317, win 965, options [nop,nop,TS val 226976758 ecr 3781653930], length 0<br\/>IP himanshu.41122 &gt; one.one.one.one.domain: 12755+ [1au] PTR? 22.171.154.149.in-addr.arpa. (56)<br\/>IP mails11.telegram.org.https &gt; himanshu.56824: Flags [.], ack 273652159, win 1001, options [nop,nop,TS val 3781656472 ecr 226966324], length 0<br\/>ARP, Request who-has 192.168.2.48 tell _gateway, length 46<br\/>ARP, Request who-has 192.168.2.135 tell _gateway, length 46<br\/>...<br\/>...<\/pre>\n<p>So you can see timestamp information (which is generally in the beginning of each line) isn&#8217;t present in output now.<\/p>\n<h2 id=\"q-how-to-make-tcpdump-produce-detailed-output\">Q8. How to make tcpdump produce detailed output?<\/h2>\n<p>You can use the -v command line option in this case. Following is how the tool&#8217;s man page explains this option:<\/p>\n<p class=\"command\">tcpdump -v -i [INTERFACE]<\/p>\n<pre readability=\"8\">When parsing and printing, produce (slightly more) verbose output. For example, the time to live,<br\/>identification, total length and options in an IP packet are printed. Also enables additional <br\/>packet integrity checks such as verifying the IP and ICMP header checksum.<p>When writing to a file with the -w option, report, every 10 seconds, the number of packets captured<\/p><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>We&#8217;ve just scratched the surface here as the tcpdump command offers a lot of command line options. Once you are done practicing these, you can head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/8\/tcpdump\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tcpdump-command-tutorial-for-beginners-8-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tcpdump-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tcpdump-command-tutorial-for-beginners-8-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tcpdump-command%2F&amp;text=Linux+tcpdump+Command+Tutorial+for+Beginners+%288+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tcpdump-command-tutorial-for-beginners-8-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tcpdump-command-tutorial-for-beginners-8-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tcpdump-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-tcpdump-command-tutorial-for-beginners-8-examples-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Everytime you open a webpage on your computer, data packets are sent and received on your network interface. Sometimes, analyzing these packets becomes important for many reasons. Thankfully, Linux offers a command line utility that dumps information related to these data packets in output. In this article, we will discuss the basics of the tool [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7084","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7084"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7084\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}