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.

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

To show the UTC time, use the -u option

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

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.

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

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

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.

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.

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

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.

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

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

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.

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

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

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. 

Find this post interesting. Share it!

1 Comment

  1. Bouzeghoub Redouane

    Thank you,

    Reply

Leave a Comment

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