How to synchronize Linux time with NTP server

Computer clocks are not perfect. Given a few days, weeks or months, they will drift and stop showing real time. In short, when they drift, they may appear as "10:30", but in fact it is "10:33". On older computers, it is common practice to manually readjust the computer clock periodically. However, after the ubiquitous Internet connection, modern operating systems began to automatically adjust their clocks with the help of NTP servers.

What is NTP?

NTP is the abbreviation of Network Time Protocol. It is an algorithm designed to synchronize computer clocks through a network connection and keep them accurate.

How to enable time synchronization on Linux

Fortunately, most distributions implement one or more forms of time synchronization out of the box. If you never find that your computer clock deviates from the phone clock, you almost certainly have an NTP client/daemon running.

On Linux OS with systemd

Most Linux distributions use systemd and come with a "systemd-timesyncd" daemon. This means that you have pre-installed the NTP client on Ubuntu, Debian, Fedora, Arch Linux, openSUSE, Manjaro, etc. On these distributions and other distributions based on them (for example, base operating system, Zorin operating system), run the following command to check whether NTP synchronization is enabled:

timedatectl

If Network time on: yes is seen in the output, the computer clock will be adjusted automatically and periodically via NTP. Sometimes you may also notice NTP synchronized: no, which may mean that the clock has been synchronized by other tools other than "systemd-timesyncd". This may also mean that systemd-timesyncd is not yet synchronized, but will be synchronized later.

If you see Network time on: no, run the following command to enable NTP time synchronization.

timedatectl set-ntp true

These are all the work you have to do.

On Linux OS without systemd

As MX Linux has become so popular lately, examples of this OS seem to be a good start. This is also a special case. It "kind" has systemd, but "kind" does not . timedatectl reports network time "on", but systemd-timesyncd is not used.

So how to synchronize? You can check with the next command, which can be used on any other Linux operating system.

cat / var / log / syslog | grep ntp

On MX Linux, you will get results similar to the image below.

From this filtered log message, you can see that the "ntpdate" client ran once. This is a popular client that is installed in many Linux distributions by default. It is lightweight and only occasionally queries the NTP server. It does not run in the background, but is triggered by a script, synchronizes and then exits.

This means that time will not be synchronized with extremely high precision (think milliseconds or microseconds), but it is accurate enough for most purposes. Usually it is accurate, at least to the second.

On other operating systems, you may see "ntpd", which is a daemon that runs in the background and is very accurate. The more it runs, the more accurate it becomes.

If for some reason you want to install systemd ntp or ntpdate to use systemd on it, first disable systemd's ntp client.

timedatectl set-ntp false

Of course, the above commands are not necessary on non-system distributions.

Install ntpdate client

This applies to home computers, laptops and other devices that are not frequently connected to the Internet.

On Debian, Ubuntu, Linux Mint, Zorin and other distributions based on these, run:

apt  install ntpdate

In all of the Debian-based distributions mentioned above, configure it to run after seeing a new network connection. If you disconnect from the network and reconnect, you can force time synchronization immediately.

On Fedora, ntpdate is not recommended, so it is recommended that you use the ntpd daemon instead (see the next section). It is recommended to use the same features for all other distributions for two reasons:

They sometimes do not include ntpdate in a separate package, but bundle it with ntpd in the next section.

They will not automatically make ntpdate run automatically on every network connection. You must insert ntpdate in the script that runs automatically after the network connection.

In this case, it makes more sense to install ntpd as described in the next section.

Install the ntpd daemon

On Debian, Ubuntu, Zorin, Mint and other Debian or Ubuntu-based distributions, run:

apt  install ntp

These distributions will start ntpd immediately, and then automatically start each time it starts. You do not need to take further measures.

On Fedora, install:

dnf install ntp

On CentOS, run:

yum install ntp

For Arch Linux, use:

pacman -S ntp

Then enter on openSUSE:

zypper install ntp

After that, enable the service to start on boot, and then start it:

systemctl enable ntpd

systemctl startntpd

On other distributions without systemd, you may have to run the following command:

update-rc.d ntpd enable

sevice ntpd start

Or anything equivalent to your operating system.

If you receive an error message saying that ntpd does not exist, please replace "ntpd" with "ntp" in the above command. Some distributions have different names for the ntpd daemon service files.

in conclusion

It is rarely necessary to perform any of the following steps at least on a Linux OS with a graphical user interface. Time synchronization should already be configured for you. But these steps can at least help you debug the problem sometimes. Many desktop environment control panels allow you to enable and disable time synchronization. They use similar commands in the background to configure this command for you, so when the graphical user interface tool fails, you can do this yourself.

 

Guess you like

Origin blog.csdn.net/allway2/article/details/108545869