- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
20 September 2009
I recently had a problem with my Linux server time drifting. I'm not sure of the cause, but it was easy enough to fix using the network time protocol.
The network time protocol is a means to synchronise time clocks of devices over the Internet. As well as being a convenient way to make sure your PC shows the correct time it is often used to ensure that the time of servers and routers are in synchronization. This is particularly important for database transaction logging, stock related transactions, security logging and analysis, network monitoring and other time critical applications.
NTP servers are distributed around the Internet using a hierarchical structure. The servers are given a stratum rating reflecting the accuracy of it's time keeping. A stratum 1 server is considered a primary server and these are normally directly synchronized to national time services or direct to an accurate clock (Stratum 0 device). Stratum 2 servers use stratum 1 servers for their source and then distribute the time to other servers. This continues up the chain. Time servers used to serve a small number of clients would normally synchronise from a stratum 3 or higher server, whose accuracy is adequate for most purposes.
The ntp protocol is not used to provide the local time of the machine. Instead ntp provides a consistent time source for the operating system. It is down to the operating system to determine the local time and display that to the user and pass to applications accordingly.
To update the time of a Linux system, using the network time protocol, the ntpdate command can be used. This is normally installed by default of most distributions.
The command takes a single argument as the hostname of the time server to update the time from.
For Ubuntu enter the command:
This command can be run regularly to keep the time in sync. This can be done using a startup script, a login script or via crontab. If the time needs to be updated more regularly then it may be better to use the network time protocol daemon ntpd.
ntpd is a daemon that can act as either a client or a server or both. I am only looking at the client side, but it could be useful to have a local ntp server that all local machines connect to. In client mode the daemon will check the time servers on a regular basis and correct the time accordingly. It does this by maintaining a driftfile measuring the difference between the ntp time and the local time.
The ntp software needs to be installed. On debian based systems (eg. Ubuntu) this can be done using:
sudo apt-get install ntp
The software will be installed and the daemon started.
The configuration file is normally
If using ntp as a client then the only configuration needed (if at all) is to add server entries pointing at appropriate ntp servers.
# You do need to talk to an NTP server or two (or three). server ntp.ubuntu.com server uk.pool.ntp.org
In the above example the Ubuntu server was included automatically, but I have also added the UK NTP pool server address*, which is a pool of ntp servers on the Internet available for public use.
* Actually this is a round-robin dns entry, so can route to one of many servers rather than a single server.