This post will guide you how to set date and time from the command line in Linux . How do I change date using date command under Linux Operating systems.
- Date Command
- Hwclock Command
- Displaying Current Date and Time
- Displaying HardWare Clock
- Set Date on Linux
- Set Time on Linux
- Set HardWare Clock From the System Clock
Date Command
Date command can be used to display the current time in the given FORMAT, or set the system date.
The syntax of the Date command are as followss:
date [OPTION]... [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Options:
-d, –date=STRING
display time described by STRING, not ‘now’
-s, –set=STRING
set time described by STRING
Hwclock Command
hwclock is an administration tool for the time clocks. It can: display the Hardware Clock time; set the Hardware Clock to a specified time; set the Hardware Clock from the System Clock; set the System Clock from the Hardware Clock; compensate for Hardware Clock drift; correct the System Clock timescale; set the kernel’s timezone, NTP timescale, and epoch (Alpha only); and predict future Hardware Clock values based on its drift rate.
Option:
-r, –show, –get: Read the Hardware Clock and print its time to standard output in the ISO 8601 format.
Displaying Current Date and Time
If you want to dispaly current date and time from a command prompt on your Linux system, and you can use the following date command, type:
$ date
Outputs:
[root@mydevops devops]# date
Tue Oct 1 12:05:11 EDT 2019
You can also use another command called timedatectl to display the current date and time in your Linux system, type:
$ timedatectl
Outputs:
[root@mydevops devops]# timedatectl
Local time: Wed 2019-10-02 20:23:10 EDT
Universal time: Thu 2019-10-03 00:23:10 UTC
RTC time: Thu 2019-10-03 00:23:10
Time zone: America/New_York (EDT, -0400)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
Displaying HardWare Clock
If you want to read the hardware clock and print its time to standard output, and you can type one of the following commands:
$ hwclock -r
or
$ hwclock --show
Outputs:
[root@mydevops devops]# hwclock -r 2019-10-01 12:08:37.027173-04:00 [root@mydevops devops]# hwclock --show 2019-10-01 12:08:42.268991-04:00
Set Date on Linux
If you want to set current date to a new date and time in your Linux system, and you need to pass the –set or -s option to the date command.
For example, you want to set the current date and time to “Oct 2 18:20:23 EDT 2019“, and you can use the following command:
$ date -s "Oct 2 18:20:23 EDT 2019"
Outputs:
[root@mydevops devops]# date
Tue Oct 1 12:11:08 EDT 2019
[root@mydevops devops]# date -s "Oct 2 18:20:23 EDT 2019"
Wed Oct 2 18:20:23 EDT 2019
[root@mydevops devops]# date
Wed Oct 2 18:20:24 EDT 2019
Set Time on Linux
If you only want to set time for your Linux system, and you can use the following command:
$ date +%T -s "hh:mm:ss" $ date +%T -s "20:20:20"
Outputs:
[root@mydevops devops]# date
Wed Oct 2 18:23:01 EDT 2019
[root@mydevops devops]# date +%T -s "20:20:20"
20:20:20
[root@mydevops devops]# date
Wed Oct 2 20:20:23 EDT 2019
Set HardWare Clock From the System Clock
If you want to set HardWare Clock from your system clock, and you can pass the -w or –systohc option to the hwclock command, type:
$ hwclock --systohc
Or
$ hwclock -want
Conclusion
You should know that how to display or set date and time using date or timedatectl commands from the command line in your CentOS or RHEL or Ubuntu system.