June 3, 2012 Leave a comment
There are a number of ways to synchronise Gentoo Linux with a time server on the Internet. Here I look at a few alternatives.
ntp-client and the NTP daemon
ntpd are installed when you install the package
net-misc/ntp. Although I have read on some Web sites that
/etc/init.d/ntp-client should be added to the default runlevel in order to read the time from an NTP server (once-only, during start-up), this in fact does not work because usually the network connection is not up by the time the ntp-client initscript runs. Bear in mind that
ntp-client does not run continuously; it syncs once with an external time server if there is a network connection, and that’s it.
If you are using NetworkManager, an elegant solution is to use NetworkManagerDispatcher to restart
ntp-client in order to resync your system clock every time a network connection comes up. This is my favoured solution for laptops; see further on for how to configure your machine to do this.
Another way would be to create a cronjob to run periodically the ‘
/etc/init.d/ntp-client restart‘ command or the ‘
ntpd -q‘ command (the -q option means “set the time and quit”).
Wait a while after start up
A ‘quick-and-dirty’ method, which I have used sometimes to synchronise a laptop’s system clock every time it boots, would be to delay running
ntp-client until the network is up by putting e.g. the command below in a file
10_ntp-client.start in the directory
/etc/local.d/ (10 seconds is usually enough time for a wired or wireless connection to my home network to be established):
#!/bin/bash sleep 10s && /etc/init.d/ntp-client restart
Don’t forget to make it executable:
Regarding the NTP daemon, it is possible to configure this from the command line, rather than via a Desktop Environment GUI, to run at start-up and continue running to adjust your system clock. The command:
will add the daemon’s initscript to the default runlevel so that it is launched automatically at the next startup, and the command:
will start the daemon running right now.
Note that, by default, the NTP daemon won’t correct, all in one go, a time difference between your system clock and the remote NTP server if that difference is above a certain size. However, if you want to override the default behaviour, i.e. allow the NTP daemon to make a large first adjustment to the system clock, you can set the environment variable NTPD_OPTS in the file
/etc/conf.d/ntpd as follows:
NTPD_OPTS="-g" # The -g option enables ntpd to make large adjustments.
This would mean that you would not need to run
ntpd. However, if you run
ntp-client automatically — either once after start-up or periodically — then that would be good enough for the typical Desktop user, and could be an alternative to having a continuously-running NTP daemon. Nothing stops you doing both if you want, of course.
Updating the hardware clock
If you make
clock_systohc="YES" in the file
/etc/conf.d/hwclock then the time in the system clock will be written to the BIOS (CMOS) clock (a.k.a. hardware clock) when you shut down your PC.
How to configure NetworkManager Dispatcher to synchronise the system clock only when a network connection is made
If you’re using a machine that is permanently connected to a network, running the NTP daemon makes sense. But what if you have a machine that is not always connected to a network when it is powered up? I have a laptop and I don’t want the NTP daemon running all the time. But I would like my laptop to synchronise with an external time server once after start up when I connect to the Internet. NetworkManager has a handy tool called NetworkManager Dispatcher for doing just this.
If you have installed NetworkManager, you’ll find there is an initscript
/usr/portage/net-misc/networkmanager/files/NetworkManagerDispatcher. Copy it to the directory
/etc/init.d/ and give it the necessary restrictive permissions:
Then create a shell script called e.g.
99_ntp-client in the directory
/etc/NetworkManager/dispatcher.d/ to be run by NetworkManagerDispatcher when a network connection is established, containing the following code:
#!/bin/bash INTERFACE=$1 # The interface which is brought up or down STATUS=$2 # The new state of the interface case "$STATUS" in 'up') # $INTERFACE is up echo "System time before starting ntp-client:" > /home/fitzcarraldo/ntp-client.txt date >> /home/fitzcarraldo/ntp-client.txt echo "Starting ntp-client:" >> /home/fitzcarraldo/ntp-client.txt rc-config restart ntp-client &>> /home/fitzcarraldo/ntp-client.txt echo "System time after starting ntp-client:" >> /home/fitzcarraldo/ntp-client.txt date >> /home/fitzcarraldo/ntp-client.txt ;; 'down') # $INTERFACE is down # Check for active interface and down if no one active if [ ! `nm-tool|grep State|cut -f2 -d' '` = "connected" ]; then echo "Stopping ntp-client at:" > /home/fitzcarraldo/ntp-client.txt date >> /home/fitzcarraldo/ntp-client.txt rc-config stop ntp-client &>> /home/fitzcarraldo/ntp-client.txt fi ;; esac
Make the root user the owner of the script, and only allow the root user to write to it and execute it:
Then add NetworkManagerDispatcher to the default runlevel so that it will be launched every time you boot your machine:
As the package
net-misc/ntp installs both
/etc/init.d/net-client, users could optionally add the NTP daemon
ntpd to the default runlevel too if desired, which would provide continuous, incremental adjustments to the system clock once
net-client has done its one-shot adjustment each time a network comes up:
But users who don’t leave their PCs on for days on end — or who use laptops — can ignore the above step and just stick with the NetworkManagerDispatcher and
net-client solution, whereas users who leave their machines on for days or weeks on end can also use the NTP daemon to keep the system clock in sync in between the times when
ntp-client has synchronised.
Don’t forget to delete
ntp-client from the start-up level if you are using NetworkManagerDispatcher to run it:
Notice that the script
/etc/NetworkManager/dispatcher.d/99_ntp-client logs some information in a text file
ntp-client.txt in my home directory which I can check. Here is an example of what
ntp-client.txt contains after I select a network (or it is selected automatically) following start up of my laptop:
System time before starting ntp-client: Sun Jun 3 19:24:08 BST 2012 Starting ntp-client: Restarting init script * Setting clock via the NTP client 'ntpd' ...ntpd: time slew +0.067178s [ ok ] System time after starting ntp-client: Sun Jun 3 19:24:17 BST 2012
As you can see above, the
ntpd command was executed once by NetworkManagerDispatcher and made a small adjustment to the system time on my laptop.
Replacing ntpdate with ntpd in ntp-client
Just for the fun of it, I changed
/etc/conf.d/ntp-client to use the command
ntpd instead of
ntpdate, even though the
ntpdate command works fine. Anyway, here’s my
/etc/conf.d/ntp-client file these days:
NTPCLIENT_CMD="ntpd" NTPCLIENT_OPTS="-g -q"
I have added the -g option so that the
ntpd command can make large adjustments to the system time if it is way off the actual time. This is useful at the beginning and end of Daylight Saving Time, or if you dual boot with Windows. Here is an example of the former when I powered up my laptop the morning after the clocks changed from BST to GMT at the end of Summer 2010:
You can specify the NTP server or NTP server pool in the file
/etc/ntp.conf, but the default server pool already specified in that file should work. Note again that, when ntpd is run with the -q option, it synchronises the system clock once and terminates, i.e. it is not running as a daemon.