Keeping our date and time settings accurate in Linux is extremely important for the smooth functioning of system tasks. The timedatectl
command in Linux allows you to query and change the system clock and its various associated settings. It comes as part of systemd and provides a simple way to ensure your system reports the right date and time. Here we show you how to use timedatectl
to control the time and date on your Linux machine.
Tip: You can also use systemd timer as a cron replacement.
System Clock vs. Real-Time Clock (RTC)
Your Linux system uses two different clocks to keep track of time – system clock and real-time clock.
The system clock or the software-managed clock is maintained by your operating system or the Linux kernel. It needs help from the real-time clock to get the initial value during a system boot.
The real-time clock or RTC is a hardware clock on your motherboard and remains active even when the system is turned off. It uses a battery to keep it running continuously so it can track the time and inform the system of the correct time during the boot.
The timedatectl
command helps you to manage both these clocks.
Display System Date and Time With timedatectl
The timedatectl
command is one of the most useful Linux commands. To simply check the current date and time on your Linux system, type in the timedatectl
command without any other parameters and press Enter. The output will display the local time, universal time, RTC time, time zone, and some other pieces of useful information.
While the Local time (based on the system time zone), Universal time, and RTC time entries are self-explanatory, the Time zone entry refers to the time zone that is currently set in the system.
The other items inform you whether the system clock is synchronized with an NTP server (for real-time update using the internet), whether that NTP service is active, and whether the RTC is using the local time instead of UTC.
Update System Time Zone With set-timezone
Depending on the time zone your Linux system is using, the system clock might not show the correct local time.
To check the time zone your system is using, refer to the Time zone line in the output in the previous section. For instance, my system is using the US/Eastern (EST, -0500) time zone. The -0500 offset refers to the difference from the UTC or the universal time.
If you want to change the time zone, first query the system for a list of available time zones you can use:
timedatectl list-timezones
It will generate a long list and will take a long time for you to scroll through. For a more practical output, you can modify the command to limit the results to your local region or country:
timedatectl list-timezones | grep "America/" | less
To change the system’s current time zone, you can use the set-timezone
parameter as follows:
timedatectl set-timezone "America/Chicago"
The system might prompt you to enter your password as the command requires elevated privileges to execute. Once done, you can verify the change by simply executing timedatectl
again.
You can see that the output reports the new time zone that was updated in the previous command. If you prefer, you can even set multiple timezones in Ubuntu and other Linux distros for a quick glance at multiple time zones at once.
Set System Date and Time Manually With set-time
In most cases, your system will synchronize the system clock with an NTP server on startup, so your system time and date are always correct. However, in the event that your system don’t have access to the Internet, you can still use the set-time
parameter to manually set a new date or time.
To manually set the system date and time, first disable the synchronization with the NTP server:
sudo systemctl disable --now chronyd
Next, set the new date and time using the following command:
timedatectl set-time "2023-11-16 10:00:00"
Configure Real-Time Clock With timedatectl
You can also configure the real-time clock of your system. By default, the RTC is synced to the universal time or UTC, but it’s possible to set it to your local time zone.
One thing to note is that maintaining the RTC in the local time zone can create various problems with time zone changes and daylight saving adjustments. Your system might also display a similar warning if you change the RTC to your local time zone.
In any case, if you still want to set the RTC to your local time zone, execute the following command:
timedatectl set-local-rtc 1
If you want to set the RTC back to UTC again, simply replace 1
with 0
in the same command.
Synchronize System Clock With NTP
NTP, or Network Time Protocol, is an internet protocol for clock synchronization between a wide network of computers over the internet. It is intended to synchronize all participating computers to within a few milliseconds of UTC.
To be able to synchronize the system clock with an NTP server, you must have NTP installed on your system. You can simply execute the installation command to do this:
sudo apt-get -y install ntp
Now, to enable automatic synchronization with an NTP server, execute the following command:
timedatectl set-ntp true
Similarly, you can disable NTP synchronization by using the following command: timedatectl set-ntp false
.
Manage System Time & Date With timedatectl
The timedatectl
command is a handy tool for you to adjust your system clock and RTC configurations as well as set the required time zone if you’ve moved or are traveling. If you are always on the move, you will want to make use of these time zone converters for easy scheduling of tasks.
All screenshots by Tanveer Singh.
Our latest tutorials delivered straight to your inbox