Dumps traffic on a network
tcpdump [ -a ] [ -A ] [ -d ] [ -D ] [ -e ] [ -f ] [ -l ] [ -K ] [ -L ] [ -n ] [ -N ] [ -O ] [ -p ] [ -q ] [ -R ] [ -S ] [ -t ] [ -u ] [ -U ] [ -v ] [ -x ] [ -X ] [ -c count ][ -C file_size ] [ -F file ] [ -G rotate_seconds ] [ -i interface ] [ -m module ] [ -M secret ] [ -r file ][ -s snaplen ] [ -w file ][ -E addr ] [ -y datalinktype ] [-z command ] [-Z user ] [ expression ]
The tcpdump command prints the headers of packets on a network interface that match the boolean expression. You can run the command with the -w flag to save the packet data in a file for further analysis. You can also run the command with the -r flag to read data from a saved packet file instead reading the packets from a network interface. In all cases, only packets that match expression is processed by the tcpdump command.
If it is not run with the -c flag, tcpdump continues capturing packets until it is interrupted by a SIGINT signal (typically control-C) or a SIGTERM signal (typically the kill(1) command). If tcpdump is run with the -c flag, it captures the packets until it is interrupted by a SIGINT or SIGTERM signal or the specified number of packets have been processed.
Allowable Primitives
ether proto \ip and host host
If host
is a name with multiple IP addresses, each address is checked for
a match.If the subnet mask of the interface on which the capture is being done is not available, for example, because the interface on which capture is being done has no netmask this check does not work correctly.
Abbreviations for:
ether proto p
where p is
one of the above protocols. lat, moprc, mopdl
ether proto p
where p is
one of the above protocols. Note that tcpdump does
not currently know how to parse these protocols.ip proto p or ip6 proto p
where p is
one of the above protocols.proto [ expr : size ]
Proto
is one of ether, fddi, tr, wlan, ppp, slip, link, ip, arp, rarp, tcp,
udp, icmp or ip6, and indicates the protocol layer for the index operation.
(ether, fddi, wlan, tr, ppp, slip and link all refer to the link layer.)
Note that tcp, udp and other upper-layer protocol types only apply
to IPv4, not IPv6 (this will be fixed in the future). The byte offset,
relative to the indicated protocol layer, is given by expr. Size is
optional and indicates the number of bytes in the field of interest;
it can be either one, two, or four, and defaults to one. The length
operator, indicated by the keyword len, gives the length of the packet. For example, ether[0] & 1 != 0 catches all multicast traffic. The expression ip[0] & 0xf !=5 catches all IP packets with options. The expression ip[6:2] & 0x1fff = 0 catches only unfragmented datagrams and frag zero of fragmented datagrams. This check is implicitly applied to the tcp and udp index operations. For instance, tcp[0] always means the first byte of the TCP header, and never means the first byte of an intervening fragment.
Some offsets and field values may be expressed as names rather than as numeric values. The following protocol header field offsets are available: icmptype (ICMP type field), icmpcode (ICMP code field), and tcpflags (TCP flags field).
The following ICMP type field values are available: icmp-echoreply, icmp-unreach, icmp-sourcequench, icmp-redirect, icmp-echo, icmp-routeradvert, icmp-routersolicit, icmp-timxceed, icmp-paramprob, icmp-tstamp, icmp-tstampreply, icmp-ireq, icmp-ireqreply, icmp-maskreq, icmp- maskreply.
The following TCP flags field values are available: tcp-fin, tcp-syn, tcp-rst, tcp-push, tcp-ack, tcp-urg.
Combining Primitives
Negation (`!' or `not').
Concatenation (`&&' or `and').
Alternation (`||' or `or').
Negation has highest
precedence. Alternation and concatenation have equal precedence and
associate left to right. Note that explicits and tokens, not juxtaposition,
are now required for concatenation.If an identifier is given without a keyword, the most recent keyword is assumed. For example, not host vs and ace is short for not host vs and host ace which should not be confused with not ( host vs or ace )
Expression arguments can be passed to tcpdump as either a single argument or as multiple arguments, whichever is more convenient. Generally, if the expression contains Shell metacharacters, it is easier to pass it as a single, quoted argument. Multiple arguments are concatenated with spaces before being parsed.
Item | Description |
---|---|
-a | Attempts to convert network and broadcast addresses to names. |
-A | Prints each packet (minus its link level header) in ASCII. Handy for capturing web pages. |
-c | Exits after receiving Count packets. |
-C file_size | Before writing a raw packet to a savefile, check whether the file is currently larger than file_size and, if so, close the current savefile and open a new one. Savefiles after the first savefile has the name specified with the -w flag, with a number after it, starting at 2 and continuing upward. The units of file_size are millions of bytes (1,000,000 bytes, not 1,048,576 bytes). |
-d | Dumps the compiled packet-matching code to standard output, then stops. |
-D | Prints the list of the network interfaces available on the system and on which tcpdump can capture packets. For each network interface, a number and an interface name (possibly followed by a text description of the interface) is printed. The interface name or the number can be supplied to the -i flag to specify an interface on which to capture. |
-dd | Dumps packet-matching code as a C program fragment. |
-ddd | Dumps packet-matching code as decimal numbers (preceded with a count). |
-e | Prints the link-level header on each dump line. |
-E addr | Use spi@ipaddr algo:secret for decrypting
IPsec ESP packets that are addressed to addr and
contain Security Parameter Index value spi. This
combination may be repeated with comma or newline separation. Note: Setting
the secret for IPv4 ESP packets is now supported.
Algorithms may be des-cbc, 3des-cbc, blowfish-cbc, rc3-cbc, cast128-cbc, or none. The default is des-cbc. The ability to decrypt packets is only present if libcrypto is installed and is in LIBPATH. secret is the ASCII text for ESP secret key. If preceeded by 0x, then a hex value is read. The option assumes RFC2406 ESP, not RFC1827 ESP. The option is for debugging purposes only and the use of this option with a true secret key is discouraged. By presenting the IPsec secret key onto command line you make it visible to others, via ps(1) and other occasions. In addition to the above syntax, the tcpdump command might use the syntax file name to read the specified file. The file is opened upon receiving the first ESP packet, so any special permissions that tcpdump may have been given, should already have been given up. |
-f | Prints foreign IPv4 addresses numerically rather than symbolically. The test for foreign IPv4 addresses is done by using the IPv4 address and netmask of the interface on which capture is being performed. This option does not work correctly if that address or netmask is not available. |
-F file | Use file as input for the filter expression. An additional expression given on the command line is ignored. |
-G rotate_seconds | Rotates the dump file that is specified with the -w option every rotate_seconds seconds. If used in conjunction with the -C option, file names take the form of file<count>. |
-i interface | Listens on interface. If unspecified, tcpdump searches
the system interface list for the lowest
numbered, configured up interface (excluding
loopback). Ties are broken by choosing the earliest match. An interface number as printed by -D flag can be used as the interface argument. |
-K | Skips verification of TCP checksum on interfaces that perform TCP checksum calculation in hardware. If this flag is not used, all outgoing TCP checksums are flagged as bad. |
-l | Makes stdout line buffered. Useful
if you want to see the data while capturing it. For example:
|
-L | Lists the known data link types for the interface and exits. |
-m module | Loads SMI MIB module definitions from the module file. This option can be used several times to load several MIB modules into tcpdump. |
-M | Uses secret as a shared secret for validating the digests that are found in TCP segments by using the TCP-MD5 option (Request for Comment (RFC) 2385). |
-n | Blocks converting the host addresses, and the port numbers to names. |
-N | Omits printing domain name qualification of host names. For example, tcpdump prints nic instead of nic.ddn.mil. |
-O | Keeps tcpdump from running the packet-matching code optimizer. This is useful only if you suspect a bug in the optimizer. |
-p | Stops putting the interface into promiscuous mode. Note that the interface might be in promiscuous mode for some other reason; hence, -p cannot be used as an abbreviation for ether host {local-hw-addr} or ether broadcast. |
-q | Quick output. Prints less protocol information so output lines are shorter. |
-r file | Read packets from file (which was created with the -w option). Standard input is used if file is "-". |
-R | Assumes ESP/AH packets are based on old specification. (RFC1825 to RFC1829). If specified, tcpdump does not print replay prevention field. Since there is no protocol version field in ESP/AH specification, tcpdump cannot deduce the version of ESP/AH protocol. |
-S | Prints absolute rather than relative TCP sequence numbers. |
-s snaplen | Snarf snaplen bytes of data from each packet rather than the default of 68. 68 bytes is adequate for IP, ICMP, TCP and UDP but may truncate protocol information from name server and NFS packets (see below). Packets truncated because of a limited snapshot are indicated in the output with [|proto], where proto is the name of the protocol level at which the truncation has occurred. Note that taking larger snapshots increases the amount of time it takes to process packets and effectively decreases the amount of packet buffering. This can cause packets to be lost. You should limit snaplen to the smallest number that captures the protocol information you are interested in. Setting snaplen to 0 means use the required length to catch whole packets. |
-T | Forces packets selected by expression to be interpreted the specified type. Currently known types are cnfp (Cisco NetFlow protocol), rpc (Remote Procedure Call), rtp (Real-Time Applications protocol), rtcp (Real-Time Applications control protocol), snmp (Simple Network Management Protocol), tftp (Trivial File Transfer Protocol), vat (Visual Audio Tool), and wb (distributed White Board). |
-t | Omits the printing of a timestamp on each dump line. |
-tt | Prints an unformatted timestamp on each dump line. |
-ttt | Prints a delta (in microseconds) between current and previous line on each dump line. |
-tttt | Prints a timestamp in default format proceeded by date on each dump line. |
-ttttt | Prints a delta (in microseconds) between the current and the first line on each dump line. |
-u | Prints undecoded NFS handles. |
-U | Make output saved via the -w option, for example, "packet- buffered." As each packet is saved, it is written to the output file, rather than being written only when the output buffer fills. |
-v | Specifies slightly more verbose output. For example, the time to live, identification, total length and options in an IP packet are printed. Also enables additional packet integrity checks such as verifying the IP and ICMP header checksum. |
-vv | Even more verbose output than -v. For example, additional fields are printed from NFS and reply packets are fully decoded. |
-vvv | Even more verbose output than -vv. For example, telnet SB ... SE options are printed in full. With -X Telnet options are printed in hex as well. |
-w file | Writes the raw packets to file rather than parsing and printing them out. They can later be printed with the -r flag. Standard output is used if File is "-". |
-x | Prints each packet (minus its link level header) in hexadecimal. The smaller of the entire packet or snaplen bytes is printed. Note that this is the entire link-layer packet, so for link layers that pad (e.g. Ethernet), the padding bytes is also printed when the higher layer packet is shorter than the required padding. |
-xx | Prints each packet, including its link level header, in hexadecimal. |
-X | Prints each packet (minus its link level header) in hexadecimal and ASCII. This is very handy for analyzing new protocols. |
-y datalinktype | Sets the data link type to use while capturing packets to datalinktype. |
-z command | When used in conjunction with the -C or -G option,
causes the tcpdump command to run the specified command on
the savefile. For example, specifying -z gzip or -z
bzip2 compresses each savefile by using the gzip or bzip2 command. Note: The tcpdump command
runs the -z command in parallel to the capture by using the
lowest priority so that this does not disturb the capture process.
|
-Z user | Runs the tcpdump command with the system privileges of the specified user. |
fddi is an alias for ether. The parser treats it as meaning "the data link level used on the specified network interface." FDDI headers contain Ethernet-like source and destination addresses, and often contain Ethernet-like packet types, so you can filter on these FDDI fields just as with the analogous Ethernet fields. FDDI headers also contain other fields, but they cannot be named in a filter expression.
Like fddi, tr and wlan are aliases for ether. The previous paragraph's statements about FDDI headers also apply to Token Ring and 802.11 wireless LAN headers. For 802.11 headers, the destination address is the DA field and the source address is the SA field; the BSSID, RA, and TA fields aren't tested.
In addition to the above, there are some special `primitive' keywords that don't follow the pattern: gateway, broadcast, less, greater and arithmetic expressions. All of these are described below.
More complex filter expressions are built by using the words and, or, and not to combine primitives.
ksh$ LIBPATH=/opt/freeware/lib tcpdump -E"algo:secret"
Item | Description |
---|---|
0 | Success. |
non-zero | Error. |
Reading packets from a network interface requires read access to /dev/bpf*, which is typically root-only. Reading packets from a file does not require any special privileges except file read permission.
tcpdump host sundown
tcpdump host helios and \( hot or ace \)
tcpdump ip host ace and not helios
tcpdump net ucb-ether
tcpdump 'gateway snup and (port ftp or ftp-data)'
tcpdump ip and not net localnet
tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and d dst net localnet'
tcpdump 'gateway snup and ip[2:2] > 576'
tcpdump 'ether[0] & 1 = 0 and ip[16] >= 224'
tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-e choreply'
All errors and warnings are sent to stderr.
A packet trace that crosses a daylight savings time change gives skewed time stamps (the time change is ignored).
Filter expressions on fields other than those in Token Ring headers handles the source-routed Token Ring packets incorrectly.
Filter expressions on fields other than those in 802.11 headers handles the 802.11 data packets with both To DS and From DS set incorrectly.
ip6 proto should chase header chain, but at this moment it does not. ip6 protochain is supplied for this behavior.
Arithmetic expression against transport layer headers, like tcp[0], does not work against IPv6 packets. It only looks at IPv4 packets.
Item | Description |
---|---|
/usr/sbin/tcpdump | Location of the tcpdump command. |
/usr/lib/libpcap.a | |
/dev/bpf* | |
/opt/freeware/lib/libcrypto.a(libcrypto.so) | Optional |