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.
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
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)
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:
-
Changing the current date (timedatectl set-time YYYY-MM-DD)
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
-
Changing time zone (timedatectl set-timezone time_zone)
(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
date +"format" date +"%Y-%m-%d %H:%M"
-
Changing current time
date --set HH:MM:SS date --set HH:MM:SS --utc
-
Changing current date
date --set YYYY-MM-DD
date --set "2017-06-02 23:26:00"
(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
-
Setting hwclock date & time (hwclock –set –date “YYYY-MM-DD HH:MM”)
-
Synchronizing date & time
set the hardware clock to the current system time (hwclock –systohc)
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:
[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. 🙂