2011-06-12 // Time synchronisation with NTP on Ubuntu (and Debian)
The Network Time Protocol (NTP) is a protocol for synchronising the clocks of computers over TCP/IP networks. I don't go into detail here,1) but NTP is helpful if you need a really2) exact system time (e.g. on special Database servers) or just to get a correct clock setting on your common desktop. There are two ways to use NTP to set your system's clock:
ntpdate
– Simple NTP client, syncs your system's clock instantly by polling specified NTP server(s) every time it is called.ntpd
– NTP server daemon, calculates the drift of your system's hardware clock and continuously adjusts it. Can act as NTP server for other NTP servers and clients.
ntpdate
ntpdate
syncs your system's clock instantly. It is installed by default on Ubuntu (if not: install the ntpdate
(main) package) and the operating system will execute it once at boot time to set up your time according to the ntp.ubuntu.com
server. This is a sane and good setup for common desktops and so you normally don't have to care about your clock.
However, all common mainboard hardware clocks are working more or less imprecisely. So if your PC is running a few days without a break, the clock may drift a few seconds between reboots. If you want to sync the time of such machines automatically, setup a cronjob.
For a sync once a day:
- Open a terminal
- Create a
/etc/cron.daily/ntpdate
file with root privileges and your favorite editor, e.g.- nano:
sudo nano /etc/cron.daily/ntpdate
- gedit:
gksudo gedit /etc/cron.daily/ntpdate
- Store the following command:
ntpdate -u ntp.ubuntu.com pool.ntp.org
I added
pool.ntp.org
to increase accuracy and resilience. - Make the created file executable:
sudo chmod 755 /etc/cron.daily/ntpdate
If you got a really bad hardware clock, you may want to sync every hour:
- Open a terminal
- Create a
/etc/cron.hourly/ntpdate
file with root privileges and your favorite editor, e.g.- nano:
sudo nano /etc/cron.hourly/ntpdate
- gedit:
gksudo gedit /etc/cron.hourly/ntpdate
- Store the following command:
ntpdate -u ntp.ubuntu.com pool.ntp.org
I added
pool.ntp.org
to increase accuracy and resilience. - Make the created file executable:
sudo chmod 755 /etc/cron.hourly/ntpdate
The advantage of ntpdate
is its simplicity. No daemon is running and no configuration file is needed. The disadvantage is its lack of intelligence. Because your system's time will be changed immediately, there may be ugly side effects3) like doubled entries in logfiles of server daemons. But this should not affect a common desktop (or did you ever had any problems when changing your systems clock manually?). Please note that ntpdate
will decline work if ntpd
is running on the same host to prevent disturbing the daemon. Therefore it makes no sense to run ntpd
and define an additional ntpdate
cronjob. But you can use ntpdate
when the systems boots to get a good initial time before the daemon starts.
ntpd
The NTP daemon ntpd
is a background service. It calculates the drift of your system clock and continuously adjusts it. This prevents large corrections which could lead e.g. to inconsistent log files on servers. It remembers the clock drift and it will correct it autonomously after using it for a while, even if there is no reachable NTP server. Additionally, your machine is able to act as an NTP server for other computers.
To get ntpd
install the ntp
(main) package. Its configuration file is located at /etc/ntp.conf
. See The NTP FAQ or NTP Support to find a configuration fitting your needs.
Good NTP servers
Have a look at http://www.pool.ntp.org. This is a great project which uses round-robin DNS to return a NTP server from a pool, spreading the load between several different servers. The main, global pool is reachable via pool.ntp.org
. But you may want to choose a special pool for your region to get serves located near your location, e.g.:
north-america.pool.ntp.org
– Servers located in North Americaeurope.pool.ntp.org
– Servers located in Europede.pool.ntp.org
– Servers located in Germany
See http://www.pool.ntp.org/zone/@ for details.
Dual boot environments: my Windows/Ubuntu clock goes x hours wrong!
Unix-like systems are assuming your hardware clock is offering a UTC time by default. But MS Windows systems are assuming your hardware clock is offering a time fitting your local timezone. This may lead to problems regarding non-UTC timezones and Daylight saving time, resulting in an x hours drift on one of both systems. If you got such problems, you need to configure either Windows to assume an UTC hardware time or your Ubuntu to assume a localized hardware time:
- Windows→UTC:
Create the registry valueHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
→RealTimeIsUniversal
,dword:00000001
with administrator privileges - Debian/Ubuntu→Local time
sudo nano /etc/default/rcS
, changeUTC=yes
toUTC=no
. If you choose the “Dual Boot” installation option during your Ubuntu-Setup, Ubuntu setsUTC=no
by default.4)
Weblinks
ntpd
– are startedLeave a comment…
- E-Mail address will not be published.
- Formatting:
//italic// __underlined__
**bold**''preformatted''
- Links:
[[http://example.com]]
[[http://example.com|Link Text]] - Quotation:
> This is a quote. Don't forget the space in front of the text: "> "
- Code:
<code>This is unspecific source code</code>
<code [lang]>This is specifc [lang] code</code>
<code php><?php echo 'example'; ?></code>
Available: html, css, javascript, bash, cpp, … - Lists:
Indent your text by two spaces and use a * for
each unordered list item or a - for ordered ones.