CodeGym /Courses /Docker SELF /Managing Time Zones and Time: timedatectl Commands

Managing Time Zones and Time: timedatectl Commands

Docker SELF
Level 3 , Lesson 2
Available

1. Working with Time on Linux

Why is it important to set the time correctly?

Imagine you're trying to track down a bug in the system log, but the timestamps in the logs don't match the real-world time. Or, even worse, your server in an international company shows different times for different users. Setting the time properly isn’t just a convenience: it's critical for system functionality, file synchronization, maintaining security (SSL certificates depend on time), and coordinating work tasks.

On Linux, time can be represented in two forms:

  1. System Time — this is the time the operating system uses for its tasks.
  2. Hardware Time — stored at the hardware level (BIOS/UEFI).

We’ll be working with system time using the timedatectl tool.


2. Basics of the timedatectl Command

timedatectl is a command-line utility that offers a convenient interface for managing time. It lets you:

  • Check current time settings and time zones;
  • Set system and hardware time;
  • Configure time zones;
  • Enable and disable time synchronization using NTP (Network Time Protocol).

Checking Current Time

Let’s start with the basics. To check the current time and settings, run the command:

timedatectl

Example output:

Local time: Tue 2023-10-31 12:34:56 MSK
Universal time: Tue 2023-10-31 09:34:56 UTC
RTC time: Tue 2023-10-31 09:34:56
Time zone: Europe/Moscow (MSK, +0300)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

Explanation of the fields:

  • Local time: Local (system) time.
  • Universal time: Time in UTC format (Coordinated Universal Time).
  • RTC time: Hardware time (at the BIOS/UEFI level).
  • Time zone: Current time zone.
  • System clock synchronized: Whether the clock is synchronized via NTP.
  • NTP service: Whether the time synchronization service is active.
  • RTC in local TZ: Whether the hardware clock matches the local time zone.

Configuring and Changing the Time Zone

Sometimes, after relocating or due to project requirements, you might need to change the time zone. For example, your server was set to New York, but now it’s serving users in Moscow. Time zones can be found in the /usr/share/zoneinfo directory.

To view available time zones, execute:

timedatectl list-timezones

The output will be a huge list, like this:

Africa/Abidjan
America/New_York
Asia/Tokyo
Europe/Moscow

You can set a new time zone using the command:

sudo timedatectl set-timezone Europe/Moscow

Check that the time zone has changed:

timedatectl

Hands-On Exercise

  1. Find the current time zone of your server.
  2. Switch the time zone to UTC.
  3. Switch the time zone back to your local one.

3. Setting Time Manually

Even though modern servers usually sync time over NTP, sometimes you need to set the time manually. This can be useful if the server is in an isolated network without internet access.

First, you can check the current system time:

date

You can set a new time using the command:

sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"

For example, to set the time to November 1, 2023, 12:00:

sudo timedatectl set-time "2023-11-01 12:00:00"

Now check the result again:

timedatectl

Setting Hardware Time

Hardware time (also known as RTC — Real-Time Clock) is managed at the BIOS/UEFI level. If the hardware time isn’t synced with the system time, it can cause issues during reboot. To sync it, run:

sudo hwclock --systohc

This command will set the hardware time to match the system time.

If you need to do the reverse (sync system time with hardware time), use:

sudo hwclock --hctosys

4. Time Synchronization via NTP

Time synchronization via NTP is a way to automatically keep the current time using remote servers. On most modern Linux distributions, NTP is enabled by default.

Checking NTP Settings

Run:

timedatectl status

If NTP is disabled (the NTP service line shows inactive), enable it:

sudo timedatectl set-ntp true

To disable NTP, run:

sudo timedatectl set-ntp false

If NTP is not working, make sure the appropriate services are active. For example, on systems with systemd-timesyncd make sure the service is active:

sudo systemctl status systemd-timesyncd

5. Practical Exercise

Task

  1. Set your system's time zone to UTC.
  2. Manually set the system time to 12:00:00, November 1, 2023.
  3. Enable time synchronization via NTP.

Verification

After completing the task, make sure that:

  • Your system time matches the specified settings.
  • The time zone has been changed to UTC.
  • The NTP service is active.

6. Common Mistakes and Pitfalls

  1. "RTC in local TZ: no" does not match the local time zone. Many systems prefer to use UTC for hardware time to avoid confusion when working across different time zones. This is not an error, but rather a preference.

  2. Access error when changing time. The timedatectl command requires superuser privileges. Make sure you're using sudo.

  3. NTP synchronization doesn't activate. Check if the NTP service is running on your server. For example, for systemd-timesyncd, run:

    sudo systemctl start systemd-timesyncd
    

Why is this useful in practice?

  1. Server administration. Setting up time and time zones is crucial for proper log operation, data syncing, and task scheduling.
  2. Working on international projects. Using UTC as a standard helps avoid confusion when working with clients from different time zones.
  3. Interviews and certification. Questions about timedatectl and time management often come up in exams and interviews for system administrator positions.

Now you're ready to manage time in Linux like you handle your deadlines: confidently, precisely, and with no surprises.

1
Task
Docker SELF, level 3, lesson 2
Locked
Checking current time and settings
Checking current time and settings
1
Task
Docker SELF, level 3, lesson 2
Locked
Viewing available time zones
Viewing available time zones
1
Task
Docker SELF, level 3, lesson 2
Locked
Changing the Time Zone
Changing the Time Zone
1
Task
Docker SELF, level 3, lesson 2
Locked
Manual System Time Configuration
Manual System Time Configuration
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION