Use NTP in Ubuntu for time synchronization Settings

  • 2020-05-30 21:56:13
  • OfStack

NTP is an TCP/IP protocol that synchronizes time over the network. Usually the client requests the current time from the server and sets its clock based on the result.

While this description is fairly simple, implementing this functionality is extremely complex - there are multiple layers of NTP servers, layer 1 NTP servers connected to the atomic clock, and layer 2 and 3 servers responsible for load balancing to handle all requests from the Internet. In addition, the client may be more complex than you think - it must eliminate communication delays and adjust the time without interfering with other processes running on the server. Fortunately, all of this complexity is encapsulated and you don't need to see it.

In Ubuntu, yes ntpdate and ntpd To synchronize the time.

timedatectl

In the latest version of Ubuntu, timedatectl Instead of the old one ntpdate . By default, timedatectl The time will be synchronized immediately when the system is started and checked again by socket later when the network connection is activated.

If installed ntpdate / ntp . timedatectl It will back you up and let you use the previous Settings. This ensures that the two time synchronization services do not conflict with each other, while retaining the original behavior and configuration when you upgrade. But it also means upgrading from an older release ntp / ntpdate It will still be installed, thus causing the new systemd-based time service to be disabled.

timesyncd

In the latest version of Ubuntu, ntpd 0 Replaced the ntpd The client part. By default ntpd 0 Periodically check and synchronize the time. It also stores the update times locally so that you can step through them when the system restarts.

through timedatectl and timesyncd Sets the current time state and time configuration that can be used timedatectl status Command to confirm.


timedatectl status
   Local time: Fri 2016-04-29 06:32:57 UTC
 Universal time: Fri 2016-04-29 06:32:57 UTC
    RTC time: Fri 2016-04-29 07:44:02
    Time zone: Etc/UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no

If NTP is installed, use it instead timedatectl To synchronize the time, then NTP synchronized Will be set to yes .

timedatectl and ntpd 0 The nameserver used to get the time can be passed /etc/systemd/timesyncd.conf To specify in addition /etc/systemd/timesyncd.conf.d/ There are also flexible additional configuration files.

ntpdate

Due to the timedatectl The distribution has been deprecated ntpdate , the installation is no longer performed by default. If you do, it will set the time of your computer based on Ubuntu's NTP server when the system starts. Then every time a new network interface is started, it will try again to synchronize the time - during which it will slowly offset the time as long as the time difference it covers is not too great. The behavior can be passed -B / -b Switch to control.


ntpdate ntp.ubuntu.com

Time server

By default, systemd-based tools follow from ntp.ubuntu.com Request time synchronization. Classical based on ntpd The services are basically all used [0-3].ubuntu.pool.ntp.org In the pool ntpdate0 , as well as ntpdate1 , as well as support for IPv6 if needed. If you want to force IPv6, use it ntpdate2 , but this is not the default configuration.

ntpd

The daemon for ntp ntpd It calculates the time offset of your system's clock and continuously adjusts it so that there are no large corrections to the time difference, for example, resulting in discontinuous logs. This process only costs a small amount of process resources and memory, but is really trivial for a modern server.

The installation

To install ntpd, type on the terminal command line:


sudo apt install ntp

configuration

The editor /etc/ntp.conf -- add/remove server Line. The following servers are configured by default:


# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org

After you modify the configuration file, you need to reload it ntpd :


sudo systemctl reload ntp.service

Check the status

use ntpq To see more information:


# sudo ntpq -p
   remote      refid   st t when poll reach  delay  offset jitter
========================================================
+stratum2-2.NTP. 129.70.130.70  2 u  5  64 377  68.461 -44.274 110.334
+ntp2.m-online.n 212.18.1.106   2 u  5  64 377  54.629 -27.318 78.882
*145.253.66.170 .DCFa.      1 u  10  64 377  83.607 -30.159 68.343
+stratum2-3.NTP. 129.70.130.70  2 u  5  64 357  68.795 -68.168 104.612
+europium.canoni 193.79.237.14  2 u  63  64 337  81.534 -67.968 92.792

PPS support


Related articles: