How to Set Date Through Command Line in Linux?

In Linux, setting date and time ensures that the system clock is synchronized and accurate with the correct time. It has significant importance for a variety of reasons, including network synchronizations, log-in information, and many more.

This article has explained the different methods to set the date through the command line in Linux.

Note: In all the upcoming methods/explanations of this post, the “YYYY”, “DD”, and “MM” terms refer to the “year”, “month”, and “date”. While the expressions such as “hh”, “mm”, and “ss” refer to the “hours”, “minutes”, and “seconds”.

Method 1: Using date Command 

The “date” command is utilized to set the date and time. It is the most basic command to set the date and time in Linux. The syntax is as follows:

Syntax:

$ date --set="YYYY-MM-DD hh:mm:ss"

In the above syntax, the “YYYY-MM-DD” represents the “year”, “month”, “date” and “hh:mm:ss” identifying the “hours”, “minutes”, and “seconds”. 

Example:

In this example, the “date” command is utilized with the “set” utility to set the date and time as “2023-02-06” and “14:30:00” respectively:

$ sudo date --set="2023-02-06 14:30:00"

The date and time have been set to “February 6th, 2023, at 2:30 PM”.

Method 2: Using hwclock Command

The “hwclock” command is the hardware clock utility that can be utilized to set the system clock from the hardware clock. The syntax is as follows:

Syntax:

For better understanding, let’s have a look at the following example. 

$ hwclock --set --date="YYYY-MM-DD hh:mm:ss"

Example:

The “hwclock” command is utilized to set the date to “February 6th, 2023” and time to “2:30 PM”:

$ sudo hwclock --set --date="2023-02-06 14:30:00"

The output shows that the new date and time have been set through the command line.

Method 3: Using timedatectl Command

The “timedatectl” command is the element of the “systemd” system and service manager. It can be utilized to set the date and time, as well as configure the time zone. The syntax is as follows:

Syntax:

$ timedatectl set-time "YYYY-MM-DD hh:mm:ss"

Let’s implement the command via an example. 

Example:

The following “timedatectl” command sets the time and date to “February 6th, 2023”, and “2:30 PM” respectively:

$ sudo timedatectl set-time "2023-02-06 14:30:00"

The above command confirms that the date and time have been set according to the user’s needs.

Method 4: Using ntpdate Command

The “ntpdate” command is utilized to synchronize the system clock with a remote NTP server. The syntax is as follows:

Syntax:

$ ntpdate server-name

In the above syntax, the “ntpdate” requires the remote “server-name” such as “time.nist.gov”, “ntp-d.nist.gov” and many more to which the user synchronizes the date and time.

Example:

In this example, the “ntpdate” command is utilized to update the system clock time with the NTP server. 

For instance, specify the server name “pool.ntp.org” which is the most popular server for time synchronization:

$ sudo ntpdate pool.ntp.org

The output shows that the system clock has synchronized with the NTP server “pool.ntp.org”.

Conclusion

To set the date through the command line, utilize the “date”, “hwclock”, “timedatectl” and “ntpdate” commands by specifying the “YYYY-MM-DD hh:mm:ss” format. Using these commands, users can set a specific date and time based on their needs. 

This article has demonstrated various methods to set the date through the command line in Linux.