Accurate timekeeping is an important aspect of any computing system, and especially critical for servers, where the correct time is a crucial factor for many applications, such as logging, authentication, and security. The Network Time Protocol (NTP) is a widely used protocol for synchronizing computer clocks over a network.
In Linux, the system clock can be synchronized with an NTP server using the
`timedatectl` command-line utility, which is part of the systemd system and service manager. The timedatectl utility provides a simple and convenient way to manage the system time and date, including setting the NTP configuration.
Step 1: Install Timesync Service
Timesync is the minimalistic service to synchronize local time with NTP servers. The package contains the systemd-timesyncd system service that may be used to synchronize the local system clock with a remote Network Time Protocol server.
To install the service, execute following command:
apt install systemd-timesyncd
After the installation, service will be started automatically. You can verify it by running the following command:
sudo systemctl status systemd-timesyncd
Output● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2023-02-02 11:57:46 UTC; 3min 53s ago Docs: man:systemd-timesyncd.service(8) Main PID: 2004 (systemd-timesyn) Status: "Connecting to time server 22.214.171.124:123 (0.debian.pool.ntp.org)." Tasks: 2 (limit: 1828) Memory: 1.0M CPU: 48ms CGroup: /system.slice/systemd-timesyncd.service └─2004 /lib/systemd/systemd-timesyncd
Step 2: Enable the Clock Synchronization
To synchronize the Linux system clock with an NTP server using timedatectl, you need to run the following command as root or use sudo:
sudo timedatectl set-ntp true
This command enables the NTP synchronization feature and makes the system start automatically synchronizing with the NTP servers specified in the “/etc/systemd/timesyncd.conf” configuration file. By default, the timesyncd service uses the well-known NTP server pool, which provides a large number of public NTP servers.
Step 3: Verify Changes
You can verify the status of the NTP synchronization by running the following command:
The output of the command should show the NTP synchronization status, as well as the time offset from the NTP server, if available.
Step 4: Synchronize Hardware Clock
It’s important to note that the timedatectl command only affects the system clock, which is the main clock on the system used by the operating system and applications. The hardware clock, also known as the real-time clock (RTC), is a separate clock that runs independently of the system and is used to keep the time even when the system is powered off. To synchronize the hardware clock with the system clock, you need to run the following command:
sudo timedatectl set-local-rtc 1
In conclusion, the
`timedatectl set-ntp` command is a simple and convenient way to synchronize the Linux system clock with an NTP server, and ensure accurate timekeeping on your system. By using NTP, you can ensure that the system time is consistent and accurate, and avoid potential problems caused by incorrect time settings.