Linux timedatectl Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the timedatectl command to manage the system date, time, and time zone settings in Linux. The timedatectl command is part of the systemd suite of tools and provides a convenient way to view and modify these settings. You will start by understanding the basic usage of the timedatectl command, then learn how to change the time zone and configure NTP settings to keep your system clock synchronized.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") subgraph Lab Skills linux/sudo -.-> lab-422958{{"`Linux timedatectl Command with Practical Examples`"}} linux/date -.-> lab-422958{{"`Linux timedatectl Command with Practical Examples`"}} linux/set -.-> lab-422958{{"`Linux timedatectl Command with Practical Examples`"}} end

Understand the timedatectl Command

In this step, you will learn about the timedatectl command, which is used to manage the system date and time settings in Linux.

The timedatectl command is part of the systemd suite of tools, and it provides a convenient way to view and modify the system's date, time, and time zone settings.

Let's start by checking the current system date and time using the timedatectl command:

timedatectl

Example output:

               Local time: Wed 2023-04-12 10:30:00 UTC
           Universal time: Wed 2023-04-12 10:30:00 UTC
                 RTC time: Wed 2023-04-12 10:30:00
                Time zone: UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

This output shows the current local time, universal time (UTC), real-time clock (RTC) time, time zone, and the status of the system clock and NTP service.

Next, let's see how to change the system's time zone using timedatectl:

sudo timedatectl set-timezone America/New_York

This command sets the time zone to "America/New_York". You can use the timedatectl list-timezones command to see a list of available time zones.

Manage System Date and Time with timedatectl

In this step, you will learn how to manage the system date and time using the timedatectl command.

First, let's check the current system date and time:

timedatectl

Example output:

               Local time: Wed 2023-04-12 10:30:00 UTC
           Universal time: Wed 2023-04-12 10:30:00 UTC
                 RTC time: Wed 2023-04-12 10:30:00
                Time zone: UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

To change the system date, use the timedatectl set-time command:

sudo timedatectl set-time "2023-04-13 12:00:00"

This command sets the system date and time to April 13, 2023, at 12:00:00.

You can also set the system time relative to the current time using the + or - operators:

sudo timedatectl set-time "+1 hour"
sudo timedatectl set-time "-1 day"

These commands will add one hour or subtract one day from the current system time, respectively.

Configure Time Zone and NTP Settings

In this step, you will learn how to configure the time zone and Network Time Protocol (NTP) settings using the timedatectl command.

First, let's list the available time zones:

timedatectl list-timezones

This will display a long list of available time zones. You can use this list to find the appropriate time zone for your system.

To set the time zone, use the timedatectl set-timezone command:

sudo timedatectl set-timezone America/Los_Angeles

This sets the time zone to "America/Los_Angeles".

Next, let's configure the NTP settings. By default, the system should be using the NTP service to synchronize the system clock. You can check the status of the NTP service using the timedatectl command:

timedatectl

The output should show that the "NTP service" is "active".

If the NTP service is not active, you can enable it using the following command:

sudo timedatectl set-ntp true

This command enables the NTP service and starts synchronizing the system clock with NTP servers.

Summary

In this lab, you learned about the timedatectl command, which is used to manage the system date and time settings in Linux. You started by understanding the basic usage of the timedatectl command and how to view the current system date, time, and time zone. Then, you learned how to change the system's time zone using the timedatectl set-timezone command. Additionally, you explored how to manage the system date and time, including setting the date and time using the timedatectl set-time command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like