Course: Network and System Administration

Bonus lab: Intro to watching packets with tcpdump

Note: This lab is not required to successfully complete the course. It is here to help you improve and expand your Linux experience and knowledge.

Note: This lab is done on your development server, NOT your production server.

Objective: Use tcpdump to watch individual packets cross your network interface, and read what they tell you.

Everything your server sends or receives on the network travels as packets. tcpdump shows you those packets, one line each, as they cross an interface. Network engineers use it, and tools like it, to see what is really on the wire.

How to use the hints

Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.

Lab overview

  1. Before you begin - workspace, two sessions and installing tcpdump
  2. Part 1: Watch a ping
  3. Part 2: Watch a DNS lookup - Task 1
  4. Questions to consider

If you get stuck, check the Troubleshooting section at the end of this lab.


Before you begin

Create your workspace

Keep all of your bonus lab work together in ~/bonus-lab, with a subdirectory for each bonus lab. If you have already done another bonus lab, bonus-lab will already exist:

mkdir -p ~/bonus-lab/watching-packets-with-tcpdump cd ~/bonus-lab/watching-packets-with-tcpdump pwd

Open two sessions

You need two SSH sessions to your development server, both logged in as your own user. You run tcpdump in the first session, and make some network traffic in the second.

Find your interface name

The examples use enp0s1, from a UTM server. VirtualBox usually uses enp0s3. Check yours, as in Lab 9:

ip -br address

Use the name of the interface that isn't lo everywhere you see enp0s1.

Install tcpdump

sudo dnf install -y tcpdump

Part 1: Watch a ping

In your first session, start tcpdump:

sudo tcpdump -n -i enp0s1 -c 6 icmp
Part What it does
sudo Watching all the packets on an interface needs root.
-n Show addresses as numbers. Don't look up names.
-i enp0s1 The interface to watch. Use yours.
-c 6 Stop after 6 packets.
icmp A filter: only show ICMP packets, the ones ping uses.

tcpdump prints listening on enp0s1 and waits. In your second session, send three pings:

ping -c 3 8.8.8.8

Back in the first session, tcpdump shows six packets, then stops:

listening on enp0s1, link-type EN10MB (Ethernet), snapshot length 262144 bytes 10:15:02.101345 IP 192.168.64.7 > 8.8.8.8: ICMP echo request, id 3, seq 1, length 64 10:15:02.116882 IP 8.8.8.8 > 192.168.64.7: ICMP echo reply, id 3, seq 1, length 64 10:15:03.103012 IP 192.168.64.7 > 8.8.8.8: ICMP echo request, id 3, seq 2, length 64 10:15:03.118337 IP 8.8.8.8 > 192.168.64.7: ICMP echo reply, id 3, seq 2, length 64 10:15:04.104871 IP 192.168.64.7 > 8.8.8.8: ICMP echo request, id 3, seq 3, length 64 10:15:04.120190 IP 8.8.8.8 > 192.168.64.7: ICMP echo reply, id 3, seq 3, length 64 6 packets captured
You see It means
10:15:02.101345 The time the packet crossed the interface, to the millionth of a second.
192.168.64.7 > 8.8.8.8 From your server, to Google.
ICMP echo request / echo reply The ping, and its answer.
seq 1 The same number as icmp_seq in the ping output.

Subtract the request's time from the reply's time, and you get the round trip time that ping printed: about 15 ms.

Warning: Always give tcpdump a filter when you are connected with SSH. Without one, it shows your own SSH packets. Each line it prints is sent to you over SSH, which makes more SSH packets, which it prints, and the screen floods. If that happens, press Ctrl+C. To watch everything except your SSH session, use the filter not port 22.


Part 2: Watch a DNS lookup

Task 1: Watch a DNS lookup

Before ping www.washington.edu can send anything, your server has to turn the name into an IP address. It asks the DNS server you found in Lab 9.

Goal: Use tcpdump to catch the DNS lookup when you ping -c 1 www.washington.edu. Which server did your server ask? Which protocol and port did it use? What answer came back?

Hint
Solution

In the first session:

sudo tcpdump -n -i enp0s1 -c 4 port 53

In the second session:

ping -c 1 www.washington.edu

The ping itself may get no reply, because many web servers don't answer pings. That doesn't matter here. The DNS lookup happens before the ping is sent.

10:20:11.402117 IP 192.168.64.7.45021 > 192.168.64.1.53: 24180+ A? www.washington.edu. (28) 10:20:11.402233 IP 192.168.64.7.45021 > 192.168.64.1.53: 51017+ AAAA? www.washington.edu. (28) 10:20:11.418740 IP 192.168.64.1.53 > 192.168.64.7.45021: 24180 1/0/0 A 198.51.100.80 (44) 10:20:11.419012 IP 192.168.64.1.53 > 192.168.64.7.45021: 51017 0/1/0 (98)
You see It means
192.168.64.7.45021 > 192.168.64.1.53 Your server, from a random port, to the DNS server on port 53. tcpdump puts the port after the address, with a ..
A? www.washington.edu. The question: what is the IPv4 (A) address of www.washington.edu?
AAAA? www.washington.edu. The same question for an IPv6 (AAAA) address.
1/0/0 A 198.51.100.80 The answer: one IPv4 address. It matches the address ping printed.

The DNS server is the first nameserver in /etc/resolv.conf, from Lab 9: 192.168.64.1 on UTM, 10.0.2.3 on VirtualBox. Your server only asks the second one, 8.8.8.8, if the first doesn't answer.

DNS uses UDP. There is no connection to set up, just one question and one answer, so it is fast. tcpdump doesn't print UDP on these lines. It shows the DNS question and answer instead. Add -v to see proto UDP.

You look at DNS in much more detail in Lab 13. When you are finished, close your second session with exit.

Going further: Catch your own Git traffic. Run sudo tcpdump -n -i enp0s1 -c 10 port 22 and not host 192.168.64.1 in one session (use your own gateway), and git pull in your GitLab repository from Lab 10 in the other, with your agent started. The filter leaves out your own SSH sessions from your laptop. You see the TCP packets between your server and GitLab, on port 22. They are encrypted, so tcpdump can show where they go, but not what is in them.


Questions to consider

  1. Why does tcpdump need sudo, when ping doesn't?
  2. Why should you always give tcpdump a filter when you are connected with SSH?
  3. In Task 1, your server asked for an A and an AAAA address, at the same time. Why ask for both?
  4. tcpdump can show where your SSH and Git packets go, but not what is in them. Why not? What does that tell you about the DNS packets?

Troubleshooting

Q. tcpdump says "command not found".

A. Install it with sudo dnf install -y tcpdump.

Q. tcpdump floods my screen, and won't stop.

A. Press Ctrl+C. You probably left out the filter, so tcpdump is showing your own SSH session. Always add a filter, such as icmp or port 53, and -c to stop after a few packets.

Q. tcpdump says "listening on enp0s1", but shows nothing.

A. Check that you ran ping in your other session, while tcpdump was still waiting. Check the interface name after -i. If you see No such device exists, run ip -br address and use your interface name.

Q. tcpdump says "You don't have permission to capture on that device".

A. Put sudo in front of the command. Only root may watch all the packets on an interface.

If you can't solve a problem, post it in this week's Teams channel, or reach out by email for help.


End of lab