Skip to content

📚 Tutorial: Managing System Time with timedatectl

In this tutorial, you'll learn about timedatectl, a versatile command-line tool for managing system time settings. We'll explore how it replaces the old date command, how to synchronize time with NTP servers, and walk through common timedatectl commands. The tutorial includes a practical lab, a summarized shell script for quick reference, and a visual guide.


Table of Contents

  1. What is timedatectl?
  2. Common timedatectl Options
  3. Lab: Common Usage of timedatectl
  4. Lab Summary in a Shell Script
  5. Visual Guide

1️⃣ What is timedatectl?

timedatectl is a command-line utility for querying and changing the system clock and its settings. It is part of the systemd suite and replaces the older date command. Beyond setting the time and date, timedatectl can synchronize the system clock with remote NTP (Network Time Protocol) servers.

Key Features:

  • Replacement for date Command: Provides a modern interface for managing system time.
  • Time Synchronization:
  • Use NTP servers by enabling either chronyd or ntpd services and setting NTP to yes in timedatectl.
  • Alternatively, use the systemd-timesyncd daemon to synchronize time, which replaces ntpd and chronyd.

2️⃣ Common timedatectl Options

timedatectl offers several options to manage system time settings efficiently. Here are some of the most commonly used commands:

  1. Display System Time Status:

  2. Shows the current time, date, and time zone settings, along with the NTP synchronization status.

  3. Command:

    timedatectl status
    

  4. Set the Hardware Clock (RTC):

  5. Adjust the hardware clock to local time or UTC.

  6. Command:
    sudo timedatectl set-local-rtc 1
    
  7. Example:

    sudo timedatectl set-local-rtc 0  # Use UTC
    

  8. Adjust NTP Settings:

  9. Enable or disable NTP synchronization.

  10. Command to Enable NTP:
    sudo timedatectl set-ntp true
    
  11. Command to Disable NTP:

    sudo timedatectl set-ntp false
    

  12. Set the System Time Directly:

  13. Set both the system date and time in one command.

  14. Command:

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

  15. View and Select Time Zones:

  16. List all available time zones and set the system to a preferred one.
  17. Command to List Time Zones:
    timedatectl list-timezones
    
  18. Command to Set Time Zone:
    sudo timedatectl set-timezone Your/TimeZone
    

3️⃣ Lab: Common Usage of timedatectl

This lab covers practical examples of using timedatectl. Follow these steps to manage your system's time settings:

  1. Check the Current Time Status:

  2. Command:

    timedatectl
    

  3. View All Available Time Zones:

  4. Command:

    timedatectl list-timezones
    

  5. Set the Time Zone:

  6. Command:

    sudo timedatectl set-timezone America/New_York
    

  7. Set the Date:

  8. Command:

    sudo timedatectl set-time '2024-09-10'
    

  9. Set the Date and Time:

  10. Command:

    sudo timedatectl set-time '2024-09-10 14:30:00'
    

  11. Enable NTP Synchronization:

  12. Command:
    sudo timedatectl set-ntp true
    

4️⃣ Lab Summary in a Shell Script

Here's a shell script that encapsulates all the commands from the lab. Save it as timedatectl_lab.sh and run it to execute all the steps in sequence.

#!/bin/bash

# 🕒 Lab: Common Usage of timedatectl

# 1️⃣ Check the current time status
echo "🕒 Current system time, date, and time zone:"
timedatectl

# 2️⃣ View all available time zones
echo "🌐 Listing all available time zones:"
timedatectl list-timezones

# 3️⃣ Set the time zone (example: 'America/New_York')
echo "🌍 Setting time zone to 'America/New_York':"
sudo timedatectl set-timezone America/New_York

# 4️⃣ Set the date (example: '2024-09-10')
echo "📅 Setting date to '2024-09-10':"
sudo timedatectl set-time '2024-09-10'

# 5️⃣ Set the date and time (example: '2024-09-10 14:30:00')
echo "⏰ Setting date and time to '2024-09-10 14:30:00':"
sudo timedatectl set-time '2024-09-10 14:30:00'

# 6️⃣ Enable NTP synchronization
echo "🔄 Enabling NTP synchronization:"
sudo timedatectl set-ntp true

# 🏁 End of lab
echo "✅ Lab completed. Please review the outputs above."