Sunday, January 7, 2018

Playing with Date & Time in RHEL/CentOS

http://linuxtechlab.com/playing-date-time-rhelcentos

In this tutorial, we are going to discuss how we can view or modify date & time values in our RHEL/CentOS 7 servers. We are going to discuss 3 commands which we can use to view/modify date & time for our systems. Before we discuss the commands, let’s learn in brief about two different types of clocks available i.e. system clocks & Hardware clocks,
System clock or software clock is maintained by OS, kernel in particular & is initialized when your system boots up while the Hardware clock or Real time clock (RTC) is independent of the OS & works even when the system is closed. It draws power from BIOS battery to continue working. Now let’s move onto the commands which we will use to work with these clocks.
  • Timedatectl command – was introduced as a part of systemd in version 7 of RHEL/CentOS. It allows us to change/view the system clock.
  • Date command– is available for all the Linux distributions & is used to review/modify system clock.
  • Hwclock command – is used for accessing hardware clock of the system& is used for displaying time from the hardware clock

TIMEDATECTL command 


Display Date & Time

To view current date & time of your system clock, run
$ timedatectl

Changing Time

To change the time of the system clock, run
$ timedatectl set-time HH:MM:SS
Where, HH means hour, MM means minutes & SS means seconds.

Changing Date

To change the date of the system clock, run
$ timedatectl set-time ‘YYYY-MM-DD  HH:MM:SS’
Where, YYYY is the year, MM is the month & DD is the day of the month. We also need to specify the time otherwise it will be set as 00:00:00.

Changing time-zones

Firstly to change the time-zone of a system clock, we need to find the time-zone that we need to set. To find the time zone, run
$ timedatectl list-timezones
It will display the list of available time-zones. Select the zone & run the following command to set it,
$ timedatectl set-timezonetime_zone (Asia/dhaka).

DATE command


Display Date & Time

To view current date & time of your system clock, run
$ date
We can also customize the display date format using,
$ date +”MM:HH:SS”

Changing Time

To change the time of the system clock, run
$ date +%T –s “HH:MM:SS”
It will set the time in 24-hour format. To use time in AM/PM format, use
$ date +%T%P –s “HH:MM:SSAM”

Changing Date

To change the date of the system clock, run
$ date –s ‘DD MM YYYY  HH:MM:SS’
To set the date in customized format, use
$ date %y%m%d –s “YYYYMMDD”

HWCLOCK command


Display Date & Time

To view current date & time of your Hardware clock, run
$ hwclock

Changing Date &Time

To change the time of the Hardware clock, run
$ hwclock – – set – – date “DD MM YYYY HH:MM”

Synchronizing Date & Time

To synchronize Hardware clock to system clock, run
$ hwclock – – systohc
Or to synchronize system clock to hardware clock ,
$ hwclock – – hctosys
These were some commands to review/modify the date & time of your machine. If you have some queries/issues regarding the above , please leave them in the comment box below.

No comments:

Post a Comment