Science and technology

An introduction to utilizing tcpdump on the Linux command line

In my expertise as a sysadmin, I’ve typically discovered community connectivity points difficult to troubleshoot. For these conditions, tcpdump is a good ally.

Tcpdump is a command line utility that means that you can seize and analyze community visitors going by way of your system. It is commonly used to assist troubleshoot community points, in addition to a safety device.

A robust and versatile device that features many choices and filters, tcpdump can be utilized in quite a lot of instances. Since it is a command line device, it’s ultimate to run in distant servers or gadgets for which a GUI isn’t out there, to gather information that may be analyzed later. It may also be launched within the background or as a scheduled job utilizing instruments like cron.

In this text, we’ll take a look at a few of tcpdump’s commonest options.

1. Installation on Linux

Tcpdump is included with a number of Linux distributions, so chances are high, you have already got it put in. Check if tcpdump is put in in your system with the next command:

$ which tcpdump
/usr/sbin/tcpdump

If tcpdump isn’t put in, you’ll be able to set up it however utilizing your distribution’s package deal supervisor. For instance, on CentOS or Red Hat Enterprise Linux, like this:

$ sudo yum set up -y tcpdump

Tcpdump requires libpcap, which is a library for community packet seize. If it is not put in, it will likely be mechanically added as a dependency.

You’re prepared to start out capturing some packets.

2. Capturing packets with tcpdump

To seize packets for troubleshooting or evaluation, tcpdump requires elevated permissions, so within the following examples most instructions are prefixed with sudo.

To start, use the command tcpdump -D to see which interfaces can be found for seize:

$ sudo tcpdump -D
1.eth0
2.virbr0
three.eth1
four.any (Pseudo-device that captures on all interfaces)
5.lo [Loopback]

In the instance above, you’ll be able to see all of the interfaces out there in my machine. The particular interface any permits capturing in any energetic interface.

Let’s use it to start out capturing some packets. Capture all packets in any interface by working this command:

$ sudo tcpdump -i any
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
09:56:18.293641 IP rhel75.localdomain.ssh > 192.168.64.1.56322: Flags [P.], seq 3770820720:3770820916, ack 3503648727, win 309, choices [nop,nop,TS val 76577898 ecr 510770929], size 196
09:56:18.293794 IP 192.168.64.1.56322 > rhel75.localdomain.ssh: Flags [.], ack 196, win 391, choices [nop,nop,TS val 510771017 ecr 76577898], size zero
09:56:18.295058 IP rhel75.59883 > gateway.area: 2486+ PTR? 1.64.168.192.in-addr.arpa. (43)
09:56:18.310225 IP gateway.area > rhel75.59883: 2486 NXDomain* zero/1/zero (102)
09:56:18.312482 IP rhel75.49685 > gateway.area: 34242+ PTR? 28.64.168.192.in-addr.arpa. (44)
09:56:18.322425 IP gateway.area > rhel75.49685: 34242 NXDomain* zero/1/zero (103)
09:56:18.323164 IP rhel75.56631 > gateway.area: 29904+ PTR? 1.122.168.192.in-addr.arpa. (44)
09:56:18.323342 IP rhel75.localdomain.ssh > 192.168.64.1.56322: Flags [P.], seq 196:584, ack 1, win 309, choices [nop,nop,TS val 76577928 ecr 510771017], size 388
09:56:18.323563 IP 192.168.64.1.56322 > rhel75.localdomain.ssh: Flags [.], ack 584, win 411, choices [nop,nop,TS val 510771047 ecr 76577928], size zero
09:56:18.335569 IP gateway.area > rhel75.56631: 29904 NXDomain* zero/1/zero (103)
09:56:18.336429 IP rhel75.44007 > gateway.area: 61677+ PTR? 98.122.168.192.in-addr.arpa. (45)
09:56:18.336655 IP gateway.area > rhel75.44007: 61677* 1/zero/zero PTR rhel75. (65)
09:56:18.337177 IP rhel75.localdomain.ssh > 192.168.64.1.56322: Flags [P.], seq 584:1644, ack 1, win 309, choices [nop,nop,TS val 76577942 ecr 510771047], size 1060

---- SKIPPING LONG OUTPUT -----

