Bookmark this page

Maintaining Accurate Time

Objectives

After completing this section, you should be able to maintain accurate time synchronization using NTP and configure the time zone to ensure correct time stamps for events recorded by the system journal and logs.

Setting Local Clocks and Time Zones

Correct synchronized system time is critical for log file analysis across multiple systems. The Network Time Protocol (NTP) is a standard way for machines to provide and obtain correct time information on the Internet. A machine may get accurate time information from public NTP services on the Internet, such as the NTP Pool Project. A high-quality hardware clock to serve accurate time to local clients is another option.

The timedatectl command shows an overview of the current time-related system settings, including current time, time zone, and NTP synchronization settings of the system.

[user@host ~]$ timedatectl
               Local time: Fri 2019-04-05 16:10:29 CDT
           Universal time: Fri 2019-04-05 21:10:29 UTC
                 RTC time: Fri 2019-04-05 21:10:29
                Time zone: America/Chicago (CDT, -0500)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

A database of time zones is available and can be listed with the timedatectl list-timezones command.

[user@host ~]$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
...

Time zone names are based on the public time zone database that IANA maintains. Time zones are named based on continent or ocean, then typically but not always the largest city within the time zone region. For example, most of the US Mountain time zone is America/Denver.

Selecting the correct name can be non-intuitive in cases where localities inside the time zone have different daylight saving time rules. For example, in the USA, much of the state of Arizona (US Mountain time) does not have a daylight saving time adjustment at all and is in the time zone America/Phoenix.

The command tzselect is useful for identifying correct zoneinfo time zone names. It interactively prompts the user with questions about the system's location, and outputs the name of the correct time zone. It does not make any change to the time zone setting of the system.

The superuser can change the system setting to update the current time zone using the timedatectl set-timezone command. The following timedatectl command updates the current time zone to America/Phoenix.

[root@host ~]# timedatectl set-timezone America/Phoenix
[root@host ~]# timedatectl
               Local time: Fri 2019-04-05 14:12:39 MST
           Universal time: Fri 2019-04-05 21:12:39 UTC
                 RTC time: Fri 2019-04-05 21:12:39
                Time zone: America/Phoenix (MST, -0700)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Note

Should you need to use the Coordinated Universal Time (UTC) on a particular server, set its time zone to UTC. The tzselect command does not include the name of the UTC time zone. Use the timedatectl set-timezone UTC command to set the system's current time zone to UTC.

Use the timedatectl set-time command to change the system's current time. The time is specified in the "YYYY-MM-DD hh:mm:ss" format, where either date or time can be omitted. The following timedatectl command changes the time to 09:00:00.

[root@host ~]# timedatectl set-time 9:00:00
[root@host ~]# timedatectl
               Local time: Fri 2019-04-05 09:00:27 MST
           Universal time: Fri 2019-04-05 16:00:27 UTC
                 RTC time: Fri 2019-04-05 16:00:27
                Time zone: America/Phoenix (MST, -0700)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

The timedatectl set-ntp command enables or disables NTP synchronization for automatic time adjustment. The option requires either a true or false argument to turn it on or off. The following timedatectl command turns on NTP synchronization.

[root@host ~]# timedatectl set-ntp true

Note

In Red Hat Enterprise Linux 8, the timedatectl set-ntp command will adjust whether or not chronyd NTP service is operating. Other Linux distributions might use this setting to adjust a different NTP or SNTP service.

Enabling or disabling NTP using other utilities in Red Hat Enterprise Linux, such as in the graphical GNOME Settings application, also updates this setting.

Configuring and Monitoring Chronyd

The chronyd service keeps the usually-inaccurate local hardware clock (RTC) on track by synchronizing it to the configured NTP servers. If no network connectivity is available, chronyd calculates the RTC clock drift, which is recorded in the driftfile specified in the /etc/chrony.conf configuration file.

By default, the chronyd service uses servers from the NTP Pool Project for the time synchronization and does not need additional configuration. It may be useful to change the NTP servers when the machine in question is on an isolated network.

The stratum of the NTP time source determines its quality. The stratum determines the number of hops the machine is away from a high-performance reference clock. The reference clock is a stratum 0 time source. An NTP server directly attached to it is a stratum 1, while a machine synchronizing time from the NTP server is a stratum 2 time source.

The server and peer are the two categories of time sources that you can declare in the /etc/chrony.conf configuration file. The server is one stratum above the local NTP server, and the peer is at the same stratum level. More than one server and more than one peer can be specified, one per line.

The first argument of the server line is the IP address or DNS name of the NTP server. Following the server IP address or name, a series of options for the server can be listed. It is recommended to use the iburst option, because after the service starts, four measurements are taken in a short time period for a more accurate initial clock synchronization.

The following server classroom.example.com iburst line in the /etc/chrony.conf file causes the chronyd service to use the classroom.example.com NTP time source.

# Use public servers from the pool.ntp.org project.
...output omitted...
server classroom.example.com iburst
...output omitted...

After pointing chronyd to the local time source, classroom.example.com, you should restart the service.

[root@host ~]# systemctl restart chronyd

The chronyc command acts as a client to the chronyd service. After setting up NTP synchronization, you should verify that the local system is seamlessly using the NTP server to synchronize the system clock using the chronyc sources command. For more verbose output with additional explanations about the output, use the chronyc sources -v command.

[root@host ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||                                                /   xxxx = adjusted offset,
||         Log2(Polling interval) -.             |    yyyy = measured offset,
||                                  \            |    zzzz = estimated error.
||                                   |           |
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* classroom.example.com         8   6    17    23   -497ns[-7000ns] +/-  956us

The * character in the S (Source state) field indicates that the classroom.example.com server has been used as a time source and is the NTP server the machine is currently synchronized to.

References

timedatectl(1), tzselect(8), chronyd(8), chrony.conf(5), and chronyc(1) man pages

NTP Pool Project

Time Zone Database

Revision: rh124-8.2-df5a585