NTP exercises

For this experiment, you will need two hosts on GENI.

Exercise: date command

On Linux, the date command is used to display and change the system time of a host. Use

man date

to see the manual page for this utility, and study its options and usages.

Try the following date commands:

date
date --date='2 days ago'
date --date='3 months 2 days' 
sudo date --set='+1 seconds'
date -r /tmp/date.txt

Exercise: using NTP to find time offset

NTP can be used to determine the time offset between an NTP client and server as follows. First, the NTP client sends a packet containing its own timestamp to the server in the “Transmit Timestamp” field of an NTP packet. When the NTP server receives the packet, it responds with an NTP packet where the

When the client receives the reply, it computes four time values:

Using these time fields, the client will compute the round-trip transmission delay between the NTP client and server, as

δ = (T4 – T1) – (T3 – T2)

and the offset of the client clock from the server clock as

θ = 1/2 [(T2 – T1) + (T3 – T4)]

Illustration of NTP computation

On Linux, the ntpdate command can be used to query an NTP server and to set the system time using NTP. Use

man ntpdate

to see the manual page for this utility, and study its options and usages.

You are going to use ntpdate on one of your clients to query a pool of NTP servers on the Internet. Identify the name of the network interface that is used to reach the Internet, and use tcpdump to capture NTP traffic on that interface -

If you are still using the resources from the “Basic home gateway services: DHCP, DNS, NAT”, the client nodes are configured to reach the Internet via the gateway using eth1, so run:

sudo tcpdump -i eth1 'udp port 123' -w ntp-$(hostname -s).pcap

If you are using a “new” resource that connects to the Internet via eth0, run:

sudo tcpdump -i eth0 'udp port 123' -w ntp-$(hostname -s).pcap

to capture NTP traffic from the experiment network.

Then, while tcpdump is running, open another SSH session to the same client node, and run

ntpdate -p 1 -q pool.ntp.org

to query the NTP server pool at pool.ntp.org (over the public Internet), requesting one sample from each time server.

Save the response for your lab report.

Note: a response line with

server X, stratum 0, offset 0.000000, delay 0.00000

in the ntpdate output indicates that there was no response from the server X. This line of output should not be used in any computations.

Stop the tcpdump and transfer the packet capture to your laptop with scp. You can also play back the packet capture with

tcpdump -tt -nv -r ntp-$(hostname -s).pcap

Note: the -tt argument to tcpdump asks tcpdump to show packet timestamps in epoch timestamp format. To convert from epoch format to NTP timestamp format, add 2208988800 seconds.

Lab report: How many NTP servers did the client receive a response from? What stratum did each server belong to, and what was the estimated network delay to each server from the client? In the last line of the ntpdate output, it reports the result from the server it considers most reliable - can you explain (using stratum and delay values) why it selects the server it did in your example?

Lab report: Select one NTP request and its associated response from your packet capture. For that NTP transaction, show the NTP request and response, and answer the following questions:

Exercise: sync time with NTP

The Network Time Protocol daemon ntpd can be used to maintain system time in sync with NTP time servers. We will use it to synchronize the two client nodes using NTP.

First, open two terminal windows, one to each client, and place them side-by-side in your display. In each, run

watch -n 0.1 timedatectl

on each host to show the output of the timedatectl command every 0.1 seconds.

On a second terminal in each host, run

sudo service ntp stop

to stop the ntp service, if it is running.

Find the time offset between the two hosts (from the output of the timedatectl command). If there is no time offset, use the date command (as in Exercise 6) to adjust the system time of one of the hosts by a few seconds, so that there is a small offset. For example, you can shift the clock on one client with:

sudo date --set='+3 seconds'

Take a screenshot showing both windows simulataneously with the timedatectl. Annotate the screenshot to show the time offset between the two hosts.

Then, while timedatectl is running, use the second terminal on each host to run

sudo ntpd -gq -d 1 0.us.pool.ntp.org 1.us.pool.ntp.org
sudo service ntp start

on both client nodes, to sync them with the same NTP time servers and set the time using NTP.

Now, check the timedatectl output again. Take a screenshot showing both windows simultaneously, and compare the date/time offset between the two hosts.

Lab report: Show a screenshot of your two hosts’ time, side by side,