09:56:19.342939 IP 192.168.64.1.56322 > rhel75.localdomain.ssh: Flags [.], ack 1752016, win 1444, choices [nop,nop,TS val 510772067 ecr 76578948], size zero
^C
9003 packets captured
9010 packets obtained by filter
7 packets dropped by kernel
$

Tcpdump continues to seize packets till it receives an interrupt sign. You can interrupt capturing by urgent Ctrl+C. As you’ll be able to see on this instance, tcpdump captured greater than 9,000 packets. In this case, since I’m linked to this server utilizing ssh, tcpdump captured all these packages. To restrict the variety of packets captured and cease tcpdump, use the -c choice:

$ sudo tcpdump -i any -c 5
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
11:21:30.242740 IP rhel75.localdomain.ssh > 192.168.64.1.56322: Flags [P.], seq 3772575680:3772575876, ack 3503651743, win 309, choices [nop,nop,TS val 81689848 ecr 515883153], size 196
11:21:30.242906 IP 192.168.64.1.56322 > rhel75.localdomain.ssh: Flags [.], ack 196, win 1443, choices [nop,nop,TS val 515883235 ecr 81689848], size zero
11:21:30.244442 IP rhel75.43634 > gateway.area: 57680+ PTR? 1.64.168.192.in-addr.arpa. (43)
11:21:30.244829 IP gateway.area > rhel75.43634: 57680 NXDomain zero/zero/zero (43)
11:21:30.247048 IP rhel75.33696 > gateway.area: 37429+ PTR? 28.64.168.192.in-addr.arpa. (44)
5 packets captured
12 packets obtained by filter
zero packets dropped by kernel
$

In this case, tcpdump stopped capturing mechanically after capturing 5 packets. This is beneficial in numerous situations—as an illustration, for those who’re troubleshooting connectivity and capturing a couple of preliminary packages is sufficient. This is much more helpful after we apply filters to seize particular packets (proven under).

By default, tcpdump resolves IP addresses and ports into names, as proven within the earlier instance. When troubleshooting community points, it’s typically simpler to make use of the IP addresses and port numbers; disable title decision by utilizing the choice -n and port decision with -nn:

$ sudo tcpdump -i any -c5 -nn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
23:56:24.292206 IP 192.168.64.28.22 > 192.168.64.1.35110: Flags [P.], seq 166198580:166198776, ack 2414541257, win 309, choices [nop,nop,TS val 615664 ecr 540031155], size 196
23:56:24.292357 IP 192.168.64.1.35110 > 192.168.64.28.22: Flags [.], ack 196, win 1377, choices [nop,nop,TS val 540031229 ecr 615664], size zero
23:56:24.292570 IP 192.168.64.28.22 > 192.168.64.1.35110: Flags [P.], seq 196:568, ack 1, win 309, choices [nop,nop,TS val 615664 ecr 540031229], size 372
23:56:24.292655 IP 192.168.64.1.35110 > 192.168.64.28.22: Flags [.], ack 568, win 1400, choices [nop,nop,TS val 540031229 ecr 615664], size zero
23:56:24.292752 IP 192.168.64.28.22 > 192.168.64.1.35110: Flags [P.], seq 568:908, ack 1, win 309, choices [nop,nop,TS val 615664 ecr 540031229], size 340
5 packets captured
6 packets obtained by filter
zero packets dropped by kernel

As proven above, the seize output now shows the IP addresses and port numbers. This additionally prevents tcpdump from issuing DNS lookups, which helps to decrease community visitors whereas troubleshooting community points.

Now that you simply’re capable of seize community packets, let’s discover what this output means.

three. Understanding the output format

Tcpdump is able to capturing and decoding many various protocols, resembling TCP, UDP, ICMP, and lots of extra. While we will not cowl all of them right here, that can assist you get began, let’s discover the TCP packet. You can discover extra particulars in regards to the completely different protocol codecs in tcpdump’s manual pages. A typical TCP packet captured by tcpdump seems to be like this:

08:41:13.729687 IP 192.168.64.28.22 > 192.168.64.1.41916: Flags [P.], seq 196:568, ack 1, win 309, choices [nop,nop,TS val 117964079 ecr 816509256], size 372

The fields might fluctuate relying on the kind of packet being despatched, however that is the final format.

