Ubuntu Time Management

This page gives useful information about the way that Ubuntu manages time by changing the timezone, setting up synchronization using Network Time Protocol (NTP), and adjusting servers.

Changing the Time Zone

This section shows you how to accomplish this task using either the Graphical User Interface (GUI) or by using the command line (terminal).

Using the GUI

Perhaps the most user friendly way to change the time zone of your Ubuntu system is to use the Graphical User Interface (GUI).

  1. Click on the System menu on the top panel, go into the Administration sub-menu, and click on Time and Date.

  2. Click on the currently selected time zone to bring up the selection map.
    • ntpgui5.png

  3. Click in the general area of your location on the time zone map, the map will zoom in.
    • ntpgui7.png

  4. Select the closest city then choose Close

    • ntpgui8.png

Using the Command Line (terminal)

Using the command line, you can use dpkg-reconfigure tzdata.

  1. Open a terminal window by going to Applications>Accessories>Terminal

  2. dpkg-reconfigure tzdata

  3. Follow the directions in the terminal.

Using the Command Line (unattended)

Find out the long description for the timezone you want to configure. Save this name to /etc/timezone and run sudo dpkg-reconfigure --frontend noninteractive tzdata:

$ echo "Australia/Adelaide" | sudo tee /etc/timezone
Australia/Adelaide
$ sudo dpkg-reconfigure --frontend noninteractive tzdata

Current default time zone: 'Australia/Adelaide'
Local time is now:      Sat May  8 21:19:24 CST 2010.
Universal Time is now:  Sat May  8 11:49:24 UTC 2010.

This can be scripted if required.

Time Synchronization using NTP

This page describes methods for automatically keeping your computer's time accurate. This is extremely useful for servers, and is also recommended for desktop machines.

Network Time Protocol (NTP) is a UDP/IP protocol for synchronizing time over a network connection. Basically a client requests the current time from a server, and then uses the servers response to set its own clock.

Beyond this simple description, there is a lot of complexity. There are multiple tiers of NTP servers, with the tier one NTP servers connected to atomic clocks (often via GPS), and tier two and three servers spreading the load of actually handling requests across the internet. The client software is a lot more complex than you might think as it has to factor communication delays, and adjust the time in a way that does not affect the other processes that run on the system. Luckily all that complexity is hidden from the user.

Using the GUI

This is how to enable NTP time synchronization using the GUI (Graphical User Interface).

  1. Click on the System menu on the top panel, go into the Administration sub-menu, and click on Date and Time.

  2. Select the configuration option Keep synchronized with Internet servers

    • ntpgui2.png

  3. You may get a dialog box informing you that NTP support has to be installed.
    • ntpgui3.png

  4. After selecting Install NTP Support it will download the needed files.

    • ntpgui4.png

  5. You are now finished and you can click Close or you can customize the NTP servers (default is ntp.ubuntu.com) that your computer uses to synchronize with by choosing Select Servers.

    • ntpgui5.png

Command Line ntpdate

Ubuntu comes with ntpdate as standard, and will run it once at boot time to set up your time according to Ubuntu's NTP server. However, a system's clock is likely to drift considerably between reboots if the time between reboots is long. In that case it makes sense to correct the time occasionally. The easiest way to do this is to get cron to run it every day. With your favorite editor, create (needs sudo) a file /etc/cron.daily/ntpdate containing:

 #!/bin/sh
 ntpdate ntp.ubuntu.com

Make sure that you make this new file executable:

sudo chmod 755 /etc/cron.daily/ntpdate

Command Line ntpd

ntpdate is a bit of a blunt instrument as it can only adjust the time once a day in one big correction. The ntp daemon ntpd is far more subtle as it calculates the drift of your system clock and continuously adjusts it in small increments. Using ntpd there are no large corrections that could lead to inconsistent logs for instance. The cost is a little processing power and memory, but for a modern system this is negligible.

To setup ntpd in versions of Ubuntu 7.04 and higher

sudo apt-get install ntp

To set up ntpd pre Ubuntu 7.04:

sudo apt-get install ntp-simple

Changing NTP Time Servers

In both cases above, your system will use Ubuntu's NTP server at ntp.ubuntu.com by default. This is OK, but you might want to use several servers to increase accuracy and resilience, and you may want to use time servers that are geographically closer to you.

Using the GUI

  1. Click on the System menu on the top panel, go into the Administration sub-menu, and click on Date and Time.

  2. You may have to enter your password to continue.
    • ntpgui1.png

  3. Select Configure Servers

    • ntpgui5.png

  4. Enable the box next to your choice of server(s) or add your own.
    • ntpgui6.png

ntpdate

  • Change the contents of /etc/cron.daily/ntpdate to:

ntpdate ntp.ubuntu.com pool.ntp.org

ntpd

  • Change the contents of /etc/ntp.conf to include additional server lines:

server ntp.ubuntu.com
server pool.ntp.org

You may notice pool.ntp.org in the examples above. This is a really good idea which uses round-robin DNS to return an NTP server from a pool, spreading the load between several different servers. Even better, they have pools for different regions - I am in New Zealand, so I use nz.pool.ntp.org instead of pool.ntp.org. Look at http://www.pool.ntp.org/ for more details.

