Configuring the Linux clock

A problem often reported by users in Linux forums is that the time displayed by the system clock is incorrect. Typically the problem is that it displays UTC instead of local time. The most common cause is incorrect configuration, although dual booting with Windows will also change the Linux system clock’s time unless either a) Linux is configured to use local time for the hardware clock, or b) Windows is configured to use UTC* and Windows’ time synchronisation with an Internet time server is disabled.

* A change to the Windows Registry is required in order to enable Windows to use UTC for hardware clock time, but even then you may still face problems (see e.g. Force Windows 8 to use UTC when dealing with BIOS clock).

Some terms you need to know: ‘hardware clock’, ‘RTC’ (real-time clock), ‘CMOS clock’ and ‘BIOS clock’ are terms commonly used for the same thing. The hardware clock is non-volatile. ‘system clock’ is the software clock the operating system uses. It is volatile (it is in the kernel). The system clock is normally set from the hardware clock when the operating system boots, taking into account various configuration options. I recommend that you read the manual for the hwclock command, as it explains in detail about clocks in Linux:

# man hwclock

To configure your time zone and clocks correctly, follow the steps below.

If your installation uses the OpenRC init system

  1. Set the time zone:

    1. Specify your time zone in the file /etc/timezone. If you are in Spain, for example, the contents of that file should be ‘Europe/Madrid‘ (without the quotes).

      # nano /etc/timezone
      

      You can find out which time zones are available by looking in the directory /usr/share/zoneinfo/ and its sub-directories.

    2. Provide the information for your time zone. If you are in Spain, for example, you would enter the following command:

      # cp /usr/share/zoneinfo/Europe/Madrid /etc/localtime
      

      Note: Some people recommend making /etc/localtime a symlink rather than creating it by copying a file from /usr/share/zoneinfo/, however I recommend copying. You should definitely not use a symlink if /usr is on a different partition (see Ref. 1).

  2. Set the system clock’s time to your local time now (I’ll assume it is 23:54:30 on 21 September 2014 when you press Enter):

    # date 092123542014.30
    
  3. Specify whether the hardware clock time is UTC:

    # hwclock --utc
    

    or local time:

    # hwclock --localtime
    

    It is preferable for the hardware clock time to be in UTC. However, if you dual boot with Windows it is recommended for the hardware clock time to be in local time rather than UTC unless both the following are true: a) the Windows Registry has been configured for hardware clock time in UTC; b) Windows’ time synchronisation with an Internet time server is disabled.

  4. Set the hardware clock’s time by specifying your local time (I’ll assume it is 23:54:50 on 21 September 2014 when you press Enter):

    # rm /etc/adjtime
    # hwclock --set --date="2014-09-21 23:54:50"
    

    Note: You must specify local time in the above command, even if you intend the hardware clock time to be UTC.

    Note that the third line in /etc/adjtime should be ‘UTC’ if the hardware clock time is in UTC, or ‘LOCAL’ if the hardware clock time is in local time. For example:

    # cat /etc/adjtime
    0.000000 1412038530 0.000000
    1412038530
    UTC
    
  5. Tell OpenRC whether the hardware clock uses UTC or local time, and to update the hardware clock time from the system clock time at every shutdown (taking into account any time zone difference between the two clocks):

    # nano /etc/conf.d/hwclock
    
    1. Make clock="UTC" (or clock="local" if you dual boot with Windows and the Windows Registry is not configured to use UTC for hardware clock time).
    2. Make clock_systohc="YES" to adjust the hardware clock time at shutdown based on the software clock time (taking time zone into account). However, you don’t need to bother doing this if your kernel configuration has CONFIG_RTC_SYSTOHC=y and your installation is configured to synchronise the system clock with a NTP time server (see my post Synchronise your Gentoo Linux clock with an Internet time server), i.e. make clock_systohc="NO" if CONFIG_RTC_SYSTOHC=y. You can check if clock_systohc="YES" is causing the hardware clock time to be updated from the software clock time (taking time zone into account) at shutdown by looking at the kernel messages displayed when in verbose mode during shutdown to see if the following message is displayed:

      Setting hardware clock using the system clock [UTC] ...
      
    3. Make clock_hctosys="YES" to adjust the system clock time at start up based on the hardware clock time (taking time zone into account). However, you don’t need to bother doing this if your kernel configuration has CONFIG_RTC_HCTOSYS=y, i.e. make clock_hctosys="NO" if CONFIG_RTC_HCTOSYS=y. You can check if clock_hctosys="YES" is causing the system clock to be updated from the hardware clock at boot by looking at the kernel messages displayed when in verbose mode during boot to see if the following message is displayed:

      Setting system clock using the hardware clock [UTC] ...
      
  6. Reboot and check everything is working as expected:

    # date
    Tue 30 Sep 01:55:55 BST 2014
    # hwclock
    Tue 30 Sep 2014 01:55:59 BST  -0.031877 seconds
    

    The hwclock command always shows local time, even if you keep your hardware clock in UTC.

  7. Optionally (but I recommend it), configure your installation to synchronise the system clock with an Internet NTP server (see my post Synchronise your Gentoo Linux clock with an Internet time server).

If your installation uses systemd

  1. Set the time zone:

    1. Find out the available time zones:

      # timedatectl list-timezones
      
    2. Specify your time zone (I’ll assume you are in Spain in this example):

      # timedatectl set-timezone Europe/Madrid
      
  2. Set the system clock time to your local time (I’ll assume it is 23:54:30 on 21 September 2014 when you press Enter):

    # timedatectl set-time "2014-09-21 23:54:30"
    
  3. Inform systemd that the hardware clock uses UTC, and set the current UTC in the hardware clock now:

    # timedatectl set-local-rtc 0
    

    Note that, by default, systemd assumes the hardware clock uses UTC. Although systemd can be configured to assume the hardware clock uses local time, for technical reasons it is not recommended (see man timedatectl and Arch Wiki article Time for an explanation of why this is not advisable).

    Note that the third line in /etc/adjtime should be ‘UTC’ if the hardware clock time is in UTC, or ‘LOCAL’ if the hardware clock time is in local time. For example:

    # cat /etc/adjtime
    0.000000 1412038530 0.000000
    1412038530
    UTC
    
  4. Reboot and check everything is working as expected:

    $ timedatectl
    
  5. Optionally (but I recommend it), configure your installation to synchronise the system clock with an Internet NTP server (see my post Synchronising the clock using NTP in Sabayon Linux).

References

[1] Gentoo Forums – how exactly to set timezone in /etc/localtime ?[solved]

About Fitzcarraldo
A Linux user with an interest in all things technical.

4 Responses to Configuring the Linux clock

  1. Pingback: My thoughts on systemd | Fitzcarraldo's Blog

  2. Pingback: Using a keyboard shortcut in Linux to add an e-mail signature giving current location and local time | Fitzcarraldo's Blog

  3. ilnanny says:

    Thank you so much for your post. After 10 years my computer broke down and I had to install gentoo on a secondary (temporary) PC where a microsoft operating system is already installed, this made it all difficult for me because the configuration of the clock was always wrong. this error I set to local in /etc/conf.d/clock, without solving the problem. But with ntp I solved.
    (usually I’ve always used cron)
    Anyway thanks and sorry for my bad english.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.