Understanding Linux System Time

Archwiki explains there are four parts when determining time:

  • Time value
  • Whether is local time, UTC, or something else
  • time zone
  • Daylight Saving Time (DST)

Two clocks are used on the system:

  • A hardware clock
  • A system clock

Standard behavior for most operating systems:

  • Set the system clock from hardware clock on boot
  • Keep accurate time of the system clock
  • On shutdown, set hardware clock from the system clock

Okay, once they made it clear for us, let’s dive into each of these steps and keywords.

Understanding Linux Time Concepts

Keeping time correct on all systems is crucial. Many elements depend on accurate time: remote services expect accurate client times, maintaining log message time stamps to properly invest client/server issues…

  • Localtime – self explanatory
  • Coordinated Universal Time (UTC) – UTC is a time that does not change according to individual time zones. Meaning, we can see I will contact you at 10:00 UTC time tomorrow. The other person will wait for 10:00 UTC in their timezone, wherever they are.
  • Hardware clock – This clock attempts to maintain correct time even when system is powered down. To do this, hardware clock uses power from CMOS battery. When system boots up, Linux pulls time from the hardware clock and updates its software clock.
  • Software clock – This clock runs only when system is running. When you shut down the system, software clock saves time to hardware clock. Then, hardware clock keeps ticking until you power up the machine again. Very cool process.

Viewing and Setting Time

We can view and change time on both hardware and software clocks. Let’s discuss three utilities that can do those things:

The hwclock utility

The command used to set hardware clock is called hwclock. Here are most used command options to consider:

–localtimeSet hardware clock to use localtime standard
–utcSet hardware clock to use UTC standard
–showDisplay current hardware clock time
–hctosysSet software clock based on time of the hardware clock (hc to sys)
–systohcSet hardware clock based on time of the software clock (sys to hc)

The date Utility

The command used to set and view software clock is called date utility (timedatectl can also change software clock). The date command will not be able to set system time if we are using Network Time Protocol (NTP) or Simple Network Time Protocol (SNTP). To check if we are running one of those services use systemctl status ntpd, systemctl status chronyd, systemctl status systemd-timesyncd. If any of these services is active, date will not be able to set software clock. To specify software clock with date command use the following, where 12 is month, 24 is day, 00 is hour, and 59 is minute:

# date 12240059

The timedatectl Utility

This is preferred method to set software clock. This is output from base command:

[aldin@arch tor-browser_en-US]$ timedatectl
               Local time: Wed 2020-10-21 19:06:25 CEST 
           Universal time: Wed 2020-10-21 17:06:25 UTC  
                 RTC time: Wed 2020-10-21 17:06:24      
                Time zone: Europe/Belgrade (CEST, +0200)
System clock synchronized: no                           
              NTP service: inactive                     
          RTC in local TZ: no 

To change software clock, use the following format “timedatectl set-time “YYYY-MM-DD HH:MM:SS”.

Understanding Network Time Protocol (NTP)

NTP is used to synchronize clocks over a network in order to provide accurate time for systems, routers, servers, and clients. To accomplish this, NTP uses clock stratum scheme. Clock stratum scheme provides layered approach, layered from 0 to 15, to access correct time sources. All devices starting at stratum 0 are highly accurate time-keeping hardware devices. Devices with stratum 1 have computers that are directly connected to stratum 0 devices. The stratum 2 devices use NTP software that allows them to pull time data served by stratum 1 devices.

Stratum 0 devices have the most accurate time, stratum 1 devices are falling behind a bit (milliseconds, or even nanoseconds). Because network travel time may take some milliseconds, time becomes less accurate for devices in lower stratum hierarchy. However, NTP tries to give time data packets a priority and allow then to travel faster in a network (between NTP server stratum 1 and NTP servers stratum 2). Another way to improve accuracy, is to give a little offset to time data so when it arrives on the system it is accurate as stratum 1.

Most popular NTP server is cluster of servers that work together in a pool (pool.ntp.org). To use this pool, when configuring NTP client, enter pool.ntp.org as your NTP server. Inside this pool, there are sub-pools, for distributions such is debian.pool.ntp.org, or even pools close to your geographical location for faster data transfer.

To use NTP client program, there are two choices: NTP Daemon (ntpd) or Chrony Daemon (chronyd)

Using NTP Daemon

Primary configuration file is in /etc/ntp.conf. The directive name for setting a server is “server“. Let’s take a look what’s inside:

[aldin@arch tor-browser_en-US]$ cat /etc/ntp.conf 
[SNIP]
# Associate to Arch's NTP pool
server 0.arch.pool.ntp.org
server 1.arch.pool.ntp.org
server 2.arch.pool.ntp.org
server 3.arch.pool.ntp.org

# By default, the server allows:
# - all queries from the local host
# - only time queries from remote hosts, protected by rate limiting and kod
restrict default kod limited nomodify nopeer noquery notrap
restrict 127.0.0.1
restrict ::1

# Location of drift file
driftfile /var/lib/ntp/ntp.drift

The server directive sets servers from a pool. Each server needs to be ordered from 0 to X. The iburst directive helps to speed up the time synchronization. On older systems, if system time is late for 17+ minutes, NTP server would not talk to that system. To get around this, you have to manually update system time, and then servers from /etc/ntp.conf would synchronize time. The ntpdate command is used manually update system time. Here’s an example:

# ntpdate 0.pool.ntp.org
# systemctl start ntpd
# ntpstat
# ntpq -p

Once the system clock is synchronized with NTP, update hardware clock with hw –systohc

Using chrony Daemon

The chronyd can keep accurate time even on busy networks or on systems that are down for periods of time, or even on virtual machines. Also, chronyd will synchronize clock faster than ntpd, and is easily configured to act as local time server.

Primary configuration file for chronyd is at /etc/chrony.conf or inside /etc/chrony/ directory. For chronyd, directive to set a server is “pool” or “server“. To set a single server, use “server” directive, whereas to set pool of servers, use “pool” directive. The contents of /etc/chrony.conf are almost identical as /etc/ntpd.conf. Also, inside configuration file, you can set rtcsync directive, which will update hardware clock for you periodically (hw –systohc). To look at time servers, use chronyc sources -v command:

# chronyc sources -v

To view time server statistics, use chronyc sourcestats:

# chronyc sourcestats

To view software clock information, use chronyc tracking command:

# chronyc tracking

Leave a Reply