You can also Google for NTP servers in your region, and add these to your configuration. To test that a server works, just type sudo ntpdate ntp.server.name and see what happens.

Multiple Boot Systems Time Conflicts

Operating systems store and retrieve the time in the hardware clock located on your motherboard so that it can keep track of the time even when the system does not have power. Most operating systems (Linux/Unix/Mac) store the time on the hardware clock as UTC by default, though some systems (notably Microsoft Windows) store the time on the hardware clock as the 'local' time. This causes problems in a dual boot system if both systems view the hardware clock differently.

The advantage of having the hardware clock as UTC is that you don't need to change the hardware clock when moving between timezones or when Daylight Savings Time (DST) begins or ends as UTC does not have DST or timezone offsets.

Changing Linux to use local time is easier and more reliable than changing Windows to use UTC, so dual-boot Linux/Windows systems tend to use local time.

Since Intrepid (8.10), UTC=yes is default.

Make Windows use UTC

Note: This method was not initially supported on Windows Vista and Server 2008, but came back with Vista SP2, Windows 7 and Server 2008 R2.

To make MS Windows calculate the time from the hardware clock as UTC.

  • Create a file named WindowsTimeFixUTC.reg with the following contents and then double click on it to merge the contents with the registry:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
     "RealTimeIsUniversal"=dword:00000001

Reversing the change

You can create a file with the following contents and then double-click it to merge in the original changes, as above:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
     "RealTimeIsUniversal"=dword:00000000

or you can enter Registry Editor with

regedit

and navigate down to:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\

and delete RealTimeIsUniversal by right-clicking it and deleting it.

Make Linux use 'Local' time

To tell your Ubuntu system that the hardware clock is set to 'local' time:

  1. edit /etc/default/rcS
  2. add or change the following section
    # Set UTC=yes if your hardware clock is set to UTC (GMT)
    UTC=no

Troubleshooting

Is NTP installed?

Run dpkg-query --list ntp\* and look for ntp-simple (pre 7.04) or ntp. The ntpdate and ntp-doc packages may also be useful.

Which configuration file is it using?

By default NTP uses /etc/ntp.conf. If /etc/ntp.conf.dhcp exists then the NTP daemon assumes you're using DHCP to redefine the NTP settings and it uses that file instead.

How do I configure NTP to use DHCP

Your DHCP server must be configured to supply NTP servers (man dhcpd.conf may be helpful).

Your client must be configured to use the supplied NTP servers. To do this, add ntp-servers to the request (or even require) line in /etc/dhcp3/dhclient.conf. You may wish to use the default and prepend features as well to provide default/additional NTP servers. Please see man dhclient.conf and man dhcp-options for details.

Is NTP running?

NTP most commonly 'fails' if it cannot resolve the servers in its configuration file at startup (this can happen if DNS isn't available during boot). Also, if NTP is unable to keep the local clock reasonably accurate it gives up and terminates (see below).

Try sudo /etc/init.d/ntp status to check if NTP is running, and sudo /etc/init.d/ntp restart to restart it if necessary.

I have various systems on unreliable networks so I use a simple script to bounce NTP if needed:

$ cat /etc/cron.hourly/BOUNCE-NTP
#!/bin/sh

/usr/bin/ntpq -np | /bin/grep '^\*' > /dev/null || ( /usr/bin/ntpq -np ; /etc/init.d/ntp restart )

Which servers are you using?

Run grep ^server /etc/ntp.conf or grep ^server /etc/ntp.conf.dhcp to figure out which servers you're trying to use

If your network or ISP have an NTP server they should be listed here. Failing that, try ntp.ubuntu.com and/or the NTP pool servers (http://www.pool.ntp.org/). 3 or 4 servers is plenty, and don't use a server without permission (see http://en.wikipedia.org/wiki/NTP_vandalism).

Can these servers be resolved?

If host $servername fails then you may have DNS issues that need to be fixed ($servername is the name of a server listed in your configuration file, see above).

Can these servers be reached?

If ntptrace $servername fails then you may have routing/firewall issues that need to be fixed ($servername is the name of a server listed in your configuration file, see above). NTP uses UDP port 123, make sure these packets can are properly NAT-ed through your gateway if necessary.

What's NTP up to?

ntpq --numeric --peers

--numeric removes the DNS lookups. You can leave it off, but don't be surprised if the resolved names don't match names in the ntp.conf file.

Ideally you want a * and a few +'s in the fist column and a reach of 377 (see http://www.ntp.org/ntpfaq/NTP-s-trouble.htm#Q-MON-REACH)

delay is in milliseconds. It should be < 1 for local network servers, < 10 for ISP servers over DSL and ideally < 100 for wireless. offset is in milliseconds and is the current best guess of the time difference between your system and the server. The smaller the better! jitter is an estimate the the local clock frequency error. The smaller the better. If it's consistently high then your system may be drifting (see below)

Why does NTP keep resetting/failing?

NTP attempts to fix your local clock to keep accurate time. If your local clock drifts too fast (usually HW problems or IRQ lockups or somesuch) then NTP either keeps resetting your clock or gives up and terminates. Fix the drift problem and NTP will behave.


UbuntuTime (last edited 2012-12-25 18:32:41 by rarkenin)