In a networked environment, it's a good practice to keep your Linux system time accurate. There are many services in the Linux system that require the right time to work properly under networking conditions.
Let's take a look at the commands you can use to manage your machine time. In Linux, there are two ways of managing time: System time and hardware time.
The system time is managed by the system clock, and the hardware time is managed by the hardware clock.
To view your system time, date, and time zone, use the date command as follows.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ date
Wed SEP 9 12:25:40 IST 2015
Set the system time as follows with Date-s or date-set= "STRING".
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ sudo date-s "12:27:00"
Wed SEP 9 12:27:00 IST 2015
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ sudo date--set= "12:27:00"
Wed SEP 9 12:27:00 IST 2015
You can also set the time and date as follows.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ sudo date 090912302015
Wed SEP 9 12:30:00 IST 2015
Use the CAL command to view the current date from the calendar.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Use the Hwclock command to view the hardware clock time.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ sudo hwclock
Wednesday September 2015 06:02:58 PM IST-0.200081 seconds
To set the hardware clock time, use the hwclock–set–date= STRING command as follows.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ sudo hwclock--set--date= "09/09/2015 12:33:00"
Tecmint@tecmint ~/linux-tricks $ sudo hwclock
Wednesday September 2015 12:33:11 PM IST-0.891163 seconds
The system time is set by the hardware clock time at startup, and the hardware time is reset to the system time when the system shuts down.
So when you look at the system time and hardware time, they are the same unless you change the system time. When your CMOS battery is low, the hardware time may be incorrect.
You can also use the hardware clock to set the system time as follows.
The code is as follows:
$ sudo hwclock--hctosys
You can also use the system clock time to set the hardware clock time as follows.
The code is as follows:
$ sudo hwclock--SYSTOHC
To see how long your Linux system has been running, you can use the uptime command.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ uptime
12:36:27 up 1:43, 2 users, load average:1.39, 1.34, 1.45
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ uptime-p
Up 1 hour, minutes
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ uptime-s
2015-09-09 10:52:47
Linux command date date time and Unix time stamp interchange
A. Convert a date to a Unix timestamp
To indicate the current time as a Unix timestamp:
The code is as follows:
Date +%s
The output is as follows:
1361542433
Convert specified date to UNIX timestamp:
The code is as follows:
Date-d ' 2013-2-22 22:14 ' +%s
The output is as follows:
1361542440B. Convert Unix timestamp to date time
Do not specify date-time formats:
The code is as follows:
Date-d @1361542596
The output is as follows:
Fri Feb 22:16:36 CST 2013 Specify date format conversions:
The code is as follows:
date-d @1361542596 + "%y-%m-%d%h:%m:%s"
The output is as follows:
2013-02-22 22:16:36