The first area, 08:41:13.729687, represents the timestamp of the obtained packet as per the native clock.

Next, IP represents the community layer protocol—on this case, IPv4. For IPv6 packets, the worth is IP6.

The subsequent area, 192.168.64.28.22, is the supply IP handle and port. This is adopted by the vacation spot IP handle and port, represented by 192.168.64.1.41916.

After the supply and vacation spot, yow will discover the TCP Flags Flags [P.]. Typical values for this area embrace:

Value Flag Type Description
S SYN Connection Start
F FIN Connection Finish
P PUSH Data push
R RST Connection reset
. ACK Acknowledgment

This area may also be a mix of those values, resembling [S.] for a SYN-ACK packet.

Next is the sequence variety of the info contained within the packet. For the primary packet captured, that is an absolute quantity. Subsequent packets use a relative quantity to make it simpler to comply with. In this instance, the sequence is seq 196:568, which suggests this packet accommodates bytes 196 to 568 of this circulation.

This is adopted by the Ack Number: ack 1. In this case, it’s 1 since that is the facet sending information. For the facet receiving information, this area represents the following anticipated byte (information) on this circulation. For instance, the Ack quantity for the following packet on this circulation could be 568.

The subsequent area is the window dimension win 309, which represents the variety of bytes out there within the receiving buffer, adopted by TCP choices such because the MSS (Maximum Segment Size) or Window Scale. For particulars about TCP protocol choices, seek the advice of Transmission Control Protocol (TCP) Parameters.

Finally, now we have the packet size, size 372, which represents the size, in bytes, of the payload information. The size is the distinction between the final and first bytes within the sequence quantity.

Now let’s learn to filter packages to slim down outcomes and make it simpler to troubleshoot particular points.

four. Filtering packets

As talked about above, tcpdump can seize too many packages, a few of which aren’t even associated to the problem you are troubleshooting. For instance, for those who’re troubleshooting a connectivity subject with an online server you are not within the SSH visitors, so eradicating the SSH packets from the output makes it simpler to work on the true subject.

One of tcpdump’s strongest options is its potential to filter the captured packets utilizing quite a lot of parameters, resembling supply and vacation spot IP addresses, ports, protocols, and so forth. Let’s take a look at a number of the commonest ones.

Protocol

To filter packets primarily based on protocol, specifying the protocol within the command line. For instance, seize ICMP packets solely by utilizing this command:

$ sudo tcpdump -i any -c5 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes

In a unique terminal, attempt to ping one other machine:

$ ping opensource.com
PING opensource.com (54.204.39.132) 56(84) bytes of knowledge.
64 bytes from ec2-54-204-39-132.compute-1.amazonaws.com (54.204.39.132): icmp_seq=1 ttl=47 time=39.6 ms

Back within the tcpdump seize, discover that tcpdump captures and shows solely the ICMP-related packets. In this case, tcpdump isn’t displaying title decision packets that had been generated when resolving the title opensource.com:

09:34:20.136766 IP rhel75 > ec2-54-204-39-132.compute-1.amazonaws.com: ICMP echo request, id 20361, seq 1, size 64
09:34:20.176402 IP ec2-54-204-39-132.compute-1.amazonaws.com > rhel75: ICMP echo reply, id 20361, seq 1, size 64
09:34:21.140230 IP rhel75 > ec2-54-204-39-132.compute-1.amazonaws.com: ICMP echo request, id 20361, seq 2, size 64
09:34:21.180020 IP ec2-54-204-39-132.compute-1.amazonaws.com > rhel75: ICMP echo reply, id 20361, seq 2, size 64
09:34:22.141777 IP rhel75 > ec2-54-204-39-132.compute-1.amazonaws.com: ICMP echo request, id 20361, seq three, size 64
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

Host

Limit seize to solely packets associated to a particular host by utilizing the host filter:

