timedatectl is part of the systemd System and Services Manager and is used in linux distributions based on the sysvinit daemon. timedatectl allows you to query and change the system clock configuration and its Settings. You can use this command to set or change the current date, time, and time zone or enable automatic system clock synchronization with a remote NTP server.
In Linux, it is beneficial to keep the correct time on the system, because most tasks in linux are controlled by time, so it is necessary to keep the system tasks running in a timely manner, which is conducive to recording the correct time of events and other system information.
Display the current time and date of the linux system:
# timedatectl status
On Linux, time is always managed by the time zone set on the system. Check the current time zone:
# timedatectl
or
# timedatectl | grep Time
View all available time zones:
# timedatectl list-timezones
Find the local time zone based on your location:
# timedatectl list-timezones | egrep -o "Asia/B.*"
# timedatectl list-timezones | egrep -o "Europe/L.*"
# timedatectl list-timezones | egrep -o "America/N.*"
To set the local timezone in linux, you can use the set-timezone switch:
# timedatectl set-timezone "Asia/Kolkata"
It is always recommended to use and set Coordinated Universal Time (UTC).
# timedatectl set-timezone UTC
You need to enter the correct name time zone, otherwise an error may occur when changing the time zone. To set the time only, we can use the set time switch and the time format HH:MM:SS (hours, minutes, and seconds).
# timedatectl set-time 15:58:30
When setting the date as shown above, you may receive the following error:
Failed to set time: NTP unit is active
The NTP server is active and needs to be disabled using a command:
#systemctl disable--now chronyd
To set the date and time, we can use the Set time switch and the date format YY:MM:DD (year, month, day) and the time format HH:MM:SS (hour, minute, second).
# timedatectl set-time '2015-11-20 16:14:50'
To set the hardware clock to UTC, use set-local-rtc boolean-value. Check if your hardware clock is set to the local time zone:
# timedatectl | grep local
To set the hardware clock to the local time zone:
# timedatectl set-local-rtc 1
Set the hardware clock to Coordinated Universal Time (UTC) :
# timedatectl set-local-rtc 0
Example Synchronize the Linux system time with the remote NTP server. NTP is an Internet protocol that synchronizes system time between servers.
The timedatectl utility lets you use NTP to automatically synchronize your Linux system clock with a remote server group. To enable automatic time synchronization with the NTP server, you must install NTP on the system. To start automatic time synchronization with the remote NTP server, enter the following command on the terminal.
# timedatectl set-ntp true
To disable NTP time synchronization, run the following command on the terminal.
# timedatectl set-ntp false
You can also continue reading the timedatectl man page if you need to learn more about it.