天天看點

實作基于LAN的NTP時鐘同步1.搭建LAN環境2.選取某個節點作為 NTP伺服器3.其餘節點手動同步4.其餘節點通過 chrony 實作同步 5.示例6.常見問題7.參考文獻

1.搭建LAN環境

2.選取某個節點作為 NTP伺服器

例如 選取 192.168.1.149節點部署NTP,配置為NTP伺服器

#相關指令
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.其餘節點手動同步

ntpdate 192.168.1.149
           

4.其餘節點通過 chrony 實作同步 

實作基于LAN的NTP時鐘同步1.搭建LAN環境2.選取某個節點作為 NTP伺服器3.其餘節點手動同步4.其餘節點通過 chrony 實作同步 5.示例6.常見問題7.參考文獻

5.示例

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.常見問題

  • ntp未自啟動 
  • 關閉 chronyd ; 關閉 Ubuntu 系統設定裡面的時鐘擷取

7.參考文獻

設定區域網路NTP對時:https://blog.csdn.net/s_p_j/article/details/88386981

V2X