$ sudo tcpdump -i any -c5 -nn host 54.204.39.132
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
09:54:20.042023 IP 192.168.122.98.39326 > 54.204.39.132.80: Flags [S], seq 1375157070, win 29200, choices [mss 1460,sackOK,TS val 122350391 ecr zero,nop,wscale 7], size zero
09:54:20.088127 IP 54.204.39.132.80 > 192.168.122.98.39326: Flags [S.], seq 1935542841, ack 1375157071, win 28960, choices [mss 1460,sackOK,TS val 522713542 ecr 122350391,nop,wscale 9], size zero
09:54:20.088204 IP 192.168.122.98.39326 > 54.204.39.132.80: Flags [.], ack 1, win 229, choices [nop,nop,TS val 122350437 ecr 522713542], size zero
09:54:20.088734 IP 192.168.122.98.39326 > 54.204.39.132.80: Flags [P.], seq 1:113, ack 1, win 229, choices [nop,nop,TS val 122350438 ecr 522713542], size 112: HTTP: GET / HTTP/1.1
09:54:20.129733 IP 54.204.39.132.80 > 192.168.122.98.39326: Flags [.], ack 113, win 57, choices [nop,nop,TS val 522713552 ecr 122350438], size zero
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

In this instance, tcpdump captures and shows solely packets to and from host 54.204.39.132.

Port

To filter packets primarily based on the specified service or port, use the port filter. For instance, seize packets associated to an online (HTTP) service by utilizing this command:

$ sudo tcpdump -i any -c5 -nn port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
09:58:28.790548 IP 192.168.122.98.39330 > 54.204.39.132.80: Flags [S], seq 1745665159, win 29200, choices [mss 1460,sackOK,TS val 122599140 ecr zero,nop,wscale 7], size zero
09:58:28.834026 IP 54.204.39.132.80 > 192.168.122.98.39330: Flags [S.], seq 4063583040, ack 1745665160, win 28960, choices [mss 1460,sackOK,TS val 522775728 ecr 122599140,nop,wscale 9], size zero
09:58:28.834093 IP 192.168.122.98.39330 > 54.204.39.132.80: Flags [.], ack 1, win 229, choices [nop,nop,TS val 122599183 ecr 522775728], size zero
09:58:28.834588 IP 192.168.122.98.39330 > 54.204.39.132.80: Flags [P.], seq 1:113, ack 1, win 229, choices [nop,nop,TS val 122599184 ecr 522775728], size 112: HTTP: GET / HTTP/1.1
09:58:28.878445 IP 54.204.39.132.80 > 192.168.122.98.39330: Flags [.], ack 113, win 57, choices [nop,nop,TS val 522775739 ecr 122599184], size zero
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

Source IP/hostname

You may also filter packets primarily based on the supply or vacation spot IP Address or hostname. For instance, to seize packets from host 192.168.122.98:

$ sudo tcpdump -i any -c5 -nn src 192.168.122.98
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
10:02:15.220824 IP 192.168.122.98.39436 > 192.168.122.1.53: 59332+ A? opensource.com. (32)
10:02:15.220862 IP 192.168.122.98.39436 > 192.168.122.1.53: 20749+ AAAA? opensource.com. (32)
10:02:15.364062 IP 192.168.122.98.39334 > 54.204.39.132.80: Flags [S], seq 1108640533, win 29200, choices [mss 1460,sackOK,TS val 122825713 ecr zero,nop,wscale 7], size zero
10:02:15.409229 IP 192.168.122.98.39334 > 54.204.39.132.80: Flags [.], ack 669337581, win 229, choices [nop,nop,TS val 122825758 ecr 522832372], size zero
10:02:15.409667 IP 192.168.122.98.39334 > 54.204.39.132.80: Flags [P.], seq zero:112, ack 1, win 229, choices [nop,nop,TS val 122825759 ecr 522832372], size 112: HTTP: GET / HTTP/1.1
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

Notice that tcpdumps captured packets with supply IP handle 192.168.122.98 for a number of companies resembling title decision (port 53) and HTTP (port 80). The response packets will not be displayed since their supply IP is completely different.

Conversely, you should utilize the dst filter to filter by vacation spot IP/hostname:

