Issue

Sometimes, our Linux servers may encounter issues where the system time is out of sync with the actual time, resulting in various errors. Today, we will explain how to fix this problem.

This article will use Debian Linux system as an example.

Time Zone

If the time on the server is off by a multiple of an hour, such as one hour or two hours, it is likely a result of incorrect time zone configuration on the server. You can check the status by entering the following command:

If you provide the output of the command “timedatectl status”, I can help you check if the time zone is set correctly. Please provide the result for further assistance.

If the time zone is not correct, you can use the following command to set the time zone:

timedatectl set-timezone Asia/Shanghai

You can use the following command to view the supported time zones by the system:

timedatectl list-timezones

NTP Server

If the time difference between the server and the actual time is not a multiple of an hour, it could be a problem with the NTP (Network Time Protocol) server. The system uses debian.pool.ntp.org by default, but in some regions, there may be connectivity issues. In such cases, manual modification of the NTP synchronization server is required.

You can use the following command to edit the “/etc/systemd/timesyncd.conf” file:

nano /etc/systemd/timesyncd.conf

To locate the NTP line under the [Time] section, remove the “#” character at the beginning, and add several common NTP servers separated by spaces:

After making the modifications, press Ctrl + O to save the changes, and then press Ctrl + X to exit the text editor.

To restart the time synchronization service, you can use the following command:

systemctl restart systemd-timesyncd

This will restart the systemd-timesyncd service and apply the new NTP server settings.

To enable NTP synchronization on the server, you can use the following command:

timedatectl set-ntp true

After enabling NTP synchronization, you can check if the NTP servers are configured correctly by using the following command:

timedatectl timesync-status

To verify if the time is now synchronized correctly, you can use the date command:

date -R