Managing Time in Linux

Managing time in Linux is a very important concept, as many applications may not work without a proper time configuration. In this post, we’ll explore the tools that help you manage time in Linux, but before jumping into the configuration steps, it’s worth taking some time exploring the different clocks and times that exist while dealing with time in Linux.

Hardware Clock and System Clock


There are two types of clocks that you need to be aware of, and these are the Hardware Clock and and the System Clock.

The hardware clock (aka. Real-Time clock) is a clock in the form of an integrated circuit, that resides in the computer motherboard and keeps track of the current time.

The thing with the hardware clock is that it is reliable, as it maintains time increment even when the system is powered off. While installing Linux, the hardware clock is set by default to universal time coordinated (UTC). Hardware clock can also be adjusted from BIOS.

Changing Hardware Clock from the BIOS

The system clock (aka. software clock) is the clock maintained by the operating system, and it keeps track of time independently from the hardware clock. During the boot, Linux sets its own system clock to the same time as the hardware clock, so it means It is also set to universal time coordinated (UTC). But after this, both clocks run independently. For instance, if the system time is changed, it is not automatically synchronized with hardware clock.

All applications running on top of Linux convert system time to local time, which is the actual time in the current time zone. For instance, while installing a Linux system, you are asked to choose your Time Zone, so that each application process in the system handles time zone conversions itself.


Commands Used for Managing Time in Linux


Different commands are used for managing time in your Linux system, the most common ones are:

  • date: For managing local time
  • hwclock: For managing hardware clock
  • timedatectl: A command that provides a complete set of tools for managing time in RHEL/CentOS 7 and Fedora 21+ based distibutions.

Using date

The date command enables you to manage the system time. Without any option, it shows the current date and time.

$ date
Sun Feb 10 21:21:42 +04 2019

I’m located in Dubai, so my local time zone is UTC+04

To show the UTC time, use the -u option

$ date -u
Sun Feb 10 17:21:46 UTC 2019

To set the current time to 20:30 use the -s option. You need to be root to change the current time

$ sudo date -s 20:30
Sun Feb 10 20:30:00 +04 2019

Using hwclock

The date command only shows or sets the software clock. To manage the hardware clock, use the hwclock command. For instance, you might want to set the hardware clock in local time and not in UTC, or may want to synchronize the hardware clock from the software clock or vice-versa.

To show the difference between hardware time and system time. The output of this command is refreshed every 10 seconds.

$ sudo hwclock -c
hw-time      system-time         freq-offset-ppm   tick
1549779033   1549818212.075245
1549779043   1549818222.100286              2504     25
1549779053   1549818232.125537              2515     25
1549779063   1549818242.155828              2686     27
1549779073   1549818252.178126              2572     26
...

If you want to set the hardware clock to have the same value as your local software clock, use the following command

$ sudo hwclock --systohc

As the Linux software clock is not always accurate, you may want sometime to synchronize the hardware time to the system clock.

$ sudo hwclock --hctosys

Using timedatectl

The timedatectl command can be found in RHEL/CentOS 7 and Fedora 21+ based distributions. It is a generic tools that provides the necessary options to manage all aspects of time in those distributions. Used without arguments, this command shows detailed information about the current time and date.

$ timedatectl
	  Local time: Sun 2019-02-10 21:57:42 +04
  Universal time: Sun 2019-02-10 17:57:42 UTC
		RTC time: Sun 2019-02-10 07:04:43
	   Time zone: Asia/Dubai (+04, +0400)
	 NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
	  DST active: n/a

Here are the options you can use to perform time operations

Option Function
status Show current time settings
set-time Set the current time
set-timezone Set the current time zone
list timezone Shows a list of all time zones
set-local-rtcsync Configure the RTC in universal time or in local time.
set-ntp Configure NTP

To change your local time zone, use the set-timezone option.

$ sudo timedatectl set-timezone Africa/Algiers

The system-config-date GUI or the tzselect CLI command can also be used for this purpose.

To set all available time zones, use the list-timezones option

$ timedatectl list-timezones

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
...

How about NTP


Network Time Protocol (NTP) is protocol to allows a computer to check its own clock automatically by comparing it to the time on another computer. It is a method of verifying and correcting your computer’s time by synchronizing it with a another system. This automatic synchronization will avoid you the pain to reconfigure your system clock each time you install or do casual maintenance on your system.

For larger organizations, using NTP might be a necessity, taking into consideration the number of services that depend on the correct time configuration and synchronization. Database replication and Kerberos authentication are some examples of these services.

Configuring NTP is two-steps procedure:

  1. Enabling NTP on your local server.
  2. Configuring your local server to synchronize time with remote available NTP servers.

Enabling NTP

The first thing to do is to check whether your server is enabled to use NTP. If not, you need to enable this option. The timedatectl is the command can be used for this purpose.

To check the NTP configuration on your server, simply type timedatectl and all information related to time, time zone, and if NTP is enabled and synchronized.

$ timedatectl

	  Local time: Tue 2019-02-05 05:00:31 +04
  Universal time: Tue 2019-02-05 01:00:31 UTC
		RTC time: Tue 2019-02-05 01:00:31
	   Time zone: Asia/Dubai (+04, +0400)
	 NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
	  DST active: n/a
	  

We can see from command output that NTP is not enabled yet. To enable it, use the set-ntp switch

$ sudo timedatectl set-ntp true

If you run timedatectl again, you can see that NTP is enable this time.

$ timedatectl
	  Local time: Tue 2019-02-05 05:32:20 +04
  Universal time: Tue 2019-02-05 01:32:20 UTC
		RTC time: Tue 2019-02-05 01:32:19
	   Time zone: Asia/Dubai (+04, +0400)
	 NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
	  DST active: n/a

This command will not only switch on NTP, but will also enable and start the chronyd service, which is the daemon in charge of synchronizing system clock with local or online NTP servers.

$ systemctl status chronyd.service

● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-02-10 20:22:04 CET; 3min 55s ago
	 Docs: man:chronyd(8)
		   man:chrony.conf(5)
  Process: 4401 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 4393 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 4397 (chronyd)
   CGroup: /system.slice/chronyd.service
		   └─4397 /usr/sbin/chronyd

Feb 10 20:22:04 server1.contoso.local systemd[1]: Starting NTP client/server...
Feb 10 20:22:04 server1.contoso.local chronyd[4397]: chronyd version 3.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH +SIGND +ASYNCDNS +IPV6 +DEBUG)
Feb 10 20:22:04 server1.contoso.local chronyd[4397]: Frequency 2.763 +/- 48.466 ppm read from /var/lib/chrony/drift
Feb 10 20:22:04 server1.contoso.local systemd[1]: Started NTP client/server.

Even though NTP is active now on your server, you still need to enable the NTP synchronization with remote NTP servers.

Configuring NTP servers

All NTP configuration is done in the /etc/chrony.conf file. In this file, you’ll add all the NTP servers that your server should contact for system time synchronization. On a RHEL/CentOS 7 server, the chrony.conf file looks like this

$ cat /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

To add an server an NTP server, simply add a line in the list of NTP servers. For instance, if your NTP server name ntp1.lab.local, the NTP file would look line this

$ cat /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
ntp1.lab.local
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
...

This was an overview on how simple it is to manage time in Linux. Other than setting the system time zone, upgrading the time zone and configuring NTP data files, there is little need to bother about time in your server. 

1 Comment

  1. Bouzeghoub Redouane

    Thank you,

    Reply

Leave a Reply to Bouzeghoub Redouane Cancel reply

Your email address will not be published. Required fields are marked *