Blog tvlooy

Keeping Time with NTP

IBM i, Linux, Windows | June 25, 2011

This article was published at IT Jungle.

The Network Time Protocol (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. NTP uses UDP port 123 as its transport layer. It is designed particularly to resist the effects of variable latency (jitter buffer).

The Network Time Protocol (NTP) version 3 is described in RFC1305 and the Simple Network Time Protocol (SNTP) version 4 is described in RFC2030. NTP is not related to the much simpler daytime RFC867 and time RFC868 protocols.

NTP client/server on i5/OS

You can validate the current time with the command DSPSYSVAL QDATETIME. The NTP server on the system will redistribute the time shown there. Setting up an NTP client and server is simple; use the CHGNTPA (Change SNTP Attributes) command. i5/OS implements the SNTP protocol.

The "remote system" parameter for the NTP client must be set to one or more servers, or to a server pool. I usually set this parameter to pool.ntp.org on my local NTP server. Local clients have the hostname of my local NTP server configured there. If you want to run the client, set Client autostart to *YES. If you want to run the server as well, set Server autostart to *YES. There is a nice parameter, Synchronization required, that restricts the server to serve time when the client is out of sync. Last, you can set the client and server logging level with the Client activity log and Server activity log parameters. Activate the configured values with an IPL or the following command:

STRTCPSVR SERVER(*NTP) NTPSRV(*CFGFILE)

To stop the NTP client and server, use this command:

ENDTCPSVR SERVER(*NTP)

Depending on the configured logging level, your logs will be more or less verbose. Use the following command to find these logs on the IFS:

WRKLNK '/QIBM/USERDATA/OS400/TCPIP/NTP'

Here's an example of the client with ACTLOG(*POLL):

SNTP Client Activity Log QTOTNTP/QNTP/002415 02/05/08 22:25:54.306 TCP9136 SNTP Client started. TCP9146 Using time server pool.ntp.org. TCP9146 Using time server pool.ntp.org. TCP9162 02/05/08 22:25:55.174 Time remaining for adjustment is 0.000 seconds. TCP9116 02/05/08 22:25:55.174 NTP server UTC time is 02/05/08 20:25:55.213. TCP9117 02/05/08 22:25:55.174 Client clock UTC time is 02/05/08 20:25:55.174. TCP9120 02/05/08 22:25:55.174 Client clock adjusted = 1 (0 = not adjusted, 1 = adjusted)

An example of the server with SRVACTLOG(*ALL):

SNTP Server Activity Log QTOTNTP/QNTP/020258 03/05/08 12:44:53.906 TCP9159 SNTP Server started. TCP9161 03/05/08 15:35:52.048 Client 10.1.2.30, Unsynchronized status returned. TCP9162 03/05/08 15:35:52.048 Time remaining for adjustment is 0.000 seconds. TCP9161 03/05/08 15:37:02.973 Client 10.10.1.35, Unsynchronized status returned. TCP9162 03/05/08 15:37:02.973 Time remaining for adjustment is 0.000 seconds.

As you would expect, you can view the NTP jobs from a CL command line:

WRKUSRJOB USER(QNTP)

NTP client on the HMC

The Hardware Management Console (HMC) is actually just a stripped down Linux box, and can be configured on a restricted shell. You can access the shell locally from the window manager (fluxbox) menu, or remotely with SSH (if you set it up). The current time can be printed with the date command.

NTP is configured in the files /etc/sysconfig/xntp and /etc/ntp.conf. Luckily, there are some commands available that form an interface over these configuration files, namely chhmc and lshmc. The system has manual (man) pages available for these commands. Use them if you want to know the exact scope of a command used below. The NTP client used on the HMC is the implementation of ntp.org, packaged by S.u.S.E. (now Novell). The x in xntp was intended as experimental, but after a decade of code it was a bit inappropriate so they dropped it for NTPv4. So in practice, xntp refers to an implementation of version three.

To show if the client is running, use the command lshmc -r -F xntp. The system will respond with enable or disable.

The HMC is protected by a firewall. If you want to use the NTP server on your network, you have to open up the port used by NTP on the correct network interface. The HMC usually has three interfaces (eth0, eth1 and eth2). From within WebSM, you can look up which interface will be used by NTP (the one that's connected to the local network). Try to ping your local NTP server over this interface to be sure you are configuring the correct one. For example if the interface is eth2 and the IP address of the NTP server is 10.10.1.34: ping -I eth2 10.10.1.34

Now you can configure the NTP client with the chhmc command: chhmc -c xntp -s add -a 10.10.1.34 -i eth2. The -s flag also accepts remove instead of add to remove a server. There is also a -h flag that accepts a hostname instead of an IP address. You only have to specify -h or -a, not both of them. It's possible to validate configuration changes directly in the already mentioned configuration files.

The last step is to enable the NTP client. This can be done with the command chhmc -c xntp -s enable. The server logs will show the client startup, an example of grep ntp /var/log/messages:

May 02 09:37:21 hmc1 ntpdate[15903]: adjust time server 10.10.1.34 offset 0.071392 sec May 02 09:37:21 hmc1 ntpd[15912]: ntpd 4.2.0a@1.1213-r Fri May 02 13:44:28 UTC 2006 (1) May 02 09:37:21 hmc1 ntpd[15912]: precision = 3.000 usec May 02 09:37:21 hmc1 ntpd[15912]: Listening on interface wildcard, 0.0.0.0#123 May 02 09:37:21 hmc1 ntpd[15912]: Listening on interface lo, 127.0.0.1#123 May 02 09:37:21 hmc1 ntpd[15912]: Listening on interface eth0, 9.6.24.1#123 May 02 09:37:21 hmc1 ntpd[15912]: Listening on interface eth2, 10.10.1.33#123 May 02 09:37:21 hmc1 ntpd[15912]: Listening on interface sl0, 10.253.0.1#123 May 02 09:37:21 hmc1 ntpd[15912]: kernel time sync status 0040 May 02 09:37:21 hmc1 ntpd[15912]: frequency initialized 2.643 PPM from /var/lib/ntp/drift/ntp.drift

As far as I know, it is impossible to run an NTP server on the HMC.

NTP Client on Windows

Windows also has an implementation of the SNTP protocol. It is possible to configure the client from the Windows GUI using the date and time properties. I was told it's easy to configure the NTP server for all clients on your network using active directory. As I don't use active directory, I added a command to my Samba login script, namely net time /SETSNTP:ntp1.example.com,0x1

You can also push the server into the registry with the command:

reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /v 0 /t REG_SZ /d ntp1.example.com /f

To verify that you are using the correct time server, use the command net time /querysntp. To manually resync with the server, use the command w32tm /resync. There are a handful of other w32tm commands, but I've never needed them.

The clients on my network all make use of the dynamic host configuration protocol (DHCP). DHCP is defined in RFC2131 and can make use of DHCP options, defined in RFC1533. There is an option 42, which sends network time protocol servers . Because DHCP supports sending NTP servers, I tried to configure this on my Windows client. The client does not request option 42 by default. On NT4 you could add this option to the registry:

HKLM\SYSTEM\CurrentControlSet\Services\Dhcp\Parameters\Options

But, on XP this doesn't seem to work anymore.

NTP Client on *nix

Manually setting up a NTP server/client on Linux/*BSD is a matter of configuring /etc/ntpd.conf. Some distributions, such as OpenSUSE, will make this even easier by providing a graphical frontend on the configuration files (YaST).

But, I also tried to use the DHCP option with the ISC DHCP client, which many Linux distributions use. All you need to do is add a request for ntp-servers to the /etc/dhclient.conf and extend the dhclient-script with the function below. Call it somewhere in BOUND|RENEW|REBIND|REBOOT. I only do so on $reason REBOOT:

add_new_ntp() { if [ -n "$new_ntp_servers" ]; then perl -i -ne 'print unless /^server/' /etc/ntpd.conf for address in $new_ntp_servers; do echo "server" $address >> /etc/ntpd.conf done pkill -KILL ntpd && ntpd fi }

In this case I used OpenBSD and its NTP client, so configuring ntpd.conf and restarting ntpd can be a bit different on a Linux distribution using the client of ntp.org. Here's a piece of tcpdump output showing the REQ/ACK of option 42(look for NTP):

15:51:15.261777 192.168.1.79.bootpc > 255.255.255.255.bootps: xid:0xdc015522 vend-rfc1048 HN:"glenda" RQ:192.168.1.79 DHCP:REQUEST PR:SM+BR+DG+DN+NS+HN+NTP [tos 0x10]

15:51:15.501148 192.168.1.1.bootps > 192.168.1.79.bootpc: xid:0xdc015522 Y:192.168.1.79 S:192.168.1.1 vend-rfc1048 DHCP:ACK SID:192.168.1.1 LT:43200 SM:255.255.255.0 BR:192.168.1.255 DG:192.168.1.1 DN:"example.com" NS:ns1.example.com NTP:192.168.1.3 [tos 0x10]