Synchronizing Time on Linux Servers
The most recommended approach for servers is to use NTP to ensure the time is always accurate and synchronized with external sources. Below we cover the recommended method to perform this configuration.
Using chrony (Recommended for More Accurate Synchronization)
Section titled “Using chrony (Recommended for More Accurate Synchronization)”chrony is often used on virtual machines or in environments with variable network time. It is common in RHEL/CentOS 7+ and some Ubuntu and Debian installations.
-
Install
chrony(if necessary):- Debian/Ubuntu:
sudo apt update && sudo apt install chrony - RHEL/CentOS/Fedora:
sudo dnf install chronyorsudo yum install chrony
- Debian/Ubuntu:
-
Start and Enable the Service:
Terminal window sudo systemctl enable --now chronyd# or chrony for Debian/Ubuntu -
Check the Synchronization:
Terminal window chronyc trackingLook for a
Reference IDand aStratumdifferent from zero. TheSystem timewill show the offset.
Set Timezone
Section titled “Set Timezone”First, set the correct timezone, as this affects the system clock.
-
List Timezones:
Terminal window timedatectl list-timezones | grep 'America/Sao_Paulo'(Replace with the desired timezone)
-
Set the Timezone:
Terminal window sudo timedatectl set-timezone 'America/Sao_Paulo' -
Verify that the time is correct:
Terminal window date
Synchronize the Hardware Clock (CMOS/BIOS)
Section titled “Synchronize the Hardware Clock (CMOS/BIOS)”The Linux server maintains two clocks: the System Clock (software) and the Hardware/BIOS Clock (CMOS, battery-backed). After fixing the system clock, synchronize it with the hardware clock so the time remains correct after a reboot.
sudo hwclock -w # Writes System time (soft) to the Hardware (hard)