Ubuntu enables NTP time synchronization

Ubuntu uses timesyncd to enable NTP time synchronization, and replaces it with ntpd step-by-step gradual correction of time.

The environment is Ubuntu 22.04 LTS

Time zone

In Ubuntu 20.04 and later versions, the built-in time synchronization function is activated by default using systemdthe timesyncdservice. timesyncdReplaces the functionality ntpdateof .

Check current time zone

Order:

timedatectl status

The output of the command is

               Local time: Mon 2022-12-12 23:35:24 CST
           Universal time: Mon 2022-12-12 15:35:24 UTC
                 RTC time: Mon 2022-12-12 15:35:28
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: n/a
          RTC in local TZ: no

System clock synchronized: noReflects that there is no successful synchronization with the remote NTP server, NTP service: n/awhich means that timesyncdit is not up and running. RTC in local TZ: noIndicates that the hardware clock (RTC) is set to Coordinated Universal Time (UTC), yes indicates that the hardware clock is set to local time.

The output shows that the NTP service is not activated, so start the NTP service

sudo timedatectl set-ntp on

Check the status again after successful opening, the output is

System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

If there is an error message when opening Failed to set ntp: NTP not supported, you can check systemd-timesyncdwhether the software package has been installed. If not, use package management to install, and then try to open timedatectl again after installation.

apt list --installed | grep systemd-timesyncd
sudo apt-get install systemd-timesyncd

set time zone

Time zone naming conventions generally use the format "Region/City"

List all available timezones:

timedatectl list-timezones

Modify time zone:

sudo timedatectl set-timezone Asia/Shanghai

Modify the NTP time synchronization server

NTP server (Network Time Protocol) is a protocol used to synchronize computer time. The NTP server ensures that time stamps between different systems are kept in sync.

On freshly installed ubuntu systems, the NTP server for systemd-based tools defaults to ntp.ubuntu.com. Check the system log, sometimes there will be a time-out record:

systemd-timesyncd[354]: Timed out waiting for reply from 91.189.94.4:123 (ntp.ubuntu.com).

To modify the time synchronization server, you need to modify the configuration file/etc/systemd/timesyncd.conf

sudo vim /etc/systemd/timesyncd.conf

The default content of this file is as follows:

# ...

[Time]
#NTP=
#
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

[Time]Uncomment the following. NTPIt is the master time synchronization server FallbackNTPand the backup server.

[Time]
NTP=ntp.tencent.com
FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

restart service

service systemd-timesyncd restart

Incrementally update time using ntpd

timesyncd(and ntpdate) are breakpoint updates, and ntpd is a step-by-step gradual correction of time without time jumps. Also some applications may be sensitive to any disturbance of timing. ntpd uses sophisticated techniques to keep the system's time constantly and incrementally.

close timesyncd

ntpdBefore installing , it needs to be closed timesyncdto prevent conflicts between these two services.

sudo timedatectl set-ntp no

Use timedatectl statusto check if the output is present NTP service: inactive. It means timesyncdstopped.

Install ntpd:

Install the package using apt ntp:

sudo apt update
sudo apt install ntp

ntpdwill automatically start running and working after your installation is complete.

ntpdIt may take several minutes to establish a connection.

Check the running status of ntpd:

systemctl status ntp

Check whether the NTP service UDP 123port is being monitored normally:

netstat -nupl

ntpdDetailed status information for the query :

ntpq -p

ntpqis a query tool ntpdfor . The -p flag asks for information about ntpdthe connected NTP server.

If prompted No association ID's returned, there is probably a problem with the configuration file.

configure ntpd

vim /etc/ntp.conf

The original configuration file is as follows:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# 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.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

After modifying the configuration file, you need to reload ntpd

Systemctlis a systemdtool that is responsible for controlling systemdthe system and service management programs. Systemdis a collection of system management daemons, tools, and libraries that function as a central management and configuration platform for Unix-like systems.

systemctl reload ntp.service 

Reference link:

How To Set Up Time Synchronization on Ubuntu 20.04 - DigitalOcean

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128675919