Cyberithub

How to change date and time in Linux (RedHat/CentOS 7) with Best Examples

Advertisements

In this tutorial, I will take you through the different ways to change date and time in Linux (RedHat/CentOS 7). Sometimes it is very important to have correct date and time in your Linux Server.

Change Date and Time in Linux

timedatectl is the command to change date and time in RedHat/CentOS7. We will go through different examples of this command to understand it further.

How to change date and time in Linux (RedHat/CentOS 7) with Best Examples 1

Example 1: To check current time and date

You can current date and time by using below timedatectl command.

[root@localhost ~]# timedatectl
Local time: Mon 2019-12-02 00:36:36 EST
Universal time: Mon 2019-12-02 05:36:36 UTC
RTC time: Mon 2019-12-02 05:36:32
Time zone: America/New_York (EST, -0500)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2019-11-03 01:59:59 EDT
Sun 2019-11-03 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2020-03-08 01:59:59 EST
Sun 2020-03-08 03:00:00 EDT

Example 2: To check the version of timedatectl tool

You can use --version with timedatectl command to check the installed version details of this tool.

[root@localhost ~]# timedatectl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

Example 3: To List out the available Time Zones

You can check all the available time zones by using timedatectl list-timezones command as shown below.

[root@localhost ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
................

Example 4: To list out only Asia Time Zones

You can filter out the time zones you want to check from timedatectl list-timezones command as mentioned below.

[root@localhost ~]# timedatectl list-timezones | grep -i Asia
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
Asia/Atyrau
Asia/Baghdad
Asia/Bahrain
Asia/Baku
Asia/Bangkok
Asia/Barnaul
Asia/Beirut
Asia/Bishkek
Asia/Brunei
Asia/Chita
Asia/Choibalsan
Asia/Colombo
Asia/Damascus
Asia/Dhaka
Asia/Dili
Asia/Dubai
...........

Example 5: To change timezone to Asia/Dubai

Here, you can change the timezone by using timedatectl set-timezone command and specify the correct time zone from above output of timedatectl list-timezones command.

[root@localhost ~]# timedatectl set-timezone Asia/Dubai
[root@localhost ~]# timedatectl
Local time: Mon 2019-12-02 10:01:40 +04
Universal time: Mon 2019-12-02 06:01:40 UTC
RTC time: Mon 2019-12-02 06:01:36
Time zone: Asia/Dubai (+04, +0400)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

Example 6: To change the time to 14:35:34

You can also change the time without changing the date by using timedatectl set-time command as shown below. In this example, we are setting time to 14:35:34 using timedatectl set-time '14:35:34' command in Linux.

[root@localhost ~]# timedatectl set-time '14:35:34'
[root@localhost ~]# date
Mon Dec 2 14:35:36 +04 2019

Example 7: To change the date to 3rd Dec,2019 and time to 14:35:34

You can change date and time both by using same timedatectl set-time command as mentioned below. In this example, we are trying to set date to 3rd Dec, 2019 and time to 14:35:34 using timedatectl set-time '2019-12-13 14:35:34' command as shown below.

[root@localhost ~]# timedatectl set-time '2019-12-03 14:35:34'
[root@localhost ~]# date
Tue Dec 3 14:35:35 +04 2019

Note: Sometime you might observe an error "Failed to set time: Automatic time synchronization is enabled" whenever you try to change date and time manually. In that case you need to disable the NTP and then try to change date and time again.

[root@localhost ~]# timedatectl set-ntp 0
[root@localhost ~]# timedatectl
Local time: Tue 2019-12-03 14:37:33 +04
Universal time: Tue 2019-12-03 10:37:33 UTC
RTC time: Tue 2019-12-03 10:37:33
Time zone: Asia/Dubai (+04, +0400)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

 

Recommended Posts:-

Understanding Kafka Console Producer and Consumer in 10 Easy Steps

Popular firewalld examples to open a port on RedHat/CentOS 7

8 Most Popular mkdir command in Linux with Examples

26 Useful Firewall CMD Examples on RedHat/CentOS 7

12 Most Popular rm command in Linux with Examples

9 useful w command in Linux with Examples

Popular Apache Kafka Architecture Explained Using 4 Basic Components

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

How to check which timezone in Linux

Leave a Comment