Learning the timedatectl Command Though Examples

Published:14 August 2023 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Some applications, like your web browser, tend to mess up when your system’s time and date settings are incorrect or outdated. And if you ever find yourself struggling with these settings, the timedatectl command can save the day!

In this tutorial, you will learn the basics of using the timedatectl command to manage your system’s clock and time zone settings quickly.

Keep reading to control your system’s time and date accuracy!

Prerequisites

Ensure you have the following in place to follow along with the hands-on demonstrations in this tutorial:

  • A Linux system – This tutorial uses Ubuntu 20.04, but the steps work with other distributions with systemd running.

Retrieving the Current Time and Date via the timedatectl Command

In the world of computing, having the correct time and date is crucial for various tasks and functions. Knowing the accurate time and date is essential for scheduling tasks, maintaining logs, or simply staying organized.

Thankfully, Linux-based systems provide you with commands to retrieve this information effortlessly. One such command is timedatectl, which allows you to interact with your system’s time and date settings.

To see how the timedatectl command works:

Open your terminal, and run the following command to retrieve the current time and date configuration (status) on your system

sudo timedatectl status

You will see a detailed overview of your system’s time and date settings below where:

SettingsDetails
Local timeShows the current local time on the system in “Day YYYY-MM-DD HH:mm:ss TimeZone” format. In this case, the local time is Sun 2023-08-06 15:43:25 UTC.
Universal timeAlso known as Coordinated Universal Time (UTC) or Greenwich Mean Time (GMT), this section displays the current time in UTC format. The entry Sun 2023-08-06 15:43:25 UTC shows that the current UTC is the same as the local time.
RTC timeReal-Time Clock (RTC) is your system’s hardware clock. The RTC time is kept running even when the system is powered off to maintain accurate time. In this case, the RTC time is Sun 2023-08-06 15:43:26, one second ahead of the system time, which is generally considered acceptable and normal.
Time zoneDisplays the current time zone the system is configured to use. In this case, the time zone Etc/UTC (UTC, +0000) indicates the system is set to the Etc/UTC (equivalent to UTC). The +0000 part represents the time zone offset from UTC, which is zero in this case, as UTC itself has a zero offset.
System clock synchronizedIndicates whether the system clock is synchronized with an external time source. The entry yes suggests the system clock is synchronized. Time is kept accurate by synchronizing with a reliable time server or Network Time Protocol (NTP) service.
NTP serviceSynchronizes your system’s time with a time server over a network. The active entry indicates the NTP service is active and operational on your system.
RTC in local TZDetermines whether your hardware RTC is set to use the local time or UTC. The entry no suggests the RTC is not set to local time but is configured to use UTC.
Retrieving the system’s current time and date
Retrieving the system’s current time and date

Listing All Available Time Zones

You have seen your system’s time zone is set to Etc/UTC, but is it the right one? How do you change it? Before changing time zones, you must see which ones are available.

Besides retrieving your system’s current date and time settings, you can also list all time zones you can set for your system.

Execute the below timedatectl command to list all available time zones (list-timezones) on your Linux-based system.

sudo timedatectl list-timezones

You will see a comprehensive list of time zones, as shown below, that you can configure and use for setting your system’s local time.

This list includes major cities, regions, and countries with different time offsets from Coordinated Universal Time (UTC). These offsets are due to variations in geographical location and daylight-saving time practices.

Obviously, there are too many time zones to scroll through. But no worries! The next step lets you look for specific ones to set for your system.

Listing all available time zones to set for Linux-based systems
Listing all available time zones to set for Linux-based systems

Now, run each command below, but you will extract (egrep) specific time zones this time.

The egrep command is helpful for quickly finding the local time zone corresponding to specific regions (i.e., America, Asia, and Europe).

# Extracts a list of time zones in America
sudo timedatectl list-timezones |  egrep  -o "America/N.*"
# Extracts a list of time zones in Asia
sudo timedatectl list-timezones |  egrep  -o "Asia/B.*"
# Extracts a list of time zones in Europe
sudo timedatectl list-timezones |  egrep  -o "Europe/L.*"

