NTP 服務器用於時間同步,局域網內可搭建一台 NTP 服務器,客戶端連接 NTP 服務器獲取時間校准服務,
從而使所有客戶端的時間保持一致.
連接方式: 上游 ntp 服務器(外網) --- ntp 服務器(外網/內網) --- 客戶端(內網)
本地 ntp 服務器從上游 ntp 服務器獲得時間校准服務,然后向本地內網的客戶端提供時間校准服務.
如果上游 ntp 服務不可用(斷網,故障等),則本地 ntp 服務器以本地時間為准,向客戶端提供時間校准服務.
國內推薦使用阿里雲作為上游 ntp 服務器,國外可使用 pool.ntp.org
1 服務器配置
>>> 1.1 安裝 ntp 服務
[root@ ~]#: yum install ntp ntpdate -y
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base | 3.7 kB 00:00
...
Complete! # 安裝完成
>>> 1.2 修改配置文件
[root@ ~]#: cp /etc/ntp.conf /etc/ntp.conf.bak20190903 # 備份
[root@ ~]#: vi /etc/ntp.conf # 清空原有,輸入以下:
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap # 允許客戶端從此處獲取時間
server ntp1.aliyun.com # 使用上游 ntp 服務器 ntp1.aliyun.com 阿里雲
server time1.aliyun.com
server pool.ntp.org
restrict ntp1.aliyun.com nomodify notrap noquery # 允許上游服務器修改本機時間
restrict time1.aliyun.com nomodify notrap noquery
restrict pool.ntp.org nomodify notrap noquery
server 127.127.1.0 # local clock, 本地時鍾,
fudge 127.127.1.0 stratum 10 # 若外部時間服務器不可用,則用本地時間為客戶端提供服務
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
# 保存退出
>>> 1.3 添加 iptables 規則
允許 ntp 服務器監聽 udp 端口 123
[root@ ~]#: cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak20190913 # 備份
[root@ ~]#: vi /etc/sysconfig/iptables # 添加規則
1 # Firewall configuration written by system-config-firewall
2 # Manual customization of this file is not recommended.
...
12 -A FORWARD -j REJECT --reject-with icmp-host-prohibited
13 -A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT # 添加於此
14 COMMIT
# 保存退出
配置參數簡介:
ignore: 關閉所有 ntp 聯機服務
nomodify: 客戶端可通過服務器同步時間,但不能修改服務器的時間
notrap: 拒絕特殊的 ntpdq 捕獲消息
noquery: 拒絕 btodq/ntpdc 查詢
server: 添加上游 ntp 服務器
>>> 1.4 重啟 iptables
[root@ ~]#: /etc/init.d/iptables restart
iptables:將鏈設置為政策 ACCEPT:filter [確定]
iptables:清除防火牆規則: [確定]
iptables:正在卸載模塊: [確定]
iptables:應用防火牆規則: [確定]
>>> 1.5 開啟 ntp 服務
[root@ ~]#: /etc/init.d/ntpd start
正在啟動 ntpd: [確定]
>>> 1.6 查看 ntp 服務器
[root@ ~]#: ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 17 64 1 0.000 0.000 0.000 # 本地 ntp 服務器(自身)
sv1.ggsrv.de 205.46.178.169 2 u 17 64 1 387.291 0.780 0.000 # 上層 ntp 服務器
>>> 1.7 設置 ntpd 服務開機啟動
[root@ ~]#: chkconfig --add ntpd # 添加
[root@ ~]#: chkconfig ntpd on # 開啟
[root@ ~]#: chkconfig --list | grep "ntp" # 查看
ntpd 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉
2 客戶端配置
>>> 2.1 客戶端安裝 ntp 服務
[root@ ~]#: yum install ntp ntpdate -y
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base | 3.7 kB 00:00
...
Complete! # 安裝完成
>>> 2.2 修改配置文件
[root@ ~]#: cp /etc/ntp.conf /etc/ntp.conf.bak20190903 # 備份
[root@ ~]#: vi /etc/ntp.conf # 清空原有,輸入以下:
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
server 192.168.10.61 # NTP 服務器IP
restrict 192.168.10.61 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# 保存退出
>>> 2.3 手動同步時間
[root@ ~]#: ntpdate -d 192.168.10.61 # ntp 服務器IP
...
192.168.10.61: Server dropped: no data # 報錯 no data
server 192.168.10.61, port 123
...
3 Sep 13:11:12 ntpdate[1594]: no server suitable for synchronization found # 報錯 no server
排錯1: 在 ntp 服務器上關閉 iptables
[root@ ~]#: /etc/init.d/iptables stop # 此行在服務器上操作
iptables:將鏈設置為政策 ACCEPT:filter [確定]
iptables:清除防火牆規則: [確定]
iptables:正在卸載模塊: [確定]
再次手動同步時間
[root@ ~]#: ntpdate -d 192.168.10.61 # 此行在客戶端操作
...
Looking for host 192.168.10.61 and service ntp
...
server 192.168.10.61, port 123
...
3 Sep 13:18:06 ntpdate[1606]: adjust time server 192.168.10.61 offset -0.008476 sec
可以正常獲取服務器時間.
排錯2:關閉 iptables 不安全,修改服務器 iptables 配置文件
[root@ ~]#: vi /etc/sysconfig/iptables # 此行在服務器上操作
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
#-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#-A INPUT -p icmp -j ACCEPT
#-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # 保留 ssh
#-A INPUT -j REJECT --reject-with icmp-host-prohibited
#-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # 保留 ssh
-A INPUT -p udp -m udp --dport 123 -j ACCEPT # 添加此規則
COMMIT
# 保存退出
客戶端可以正常獲取服務器時間.
按照其它教程的說明,添加的規則是 -A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
但是添加后無法正常使用,原因不明.
注意,服務器開啟 NTP 服務后,客戶端需要等待幾分鍾之后,才可以從服務器獲取時間,否則報錯.
>>> 2.4 客戶端自動校時
> 2.4.1 新建 log 文件,記錄 ntpdate 運行情況
[root@ logs]#: mkdir -p /etc/logs/ # 新建目錄
[root@ logs]#: touch /etc/logs/ntpdate.log # 新建日志文件
> 2.4.2 使用 contab 計划任務,設置客戶端自動校時.
[root@ logs]#: vi /etc/crontab # 修改配置
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
* */2 * * * /usr/sbin/ntpdate 192.168.10.61 >> /etc/logs/ntpdate.log 2>&1 # 添加此行
# 每兩個小時執行一次命令 /usr/sbin/ntpdate 192.168.10.61 >> /etc/logs/ntpdate.log 2>&1
# 保存退出.
ddns-update-style interim;ignore client-updates;
subnet 192.168.1.0 netmask 255.255.255.224 {
# --- default gateway option routers 192.168.1.254; option subnet-mask 255.255.255.224;
option nis-domain "domain.org"; option domain-name "manage.com"; option domain-name-servers 192.168.1.32;
option time-offset -28800; # Eastern Standard Time# option ntp-servers 192.168.1.1;# option netbios-name-servers 192.168.1.1;# --- Selects point-to-point node (default is hybrid). Don't change this unless# -- you understand Netbios very well# option netbios-node-type 2;
range dynamic-bootp 192.168.1.1 192.168.1.30; default-lease-time 21600; max-lease-time 43200;
# we want the nameserver to appear at a fixed address# host ns {# next-server marvin.redhat.com;# hardware ethernet 12:34:56:78:AB:CD;# fixed-address 207.175.42.254;# }}
