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:
- System Time — this is the time the operating system uses for its tasks.
- 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
- Find the current time zone of your server.
- Switch the time zone to UTC.
- 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
- Set your system's time zone to UTC.
- Manually set the system time to 12:00:00, November 1, 2023.
- 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
"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.
Access error when changing time. The
timedatectlcommand requires superuser privileges. Make sure you're usingsudo.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?
- Server administration. Setting up time and time zones is crucial for proper log operation, data syncing, and task scheduling.
- Working on international projects. Using UTC as a standard helps avoid confusion when working with clients from different time zones.
- Interviews and certification. Questions about
timedatectland 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.
GO TO FULL VERSION