In the output below, you can select a time zone that matches your geographical location or desired time reference for the system.

Extracting specific time zones
Extracting specific time zones

Setting the System’s Current Time Zone

With the retrieved list of available time zones, you can make adequate decisions in setting the appropriate time zone for your system. Setting the correct time zone is essential for accurately displaying the local time, considering daylight saving time changes.

Moreover, setting the correct time zone ensures proper synchronization with other systems and services across different time zones.

Run the below command to set your system’s new time zone (set-timezone) to America/New_York (or with your time zone of choice).

sudo timedatectl set-timezone America/New_York

When changing the time zone using the timedatectl command, it is essential to provide the correct and valid name of the time zone, as shown in the Listing All Available Time Zones section.

If successful, the command does not provide output, as shown below.

Setting the system time zone successfully
Setting the system time zone successfully

But if you set an incorrect or invalid time zone name, the execution leads to errors like the one below. As you can see, instead of America/New_York, there is a typo in the time zone as it is missing a single letter (America/New_Yor).

Setting an invalid/incorrect time zone
Setting an invalid/incorrect time zone

Now, run the following command to check the status of your system’s time zone.

sudo timedatectl status

The output below confirms your system’s time zone is now set to America/New_York.

Verifying the newly-set system time zone
Verifying the newly-set system time zone

Adjusting the Hardware Clock’s Time Zone

So far, you have been able to adjust the system’s time zone. But what if you also need to adjust the hardware clock’s time zone? The hardware clock is a component that keeps track of the time, even when the system is powered off. On that note, is it safe to change the hardware clock setting?

Ensuring the hardware clock is set correctly to local time or UTC is crucial for maintaining accurate timekeeping and avoiding time-related discrepancies.

To adjust your hardware clock’s time zone:

Execute the below command, which does not provide output to the terminal but sets your hardware clock (set-local-rtc) to local time (1).

This configuration is suitable for dual-boot systems where one OS uses local time (i.e., Windows) while another uses UTC (i.e., Linux).

sudo timedatectl set-local-rtc 1

💡 If you use a single-boot system only, change the value to 1, which sets the hardware clock to UTC instead.

Now, run the following command to check your system’s time and date status.

sudo timedatectl status

Below, you can see RTC is now set to local time. Even though this setting works for dual-boot systems, ensure you read through the warning message about setting the RTC to local time.

Verifying the hardware clock is set to local time
Verifying the hardware clock is set to local time

Enabling NTP for System Time and Date Accuracy

Manually adjusting your system time and hardware clock’s time zone works fine, even as a one-time thing. But what if your system’s time and date settings need further adjustment due to frequent power outages or other reasons?

In such cases, enabling NTP is a better option to maintain date and time accuracy over the long term. NTP is a protocol that allows computers on a network to synchronize their clocks (universal time) with an authoritative time source.

Execute the below set-ntp command to enable (true) NTP for your system’s time and date accuracy.

timedatectl set-ntp true

When prompted, provide your user password to authorize the command execution, as shown below. Once authorized, NTP synchronization activates successfully.

Enabling NTP for system time and date accuracy
Enabling NTP for system time and date accuracy

Conclusion

Knowing the correct time and date is essential for various tasks. And in this tutorial, you have realized that timedatectl offers a user-friendly approach to managing these critical settings. Moreover, you have learned to adjust the hardware clock’s time zone to ensure accurate timekeeping even when the system is powered off.

With NTP enabled, your system’s clock is guaranteed to remain precisely synchronized with an authoritative time source for time and date accuracy. You can now confidently take charge of your Linux system’s time management to ensure seamless synchronization and avoid time-related discrepancies.

Now, why not explore further by diving into the date command and its many functionalities? Learn how to use the date command to customize time displays, calculate time intervals, or even set specific dates!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!