本文測試環境以下:python
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@localhost ~]#
[root@localhost ~]# yum install ntp #等待安裝完成後執行 [root@localhost ~]# rpm -qa | grep "ntp" ntpdate-4.2.6p5-28.el7.centos.x86_64 ntp-4.2.6p5-28.el7.centos.x86_64 [root@localhost ~]#
首先演示ntp 客戶端安裝。centos
#部分系統須要安裝 [root@localhost ~]# yum install ntpdate
本文推薦兩個ntp服務器。服務器
#國內 ntp.aliyun.com ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com time1.aliyun.com time2.aliyun.com #國外 cn.ntp.org.cn 0.cn.ntp.org.cn 1.cn.ntp.org.cn 2.cn.ntp.org.cn 3.cn.ntp.org.cn pool.ntp.org asia.pool.ntp.org
安裝完成後便可使用 ntpdate 同步時間。dom
[root@localhost ~]# ntpdate ntp.aliyun.com 22 Feb 10:32:03 ntpdate[7535]: adjust time server 203.107.6.88 offset -0.005418 sec [root@localhost ~]#
能夠將同步命令做爲一個定時執行任務。命令以下測試
下文演示搭建ntp服務器。rest
#檢查ntp 安裝狀況,不是ntpdate [root@localhost ~]# rpm -qa |grep ntp ntpdate-4.2.6p5-28.el7.centos.x86_64 [root@localhost ~]#
將時間同步做爲定時任務。日誌
#10點38分(不能顛倒,不然報錯) ***(每一年每個月天天) 命令所在位置 服務器域名或ntp服務器ip地址 輸出日誌 38 10 * * * /usr/sbin/ntpdate ntp.aliyun.com >>/usr/local/logs/crontab/ntpdate.log
下文演示ntp服務器搭建。code
[root@localhost ~]# yum install ntp #等待安裝完成執行命令 [root@localhost ~]# rpm -qa | grep "ntp" ntpdate-4.2.6p5-28.el7.centos.x86_64 ntp-4.2.6p5-28.el7.centos.x86_64 [root@localhost ~]#
重啓服務。server
[root@localhost ~]# service ntpd restart Redirecting to /bin/systemctl restart ntpd.service #顯示NTP服務器節點 [root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 203.107.6.88 100.107.25.114 2 u 2 64 1 54.932 21.875 0.000 106.247.248.106 141.223.182.106 2 u 2 64 1 71.810 16.884 0.000 *LOCAL(0) .LOCL. 5 l 2 64 1 0.000 0.000 0.000 [root@localhost ~]#
關閉防火牆或添加規則,ntp 默認使用UDP 123端口,關閉SELinux。crontab
[root@localhost ~]# service firewalld stop #添加規則 [root@localhost ~]# firewall-cmd --zone=public --add-port=123/udp --permanent success [root@localhost ~]# service firewalld restart Redirecting to /bin/systemctl restart firewalld.service [root@localhost ~]# service firewalld status Redirecting to /bin/systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 五 2019-02-22 12:23:22 CST; 3s ago Docs: man:firewalld(1) Main PID: 7888 (firewalld) CGroup: /system.slice/firewalld.service └─7888 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 2月 22 12:23:22 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon. 2月 22 12:23:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 2月 22 12:23:22 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. [root@localhost ~]#
使用客戶端同步,客戶端IP爲192.168.216.136。
[root@localhost ~]# ntpdate -d 192.168.216.137 22 Feb 12:17:06 ntpdate[7901]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 192.168.216.137 and service ntp host found : 192.168.216.137 transmit(192.168.216.137) receive(192.168.216.137) transmit(192.168.216.137) receive(192.168.216.137) transmit(192.168.216.137) receive(192.168.216.137) transmit(192.168.216.137) receive(192.168.216.137) server 192.168.216.137, port 123 stratum 6, precision -25, leap 00, trust 000 refid [192.168.216.137], delay 0.02591, dispersion 0.00000 transmitted 4, in filter 4 reference time: e019f558.93ebd55b Fri, Feb 22 2019 12:13:12.577 originate timestamp: e019f64a.8635f3c7 Fri, Feb 22 2019 12:17:14.524 transmit timestamp: e019f64a.8f182003 Fri, Feb 22 2019 12:17:14.558 filter delay: 0.02597 0.02600 0.02591 0.02603 0.00000 0.00000 0.00000 0.00000 filter offset: -0.03505 -0.03503 -0.03503 -0.03500 0.000000 0.000000 0.000000 0.000000 delay 0.02591, dispersion 0.00000 offset -0.035035 22 Feb 12:17:14 ntpdate[7901]: adjust time server 192.168.216.137 offset -0.035035 sec [root@localhost ~]# ntpdate 192.168.216.137 22 Feb 12:17:28 ntpdate[7902]: adjust time server 192.168.216.137 offset -0.035002 sec [root@localhost ~]# #重啓防火牆 [root@localhost ~]# systemctl restart firewalld.service [root@localhost ~]# [root@localhost ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 五 2019-02-22 12:23:22 CST; 9min ago Docs: man:firewalld(1) Main PID: 7888 (firewalld) CGroup: /system.slice/firewalld.service └─7888 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 2月 22 12:23:22 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon. 2月 22 12:23:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 2月 22 12:23:22 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. [root@localhost ~]#
客戶端添加自動執行任務。
[root@localhost ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@localhost ~]# #規則 0 0 * * * /usr/sbin/ntpdate 192.168.216.137 >>/data/logs/ntp.log 2>&1