View all posts

How to sync server time in CentOS & AlmaLinux

The NTP protocol synchronises the clocks of computers over a network. In this article, we will show you how to quickly sync the server time.

Many systems-related tasks and processes require the correct timezone. For example, the cron daemon uses the system’s timezone to execute cron jobs, and the log files’ timestamps are set to the same timezone.

You can display and set the current system’s time and timezone using the timedatectl command on most modern Linux distributions

timedatectl

      Local time: Thu 2021-12-02 12:46:48 GMT
  Universal time: Thu 2021-12-02 12:46:48 UTC
        RTC time: Thu 2021-12-02 12:46:48
       Time zone: Europe/London (GMT, +0000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2021-10-31 01:59:59 BST
                  Sun 2021-10-31 01:00:00 GMT
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2022-03-27 00:59:59 GMT
                  Sun 2022-03-27 02:00:00 BST

Changing Timezone

In order to change the timezone, you will need to know the long name for the timezone. Timezones are usually referred to using a “Region/City” format.

You can either list the files in the /usr/share/zoneinfo directory or run timedatectl to list all available time zones.

timedatectl list-timezones
```
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
```

Once you have identified which time zone applies to your location, run the following command as sudo user:

timedatectl set-timezone Europe/London

Verify the changes by running the timedatectl command again:

timedatectl

      Local time: Thu 2021-12-02 12:52:30 GMT
  Universal time: Thu 2021-12-02 12:52:30 UTC
        RTC time: Thu 2021-12-02 12:52:29
       Time zone: Europe/London (GMT, +0000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2021-10-31 01:59:59 BST
                  Sun 2021-10-31 01:00:00 GMT
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2022-03-27 00:59:59 GMT
                  Sun 2022-03-27 02:00:00 BST

Syncing the time

The ntp and ntpdate command-line utilities allow us to set the system date and time via NTP. To install this package on your system, run the following command:

yum -y install ntp ntpdate

You can view the status of the ntpd service after you have installed and enabled the packages.

systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd

Afterwards, run the ntpdate command below to add the desired CentOS NTP server. The -u switch instructs ntpdate to use an unprivileged port for outgoing packets, and the -s switch enables logging output to the system syslog.

ntpdate -u -s 0.centos.pool.ntp.org 1.centos.pool.ntp.org 2.centos.pool.ntp.org

To synchronize your local date and time with the NTP server, restart the ntpd daemon.

systemctl restart ntpd

Verify that NTP synchronization is enabled using the timedatectl command.

timedatectl

      Local time: Thu 2021-12-02 12:59:39 GMT
  Universal time: Thu 2021-12-02 12:59:39 UTC
        RTC time: Thu 2021-12-02 12:59:38
       Time zone: Europe/London (GMT, +0000)
     NTP enabled: yes <<<< here
NTP synchronized: yes <<<< here
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2021-10-31 01:59:59 BST
                  Sun 2021-10-31 01:00:00 GMT
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2022-03-27 00:59:59 GMT
                  Sun 2022-03-27 02:00:00 BST

Last but not least, you can set the hardware clock to the current system time with the -w flag using the hwclock utility.

hwclock  -w

That’s it! Your system should now have the correct time and timezone.

Related Articles...

Disable Currency WHMCS

Disable Currency in WHMCS

In order to force a particular currency in WHMCS, we can create a hook file. First, create a hook file like so: /home/user/path-to-whmcs/includes/hooks/myhook.php With the following code: From now on, users cannot change their currency from... Read more

This website uses cookies

We use cookies for the analysis of our visitor data, to improve our website, and to give you a great website experience. For more information about the cookies we use, please see our cookie policy.