Playing with Date & Time in RHEL/CentOS

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.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

13 Responses

  1. fans says:

    Dear Sir
    I want to know how i can change date and time permanently means If i will restart my server it should not be change it should remains same as befor.

    • Shusain says:

      All these commands are used to send date permanently only. If your server is getting reset to an old date and time, then its probably because of an old CMOS battery.

  2. KLR says:

    What is the point of timedatectl? What does it do that date does not?

    • Shusain says:

      Yes date command also accomplishes the same but like other commands timedatectl is in line with systemd that was introduced with RHEL 7.
      So be prepared for future as all the old commands would be deprecated.

  3. KLR says:

    I see. Change something that works perfectly just because we can.

  4. WEB HOSTING BD says:

    How to change Time format from 24 hour to permanent 12 hour format in centos 7 server.

    • Shusain says:

      Assume you are using GNOME, please try the following. Execute the following command from terminal,

      $gsettings range org.gnome.desktop.interface clock-format
      \enum
      \’24h’
      \’12h’

      To use the GUI tools, try right clicking on the Clock applet and change the settings from there.

  5. Sujith Kumar says:

    How to check the last “date” change in Redhat6?

    • Shusain says:

      you can only check if a command was executed from history. & if you need time as well, than you need to have HISTTIMEFORMAT variable enabled for the history command.
      You can do so by running the following command,

      export HISTTIMEFORMAT=’%F %T ‘

Leave a Reply

Your email address will not be published. Required fields are marked *