Realize LAN-based NTP clock synchronization

 

1. Build a LAN environment

2. Select a node as the NTP server

For example, select the 192.168.1.149 node to deploy NTP and configure it as an NTP server

#相关命令
sudo apt-get install ntp #安装
sudo systemctl enable ntpd #自启动
systemctl is-enabled ntp #检查自启动是否开启
sudo systemctl start ntp #手启动
service ntp status #ntp 状态
ntpq -p #ntp状态
netstat -tlunp | grep ntp #查看启动的端口(123)
firewall-cmd --zone=public --add-port=123/udp --permanent && firewall-cmd --reload #开放端口 123
#ntp.conf
# /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
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #放行局域网来源,允许192.168.1.x网段中的服务器访问本ntp服务器进行时间同步


# 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

#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

server time.cloudflare.com prefer    # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)
#fudge 127.127.22.1 flag3 1            # enable PPS API
server 127.127.1.0   #local clock,和本地系统时间同步
fudge  127.127.1.0  stratum  10   #127.127.1.0为第10层。ntp和127.127.1.0同步完后,就变成了11层。ntp是层次阶级的。同步上层服务器的stratum大小不能超过或等于16
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

 

3. Manual synchronization of the remaining nodes

ntpdate 192.168.1.149

4. The remaining nodes are synchronized through chrony 

5. Example

mount -o remount -w / #挂载Linux系统外的文件,致其可修改
vi /etc/chrony.conf #修改配置文件
server 192.168.1.149 iburst minpoll 2 prefer
makestep 1.0 -1
sync # sync 指令会将存于 buffer 中的资料强制写入硬盘中。
ps -ef | grep chronyd
kill 4999
chronyd -d &
chronyc sources

6. Common problems

  • ntp does not start automatically 
  • Turn off chronyd; turn off clock acquisition in Ubuntu system settings

7. References

Set LAN NTP time synchronization : https://blog.csdn.net/s_p_j/article/details/88386981

Guess you like

Origin blog.csdn.net/qq_41854291/article/details/108011650