Set time and date in CentOS / Red Hat

Setting time and date is also one of the important things for your machine, especially if you are using IPA/LDAP services on your Linux machine.

Here is how to do it:

(execute all commands as root)

First, check status of your time and date settings by entering:

timedatectl status

Change time:

To set current time use the following command:

HH – hour (I will be using 24 hour format)

MM – minute

SS – seconds

timedatectl set-time HH:MM:SS

You will get error like I did “Failed to set time: NTP unit is active“if you already have set NTP service on your machine.

You can force your machine to use local time by typing in

sudo timedatectl set-local-rtc true

Change date:

We can change the date with following command:

sudo timedatectl set-time "yyyy-MM-dd hh:mm:ss"

yyyy – year

MM – month

dd – day

hh – hour

mm – minute

ss – seconds

Set the time also, otherwise your clock will be reset.

Change time zone:

First let’s check available time zones by typing in:

timedatectl list-timezones

Ok, I want to set time zone to Europe/Zagreb, to do so, I will enter

(change Europe/Zagreb with your time zone)

sudo timedatectl set-timezone Europe/Zagreb

You can also enable NTP service by entering following command

sudo timedatectl set-ntp yes

More details on setting NTP server in other article