Linux: Managing clock(date/time) functionality

Clock is the important but less spoken feature of OS. It comes to rescue to accurately identify the occurrence duration of any issues and obviously, correct date/time should be there on server for applications to run smoothly.

time

Linux OS version 7 offers three command line tools that can be used to configure and display information about the system date and time: the timedatectl utility, which is new in Red Hat Enterprise Linux 7 and is part of systemd; the traditional date command;  tzselect for time zone selection(RHEL6) and the hwclock utility for accessing the hardware clock.

The system time is always kept in Coordinated Universal Time (UTC) and converted in applications to local time as needed. Local time is the actual time in your current time zone, taking into account daylight saving time (DST).

(i) Timedatectl

The timedatectl utility is distributed as part of the systemd system and service manager and allows you to review and change the configuration of the system clock.

  • Displaying current date and time

pic1

Note: Changes to the status of chrony or ntpd will not be immediately noticed by timedatectl. If changes to the configuration or status of these tools is made, enter the following command:

systemctl restart systemd-timedated.service

  • Changing the current time (timedatectl set-time HH:MM:SS)

pic2.PNG

Note: In case you get above error, that means your clock by default is set to take changes via ntp, so you need to disable first as below:

pic3.PNG

  • Changing the current date (timedatectl set-time YYYY-MM-DD)

pic4

Note that changing the date without specifying the current time results in setting the time to 00:00:00. To change the current date to 2 June 2017 and keep the current time (11:26 p.m.), run the following command as root:

timedatectl set-time 2017-06-02 23:26:00

pic5

  • Changing time zone (timedatectl set-timezone time_zone)

pic6.PNG

pic7

(ii) date

The date utility is available on all Linux systems and allows you to display and configure the current date and time.

  • Displaying current date & time

date
date --utc
pic8.PNG
date +"format"
date +"%Y-%m-%d %H:%M"

pic9.PNG

  • Changing current time

date --set HH:MM:SS
date --set HH:MM:SS --utc

pic10.PNG

  • Changing current date

date --set YYYY-MM-DD

pic12.PNG

date --set "2017-06-02 23:26:00"

pic11.PNG

(iii) hwclock

hwclock is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). The hardware clock is independent of the operating system you use and works even when the machine is shut down.

  • Display current hardware clock stat

pic13.PNG

  • Setting hwclock date & time (hwclock –set –date “YYYY-MM-DD HH:MM”)

pic14.PNG

  • Synchronizing date & time

set the hardware clock to the current system time (hwclock –systohc)

pic15.PNG
To set the hardware clock to the current system time and keep the hardware clock in local time, run the following command as root: (hwclock –systohc –localtime)
set the system time from the hardware clock (hwclock –hctosys)

CentOS6

Apart from hwclock and date command discussed above for managing time and timezones, Linux OS version 6 uses an important command to update time zone – tzselect. Let’s see it’s use below:

pic16

pic17.PNG

pic18.PNG

[root@servere ~]# echo "TZ='Asia/Kolkata'; export TZ" >> /etc/profile.d/timezone.sh
[root@servere ~]# date
Wed Apr 3 06:44:35 PDT 2019
[root@servere ~]# ssh root@servere
[root@servere ~]#
[root@servere ~]# date
Wed Apr 3 19:14:51 IST 2019
[root@servere ~]#

This all in today’s blog from our end. hope you like and practice it, do share your feedback. 🙂

Leave a comment