Linux clock Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux clock command and learn how to display the current date and time, as well as set the system clock. The clock command is a useful tool for system administrators and users who need to manage the system time. We will start by understanding the basic usage of the clock command, then proceed to display the current date and time, and finally, learn how to set the system clock to a specific date and time.

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/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") subgraph Lab Skills linux/sudo -.-> lab-422607{{"`Linux clock Command with Practical Examples`"}} linux/date -.-> lab-422607{{"`Linux clock Command with Practical Examples`"}} linux/time -.-> lab-422607{{"`Linux clock Command with Practical Examples`"}} end

Understand the clock Command

In this step, we will explore the Linux clock command and understand its usage for displaying the current date and time, as well as setting the system clock.

The clock command is a utility in Linux that provides information about the system clock and allows you to set the time and date. It is a useful tool for system administrators and users who need to manage the system time.

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

clock

Example output:

The current date is: 2023-04-18
The current time is: 15:30:00

As you can see, the clock command displays the current date and time on the system.

Next, let's set the system clock to a specific date and time. Suppose we want to set the clock to April 19, 2023, at 10:00 AM. We can use the following command:

sudo clock -s 2023-04-19 10:00:00

This command sets the system clock to the specified date and time. Note that you need to use sudo to execute this command, as it requires administrative privileges.

After setting the clock, you can verify the new date and time using the clock command again:

clock

Example output:

The current date is: 2023-04-19
The current time is: 10:00:00

The clock command is a simple yet powerful tool for managing the system clock in Linux. In the next steps, we will explore more advanced uses of the clock command.

Display Current Date and Time

In this step, we will learn how to display the current date and time using the clock command.

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

clock

Example output:

The current date is: 2023-04-19
The current time is: 10:00:00

As you can see, the clock command displays the current date and time on the system.

You can also use the date command to display the current date and time:

date

Example output:

Wed Apr 19 10:00:00 UTC 2023

The date command provides a more detailed output, including the day of the week, month, day, time, and time zone.

Both the clock and date commands are useful for quickly checking the system's current date and time. You can use them interchangeably depending on your preference.

Set the System Clock

In this step, we will learn how to set the system clock using the clock command.

To set the system clock, you can use the following command:

sudo clock -s <date> <time>

Replace <date> with the desired date in the format YYYY-MM-DD, and <time> with the desired time in the format HH:MM:SS.

For example, to set the system clock to April 20, 2023, at 11:30 AM, you would use the following command:

sudo clock -s 2023-04-20 11:30:00

After running this command, you can verify the new date and time using the clock command:

clock

Example output:

The current date is: 2023-04-20
The current time is: 11:30:00

Remember that you need to use sudo to execute the clock -s command, as it requires administrative privileges to set the system clock.

Summary

In this lab, we learned how to use the Linux clock command to display the current date and time, as well as set the system clock. The clock command is a useful tool for system administrators and users who need to manage the system time. We explored how to check the current date and time using the clock command, and then demonstrated how to set the system clock to a specific date and time using the sudo clock -s command. Additionally, we learned that the date command can also be used to display the current date and time in a more detailed format.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like