1、個人安裝環境
1.1 CentOS Linux release 7.7.1908 (Core)
1.2 ntp-4.2.6
2、安裝步驟
1. 查看服務器是否安裝ntp,系統默認安裝ntpdate
rpm -qa | grep ntp
執行結果:
fontpackages-filesystem-1.44-8.el7.noarch ntpdate-4.2.6p5-28.el7.centos.x86_64 python-ntplib-0.3.2-1.el7.noarch
2.安裝ntp ntpdate,其中ntpdate默認安裝,可以只安裝ntp
yum install ntp ntpdate -y
3.查看是否已安裝完成,與第2步對比
rpm -qa | grep ntp
執行結果:
fontpackages-filesystem-1.44-8.el7.noarch ntpdate-4.2.6p5-28.el7.centos.x86_64 ntp-4.2.6p5-28.el7.centos.x86_64 python-ntplib-0.3.2-1.el7.noarch
4.查看ntp服務器狀態,兩條命令效果一樣
systemctl status ntpd
執行結果:
● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled) Active: inactive (dead)
or
service ntpd status
執行結果:
Redirecting to /bin/systemctl status ntpd.service ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled) Active: inactive (dead)
5.修改配置文件
使該NTP服務器在不聯網的情況下,使用本服務器的時間作為同步時間
vim /etc/ntp.conf
把如下四行代碼注釋掉
server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst
在下面再添加一行
server 127.127.1.0 iburst
6.重啟ntp服務
systemctl restart ntpd
or
service ntpd restart
7.再次查看服務器狀態
systemctl status ntpd
執行結果:
● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2018-08-21 14:29:12 CST; 8s ago Process: 6588 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 6589 (ntpd) Tasks: 1 CGroup: /system.slice/ntpd.service └─6589 /usr/sbin/ntpd -u ntp:ntp -g Aug 21 14:29:12 web ntpd[6589]: Listen normally on 2 lo 127.0.0.1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 3 eno1 192.168.0.163 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 4 virbr0 192.168.122.1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 5 lo ::1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 6 eno1 fe80::6e92:bfff:fe6f:daea UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listening on routing socket on fd #23 for interface updates Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c016 06 restart Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c011 01 freq_not_set Aug 21 14:29:13 web ntpd[6589]: 0.0.0.0 c514 04 freq_mode
or
service ntpd status
執行結果:
Redirecting to /bin/systemctl status ntpd.service ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2018-08-21 14:29:12 CST; 56s ago Process: 6588 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 6589 (ntpd) Tasks: 1 CGroup: /system.slice/ntpd.service └─6589 /usr/sbin/ntpd -u ntp:ntp -g Aug 21 14:29:12 web ntpd[6589]: Listen normally on 2 lo 127.0.0.1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 3 eno1 192.168.0.163 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 4 virbr0 192.168.122.1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 5 lo ::1 UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listen normally on 6 eno1 fe80::6e92:bfff:fe6f:daea UDP 123 Aug 21 14:29:12 web ntpd[6589]: Listening on routing socket on fd #23 for interface updates Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c016 06 restart Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c011 01 freq_not_set Aug 21 14:29:13 web ntpd[6589]: 0.0.0.0 c514 04 freq_mode
8.查看是否同步
ntpq -p
執行結果:
remote refid st t when poll reach delay offset jitter ============================================================================== *LOCAL(0) .LOCL. 5 l 20 64 7 0.000 0.000 0.000
9.設置開機啟動
systemctl enable ntpd
10.設置防火牆,打開udp123端口
firewall-cmd --permanent --add-port=123/udp firewall-cmd --reload
11.查看防火牆已打開端口
iptables -L -n | grep 123
執行結果:
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:123 ctstate NEW,UNTRACKED
3、windows client 配置
1.先確認可以ping通NTP server
ping 192.168.111.131
執行結果:
正在 Ping 192.168.111.131 具有 32 字節的數據: 來自 192.168.111.131 的回復: 字節=32 時間<1ms TTL=64 來自 192.168.111.131 的回復: 字節=32 時間<1ms TTL=64 來自 192.168.111.131 的回復: 字節=32 時間<1ms TTL=64 來自 192.168.111.131 的回復: 字節=32 時間<1ms TTL=64
2.日期和時間配置
執行“時間和日期” -> Internet時間 -> 更改設置
3.時間服務器配置
勾選“與Internet時間服務器同步”,輸入NTP服務器IP地址,單擊“立即更新”
顯示同步成功
4.特別注意
如果本地PC日期與NTP服務器跨度過大,比如相差16年,同步失敗。可以先手動或者bat腳本調整大概日期,縮減跨度。
4、Linux client 配置
前5步與服務器一致
6.修改配置文件
將剛剛搭建好的NTP服務器作為客戶端上游時間服務器
vim /etc/ntp.conf
內容修改如下:
#注釋掉其他上游時間服務器 #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst #配置上游時間服務器為本地的ntpd Server服務器 server 192.168.111.131 #配置允許上游時間服務器主動修改本機的時間 restrict 192.168.0.163 nomodify notrap noquery
7.與本地ntpd Server同步一下
ntpdate -u 192.168.111.131
8.啟動ntp服務
systemctl start ntpd
or
service ntpd start
9.設置開機啟動
systemctl enable ntpd
10.查看狀態
[root@db3 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *192.168.111.131 LOCAL(0) 11 u 21 64 377 0.068 -0.824 0.367
5、異常處理
1.上文已經設置開機自啟,實際未運行
查找資料發現,服務chrony和服務ntp沖突,導致ntp開機啟動失效。使用systemctl is-enabled chronyd查看chrony的啟動狀態
systemctl is-enabled chronyd
關閉chrony服務的自啟動
systemctl disable chronyd
再重啟服務器后發現ntp服務正常啟動,問題解決。
systemctl status ntpd
運行結果:
ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since 三 2020-02-19 20:03:42 CST; 2min 1s ago Process: 2298 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 2300 (ntpd) Tasks: 1 CGroup: /system.slice/ntpd.service └─2300 /usr/sbin/ntpd -u ntp:ntp -g 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listen normally on 2 lo 127.0.0.1 UD...3 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listen normally on 3 ens33 192.168.1...3 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listen normally on 4 virbr0 192.168....3 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listen normally on 5 lo ::1 UDP 123 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listen normally on 6 ens33 fe80::1ac...3 2月 19 20:03:42 localhost.localdomain ntpd[2300]: Listening on routing socket on fd #2...s 2月 19 20:03:42 localhost.localdomain ntpd[2300]: 0.0.0.0 c016 06 restart 2月 19 20:03:42 localhost.localdomain ntpd[2300]: 0.0.0.0 c012 02 freq_set kernel 0.00...M 2月 19 20:03:42 localhost.localdomain ntpd[2300]: 0.0.0.0 c011 01 freq_not_set 2月 19 20:03:43 localhost.localdomain ntpd[2300]: 0.0.0.0 c514 04 freq_mode Hint: Some lines were ellipsized, use -l to show in full.
參考文檔:https://blog.csdn.net/hellboy0621/article/details/81903091
https://blog.csdn.net/I_Demo/article/details/99673094