NTP同步方式在Linux下一般兩種:使用ntpdate命令直接同步和使用NTPD服務(wù)平滑同步。
使用ntpdate命令直接同步的方式存在風(fēng)險(xiǎn),比如一些定時(shí)任務(wù)在已有時(shí)間內(nèi)執(zhí)行過,直接同步導(dǎo)致時(shí)間變回任務(wù)執(zhí)行前的時(shí)間段,定時(shí)任務(wù)會(huì)重復(fù)執(zhí)行。
使用NTPD服務(wù)平滑同步的方式不會(huì)讓一個(gè)時(shí)間點(diǎn)在一天內(nèi)經(jīng)歷兩次,而是平滑同步時(shí)間,它每次同步時(shí)間的偏移量不會(huì)太陡,是慢慢來的。
查看是否安裝NTP包
rpm -qa |grep ntp
如果有輸出ntp和ntpdate版本信息,即已安裝。
ntp 可用systemctl管理
# 查看服務(wù)狀態(tài)service ntpd status# 啟動(dòng)ntpd服務(wù)systemctl start ntpd.service# 停止ntpd服務(wù)systemctl stop ntpd.service# 設(shè)置開機(jī)自啟動(dòng)systemctl enable ntpd.service# 停止開機(jī)自啟動(dòng)systemctl disable ntpd.service# 查看服務(wù)當(dāng)前狀態(tài)systemctl status ntpd.service# 重新啟動(dòng)服務(wù)systemctl restart ntpd.service# 查看所有已啟動(dòng)的服務(wù)systemctl list-units –type=service
作為ntp客戶端,如何同步服務(wù)器端的時(shí)間?
以公用的一些時(shí)間同步服務(wù)器為例
systemctl stop ntpd # 使用ntpdate同步前需要關(guān)閉ntpd服務(wù),不然會(huì)失敗ntpdate cn.pool.ntp.orgsystemctl enable ntpd.servicesystemctl start ntpd.service# cn.pool.ntp.org 中國開源免費(fèi)NTP服務(wù)器# ntp1.aliyun.com 阿里云NTP服務(wù)器# ntp2.aliyun.com 阿里云NTP服務(wù)器# time1.aliyun.com 阿里云NTP服務(wù)器# time2.aliyun.com 阿里云NTP服務(wù)器
如何配置服務(wù)器作為內(nèi)網(wǎng)內(nèi)的時(shí)間同步服務(wù)器?(NTP服務(wù)器的配置)
配置內(nèi)網(wǎng)NTP-Server(10.0.0.12)
配置NTPD服務(wù)的服務(wù)器需要能訪問外網(wǎng),這里挑選了一臺(tái)可以訪問外網(wǎng)的Linux服務(wù)器配置內(nèi)網(wǎng)的NTPD服務(wù),作為NTP-Server,其他幾臺(tái)內(nèi)網(wǎng)通過它來進(jìn)行時(shí)間同步。
這里假設(shè)其IP為10.0.0.12,其他幾臺(tái)內(nèi)網(wǎng)的服務(wù)器IP分別為10.0.0.13、10.0.0.14。
在配置NTPD服務(wù)之前,先手動(dòng)同步一下時(shí)間
systemctl stop ntpd # 使用ntpdate同步前需要關(guān)閉ntpd服務(wù),不然會(huì)失敗ntpdate cn.pool.ntp.orgsystemctl enable ntpd.servicesystemctl start ntpd.service
修改NTPD服務(wù)的配置文件/etc/ntp.conf
[root@localhost ~]# vim /etc/ntp.conf#和上層NTP服務(wù)器頻率誤差的記錄文件driftfile /var/lib/ntp/drift#對于默認(rèn)的client拒絕所有操作,下面的restrict開通指定權(quán)限restrict default nomodify notrap nopeer noquery#允許本機(jī)地址的一切操作restrict 127.0.0.1restrict ::1#允許192.168.203段機(jī)器同步時(shí)間,指定可以和NTP通信的IP或者網(wǎng)段restrict 192.168.203.0 mask 255.255.255.0 nomodify notrap#遠(yuǎn)程的同步時(shí)間服務(wù)器地址(需要同步外網(wǎng)時(shí)間必改)#prefer表示優(yōu)先同步的主機(jī)server cn.pool.ntp.org preferserver ntp1.aliyun.com iburstserver ntp2.aliyun.com iburstserver ntp3.aliyun.com iburstserver ntp4.aliyun.com iburstserver ntp5.aliyun.com iburst#遠(yuǎn)程時(shí)間服務(wù)器不可用時(shí),以本地時(shí)間作為服務(wù)時(shí)間server 127.127.1.0#stratum指定層數(shù),越小,離時(shí)間源,越近。 層級1為國際時(shí)間源服務(wù)器…fudge 127.127.1.0 stratum 10#允許上層服務(wù)器主動(dòng)修改本機(jī)時(shí)間(需要同步外網(wǎng)時(shí)間必改)restrict cn.pool.ntp.org nomodify notrap noqueryrestrict ntp1.aliyun.com iburst nomodify notrap noqueryrestrict ntp2.aliyun.com iburst nomodify notrap noqueryrestrict ntp3.aliyun.com iburst nomodify notrap noqueryrestrict ntp4.aliyun.com iburst nomodify notrap noqueryrestrict ntp5.aliyun.com iburst nomodify notrap noqueryincludefile /etc/ntp/crypto/pw#指定日志文件logfile /var/log/ntp#指定日志級別,info、all、event三種可以選擇logconfig all#啟用公鑰加密#crypto#密鑰文件keys /etc/ntp/keys
設(shè)置ntp client客戶端配置文件
vim /etc/ntp.confserver 10.0.0.12
重啟ntp服務(wù)
[root@localhost /]# systemctl stop firewalld[root@localhost /]# setenforce 0[root@localhost /]# vim /etc/seLinux/configSELinux=disabled[root@localhost /]# systemctl restart ntpd
查看ntp服務(wù)同步狀況
ntpd -q