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:
- Hardware clock – This is the battery-powered clock also referred to as the CMOS clock or RTC ( Real Time Clock). The clock runs independently of the operating system & keeps running even when the system is powered off provided the CMOS battery is present.
- System clock ( Software clock ) – This is also referred to as the kernel clock. At boot time, the system clock is initialized from the hardware clock and takes over from there.
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
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
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
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/
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'
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 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.
“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.”
(emphases mine)
That statement insinuates that systemd is the new standard and SysVinit, and other non-systemd inits, are totally deprecated, which is very far from the truth. Whether a Linux distro is Modern or OLD is determined by the kernel version it uses, not the version of any of the distro’s components, even if it is the init.
There still are many MODERN Linux systems, such as Gentoo, PCLinuxOS, Devuan, Artix, and others that use SysVinit, runit, or other inits rather than systemd.
BTW – Ubuntu 18.04 can in no way be considered MODERN as Ubuntu 21.04 has been already released.
“There are two main utilities used to check time and date on a Linux system.”
You should differentiate right up front which command is used by distros using systemd as init and which command is used by distros using non-systemd inits.
Somehow you did not provide the “date” command options for setting time zone, date & time, and how to sync with NTP servers.