Site icon DesignLinux

LFCA: Learn to Manage Time and Date in Linux – Part 6

This article is Part 6 of the LFCA series, here in this part, you will acquaint yourself with the general system administration commands to manage time and date settings in the Linux system.

Time is crucial in any Linux system. Multiple services such as crontab, anacron, backup and restore services depend on accurate time to carry out their tasks as expected.

Linux has 2 types of clocks:

Usually, there exists a time difference between the two clocks such that they gradually drift from each other. We will come to this later and show you how you can sync these clocks.

For now, we will see how you can check time and date on a Linux system.

Check Time and Date On a Linux System

There are two main utilities used to check time and date on a Linux system. The first is the date command. Without any arguments, it provides quite a bit of information shown

$ date

Friday 26 March 2021 11:15:39 AM IST

To view the date in dd-mm-yy time format only, execute the command:

$ date +"%d-%m-%y"

26-03-21

If you just want to view the current time only and nothing else, use the command:

$ date "+%T"

11:17:11

The timedatectl command is a new utility used in modern Linux systems such as Ubuntu 18.04, RHEL 8 & CentOS 8. It’s a replacement of the date command which was prominent in the old SysVinit systems. It can be used to query and adjust the time on a Linux system.

Without any options, the timedatectl command prints out an array of information such as the local time, UTC time, RTC time, and the timezone to mention a few.

$ timedatectl
Check Time and Date in Linux

How to Set a Timezone on a Linux System

On a Linux system, time is dependent on the timezone that is set. To check the timezone that is configured on your system, issue the command:

$ timedatectl | grep Time
Check Linux System Timezone

From the output in the snippet above, I am in the Africa/Nairobi timezone. To view the available timezones, run the command:

$ timedatectl list-timezones
Check Linux Timezones

Press ENTER to scroll through the whole list of the possible time zones that are available.

Timezones are also defined in the /usr/share/zoneinfo/ path as shown.

$ ls /usr/share/zoneinfo/
View Linux Timezones

There are a couple of ways that you can use to configure the timezone. Using the timedatectl command, you can set the timezone, for instance, to America/Chicago, using the syntax shown.

$ timedatectl set-timezone 'America/Chicago'
Set Linux Timezone

The other way you can set the timezone is to create a symbolic link from a timezone file in the /usr/share/zoneinfo path to /etc/localtime. For example, to set the local time zone to EST (Eastern Standard Time), issue the command:

$ sudo ln -sf /usr/share/zoneinfo/EST /etc/localtime
Set Local Timezone in Linux

Set Date and Time on a Linux System

To set time only on a Linux system using the format HH:MM:SS (Hour: Minute: Second ), use the syntax below

$ timedatectl set-time 18:30:45

To set the date only in YY-MM-DD (Year: Month: Day) format, use the syntax:

$ timedatectl set-time 20201020

To set both date and time, run:

$ timedatectl set-time '2020-10-20 18:30:45'

NOTE: Manually setting time and date in this manner is not recommended since you are likely to configure inaccurate time and date settings. In fact, by default, automatic time synchronization is turned on to prevent you from making manual time and date settings.

The most recommended way to set time is by either specifying the time zone that you are in as shown earlier or turning on automatic time synchronization with a remote NTP server.

Set Automatic Time Synchronization using NTP Server

NTP is short for Network Time Protocol, which is an internet protocol that is used to automatically synchronize the system’s time clock with a pool on online NTP servers.

Using the timedatectl command, you can set automatic time synchronization as follows:

$ timedatectl set-ntp true

To disable automatic NTP time synchronization, execute:

$ timedatectl set-ntp false
Conclusion

The timedatectl and date commands are handy command-line tools that can help you check and adjust your time on Linux.

Become a Linux Foundation Certified IT Associate (LFCA)
Exit mobile version