$ sudo tcpdump -i any -c5 -nn dst 192.168.122.98
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
10:05:03.572931 IP 192.168.122.1.53 > 192.168.122.98.47049: 2248 1/zero/zero A 54.204.39.132 (48)
10:05:03.572944 IP 192.168.122.1.53 > 192.168.122.98.47049: 33770 zero/zero/zero (32)
10:05:03.621833 IP 54.204.39.132.80 > 192.168.122.98.39338: Flags [S.], seq 3474204576, ack 3256851264, win 28960, choices [mss 1460,sackOK,TS val 522874425 ecr 122993922,nop,wscale 9], size zero
10:05:03.667767 IP 54.204.39.132.80 > 192.168.122.98.39338: Flags [.], ack 113, win 57, choices [nop,nop,TS val 522874436 ecr 122993972], size zero
10:05:03.672221 IP 54.204.39.132.80 > 192.168.122.98.39338: Flags [P.], seq 1:643, ack 113, win 57, choices [nop,nop,TS val 522874437 ecr 122993972], size 642: HTTP: HTTP/1.1 302 Found
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

Complex expressions

You may also mix filters by utilizing the logical operators and and or to create extra complicated expressions. For instance, to filter packets from supply IP handle 192.168.122.98 and repair HTTP solely, use this command:

$ sudo tcpdump -i any -c5 -nn src 192.168.122.98 and port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
10:08:00.472696 IP 192.168.122.98.39342 > 54.204.39.132.80: Flags [S], seq 2712685325, win 29200, choices [mss 1460,sackOK,TS val 123170822 ecr zero,nop,wscale 7], size zero
10:08:00.516118 IP 192.168.122.98.39342 > 54.204.39.132.80: Flags [.], ack 268723504, win 229, choices [nop,nop,TS val 123170865 ecr 522918648], size zero
10:08:00.516583 IP 192.168.122.98.39342 > 54.204.39.132.80: Flags [P.], seq zero:112, ack 1, win 229, choices [nop,nop,TS val 123170866 ecr 522918648], size 112: HTTP: GET / HTTP/1.1
10:08:00.567044 IP 192.168.122.98.39342 > 54.204.39.132.80: Flags [.], ack 643, win 239, choices [nop,nop,TS val 123170916 ecr 522918661], size zero
10:08:00.788153 IP 192.168.122.98.39342 > 54.204.39.132.80: Flags [F.], seq 112, ack 643, win 239, choices [nop,nop,TS val 123171137 ecr 522918661], size zero
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

You can create extra complicated expressions by grouping filter with parentheses. In this case, enclose your entire filter expression with citation marks to forestall the shell from complicated them with shell expressions:

$ sudo tcpdump -i any -c5 -nn "port 80 and (src 192.168.122.98 or src 54.204.39.132)"
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
10:10:37.602214 IP 192.168.122.98.39346 > 54.204.39.132.80: Flags [S], seq 871108679, win 29200, choices [mss 1460,sackOK,TS val 123327951 ecr zero,nop,wscale 7], size zero
10:10:37.650651 IP 54.204.39.132.80 > 192.168.122.98.39346: Flags [S.], seq 854753193, ack 871108680, win 28960, choices [mss 1460,sackOK,TS val 522957932 ecr 123327951,nop,wscale 9], size zero
10:10:37.650708 IP 192.168.122.98.39346 > 54.204.39.132.80: Flags [.], ack 1, win 229, choices [nop,nop,TS val 123328000 ecr 522957932], size zero
10:10:37.651097 IP 192.168.122.98.39346 > 54.204.39.132.80: Flags [P.], seq 1:113, ack 1, win 229, choices [nop,nop,TS val 123328000 ecr 522957932], size 112: HTTP: GET / HTTP/1.1
10:10:37.692900 IP 54.204.39.132.80 > 192.168.122.98.39346: Flags [.], ack 113, win 57, choices [nop,nop,TS val 522957942 ecr 123328000], size zero
5 packets captured
5 packets obtained by filter
zero packets dropped by kernel

In this instance, we’re filtering packets for HTTP service solely (port 80) and supply IP addresses 192.168.122.98 or 54.204.39.132. This is a fast approach of inspecting each side of the identical circulation.

5. Checking packet content material

In the earlier examples, we’re checking solely the packets’ headers for data resembling supply, locations, ports, and so forth. Sometimes that is all we have to troubleshoot community connectivity points. Sometimes, nevertheless, we have to examine the content material of the packet to make sure that the message we’re sending accommodates what we want or that we obtained the anticipated response. To see the packet content material, tcpdump gives two extra flags: -X to print content material in hex, and ASCII or -A to print the content material in ASCII.

For instance, examine the HTTP content material of an online request like this:

$ sudo tcpdump -i any -c10 -nn -A port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
13:02:14.871803 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [S], seq 2546602048, win 29200, choices [mss 1460,sackOK,TS val 133625221 ecr zero,nop,wscale 7], size zero
E..<..@.@.....zb6.'[email protected]............
............................
13:02:14.910734 IP 54.204.39.132.80 > 192.168.122.98.39366: Flags [S.], seq 1877348646, ack 2546602049, win 28960, choices [mss 1460,sackOK,TS val 525532247 ecr 133625221,nop,wscale 9], size zero
E..<..@./..a6.'
...zb.P..o..&...A..q a..........
.R.W.......     ................
13:02:14.910832 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [.], ack 1, win 229, choices [nop,nop,TS val 133625260 ecr 525532247], size zero
E..four..@.@.....zb6.'....P...Ao..'...........
.....R.W................
13:02:14.911808 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [P.], seq 1:113, ack 1, win 229, choices [nop,nop,TS val 133625261 ecr 525532247], size 112: HTTP: GET / HTTP/1.1
E.....@.@..1..zb6.'....P...Ao..'...........
.....R.WGET / HTTP/1.1
User-Agent: Wget/1.14 (linux-gnu)
Accept: */*
Host: opensource.com
Connection: Keep-Alive

................
13:02:14.951199 IP 54.204.39.132.80 > 192.168.122.98.39366: Flags [.], ack 113, win 57, choices [nop,nop,TS val 525532257 ecr 133625261], size zero
E..four.F@./.."6.'...zb.P..o..'.......9.2.....
.R.a....................
13:02:14.955030 IP 54.204.39.132.80 > 192.168.122.98.39366: Flags [P.], seq 1:643, ack 113, win 57, choices [nop,nop,TS val 525532258 ecr 133625261], size 642: HTTP: HTTP/1.1 302 Found
E....G@./...6.'...zb.P..o..'.......9.......
.R.b....HTTP/1.1 302 Found
Server: nginx
Date: Sun, 23 Sep 2018 17:02:14 GMT
Content-Type: textual content/html; charset=iso-8859-1
Content-Length: 207
X-Content-Type-Options: nosniff
Location: https://opensource.com/
Cache-Control: max-age=1209600
Expires: Sun, 07 Oct 2018 17:02:14 GMT
X-Request-ID: v-6baa3acc-bf52-11e8-9195-22000ab8cf2d
X-Varnish: 632951979
Age: zero
Via: 1.1 varnish (Varnish/5.2)
X-Cache: MISS
Connection: keep-alive

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.zero//EN">
<html><head>
<title>302 Found</title>
</head><physique>
<h1>Found</h1>
<p>The doc has moved <a href="
https://opensource.com/">right here</a>.</p>
</physique></html>
................
13:02:14.955083 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [.], ack 643, win 239, choices [nop,nop,TS val 133625304 ecr 525532258], size zero
E..four..@[email protected].'....P....o..............
.....R.b................
13:02:15.195524 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [F.], seq 113, ack 643, win 239, choices [nop,nop,TS val 133625545 ecr 525532258], size zero
E..four..@[email protected].'....P....o..............
.....R.b................
13:02:15.236592 IP 54.204.39.132.80 > 192.168.122.98.39366: Flags [F.], seq 643, ack 114, win 57, choices [nop,nop,TS val 525532329 ecr 133625545], size zero
E..four.H@./.. 6.'...zb.P..o..........9.I.....
.R......................
13:02:15.236656 IP 192.168.122.98.39366 > 54.204.39.132.80: Flags [.], ack 644, win 239, choices [nop,nop,TS val 133625586 ecr 525532329], size zero
E..four..@[email protected].'....P....o..............
.....R..................
10 packets captured
10 packets obtained by filter
zero packets dropped by kernel

This is useful for troubleshooting points with API calls, assuming the calls are utilizing plain HTTP. For encrypted connections, this output is much less helpful.

6. Saving captures to a file

Another helpful characteristic offered by tcpdump is the power to save lots of the seize to a file so you’ll be able to analyze the outcomes later. This means that you can seize packets in batch mode in a single day, for instance, and confirm the leads to the morning. It additionally helps when there are too many packets to research since real-time seize can happen too quick.

To save packets to a file as an alternative of displaying them on display screen, use the choice -w:

$ sudo tcpdump -i any -c10 -nn -w webserver.pcap port 80
[sudo] password for ricardo:
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), seize dimension 262144 bytes
10 packets captured
10 packets obtained by filter
zero packets dropped by kernel

This command saves the output in a file named webserver.pcap. The .pcap extension stands for “packet capture” and is the conference for this file format.

As proven on this instance, nothing will get displayed on-screen, and the seize finishes after capturing 10 packets, as per the choice -c10. If you need some suggestions to make sure packets are being captured, use the choice -v.

Tcpdump creates a file in binary format so you can not merely open it with a textual content editor. To learn the contents of the file, execute tcpdump with the -r choice:

$ tcpdump -nn -r webserver.pcap
studying from file webserver.pcap, link-type LINUX_SLL (Linux cooked)
13:36:57.679494 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [S], seq 3709732619, win 29200, choices [mss 1460,sackOK,TS val 135708029 ecr zero,nop,wscale 7], size zero
13:36:57.718932 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [S.], seq 1999298316, ack 3709732620, win 28960, choices [mss 1460,sackOK,TS val 526052949 ecr 135708029,nop,wscale 9], size zero
13:36:57.719005 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [.], ack 1, win 229, choices [nop,nop,TS val 135708068 ecr 526052949], size zero
13:36:57.719186 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [P.], seq 1:113, ack 1, win 229, choices [nop,nop,TS val 135708068 ecr 526052949], size 112: HTTP: GET / HTTP/1.1
13:36:57.756979 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [.], ack 113, win 57, choices [nop,nop,TS val 526052959 ecr 135708068], size zero
13:36:57.760122 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [P.], seq 1:643, ack 113, win 57, choices [nop,nop,TS val 526052959 ecr 135708068], size 642: HTTP: HTTP/1.1 302 Found
13:36:57.760182 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [.], ack 643, win 239, choices [nop,nop,TS val 135708109 ecr 526052959], size zero
13:36:57.977602 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [F.], seq 113, ack 643, win 239, choices [nop,nop,TS val 135708327 ecr 526052959], size zero
13:36:58.022089 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [F.], seq 643, ack 114, win 57, choices [nop,nop,TS val 526053025 ecr 135708327], size zero
13:36:58.022132 IP 192.168.122.98.39378 > 54.204.39.132.80: Flags [.], ack 644, win 239, choices [nop,nop,TS val 135708371 ecr 526053025], size zero
$

Since you are now not capturing the packets immediately from the community interface, sudo isn’t required to learn the file.

You may also use any of the filters we have mentioned to filter the content material from the file, simply as you’d with real-time information. For instance, examine the packets within the seize file from supply IP handle 54.204.39.132 by executing this command:

$ tcpdump -nn -r webserver.pcap src 54.204.39.132
studying from file webserver.pcap, link-type LINUX_SLL (Linux cooked)
13:36:57.718932 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [S.], seq 1999298316, ack 3709732620, win 28960, choices [mss 1460,sackOK,TS val 526052949 ecr 135708029,nop,wscale 9], size zero
13:36:57.756979 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [.], ack 113, win 57, choices [nop,nop,TS val 526052959 ecr 135708068], size zero
13:36:57.760122 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [P.], seq 1:643, ack 113, win 57, choices [nop,nop,TS val 526052959 ecr 135708068], size 642: HTTP: HTTP/1.1 302 Found
13:36:58.022089 IP 54.204.39.132.80 > 192.168.122.98.39378: Flags [F.], seq 643, ack 114, win 57, choices [nop,nop,TS val 526053025 ecr 135708327], size zero

What’s subsequent?

These primary options of tcpdump will aid you get began with this highly effective and versatile device. To study extra, seek the advice of the tcpdump website and man pages.

The tcpdump command line interface gives nice flexibility for capturing and analyzing community visitors. If you want a graphical device to grasp extra complicated flows, take a look at Wireshark.

One good thing about Wireshark is that it might learn .pcap information captured by tcpdump. You can use tcpdump to seize packets in a distant machine that doesn’t have a GUI and analyze the outcome file with Wireshark, however that could be a subject for one more day.

Most Popular

To Top