企业内部在centos7.2系统中必杀技NTP时间服务器及内网服务器时间同步(windows和linux客户端同步)...
網(wǎng)絡(luò)時間協(xié)議NTP(Network Time Protocol)是用于互聯(lián)網(wǎng)中時間同步的標(biāo)準(zhǔn)互聯(lián)網(wǎng)協(xié)議。NTP的用途是把計算機的時間同步到某些時間標(biāo)準(zhǔn)。目前采用的時間標(biāo)準(zhǔn)是世界協(xié)調(diào)時UTC(Universal Time Coordinated)。NTP的主要開發(fā)者是美國特拉華大學(xué)的David L. Mills教授。
NTP對于我們個人來說有什么用呢,簡單的講,當(dāng)你的計算機時間不準(zhǔn)確了,你可以接入到互聯(lián)網(wǎng),從網(wǎng)上同步一下時間。
對于企業(yè)來說,當(dāng)你有成百上千的計算機,都不能直接連接互聯(lián)網(wǎng),時間不統(tǒng)一影響業(yè)務(wù),如果一臺一臺的修改,工作量太大。這時搭建一個自己的NTP服務(wù)器就顯出它的優(yōu)勢了。
企業(yè)內(nèi)部架設(shè)NTP服務(wù)器的大概拓?fù)淙缦?#xff1a;
配置好相關(guān)的yum源、確保你的ntp服務(wù)器可以連網(wǎng)
# vim /etc/resolv.conf
#添加網(wǎng)關(guān),這個可以ipconfig查看默認(rèn)網(wǎng)關(guān)
nameserver 192.168.8.251
配置內(nèi)網(wǎng)NTP-Server(192.168.8.100)
1、yum安裝ntp服務(wù)
# yum install -y ntp
配置開機啟動
# chkconfig ntpd on
# chkconfig --list ntpd
ntpd ? ? ? ? ? 0:關(guān)閉1:關(guān)閉2:啟用3:啟用4:啟用5:啟用6:關(guān)閉
2、編輯ntp的配置文件
# vim /etc/ntp.conf 添加如下內(nèi)容就可以了
?
# 允許內(nèi)網(wǎng)網(wǎng)段 192.168.0.0 其他機器同步時間 restrict 192.168.0.0 mask 255.255.0.0 nomodify# 中國這邊最活躍的時間服務(wù)器 : http://www.pool.ntp.org/zone/cn server ntp.api.bz perfer server 210.72.145.44 # 中國國家受時中心 server 202.112.10.36 # 1.cn.pool.ntp.org server 59.124.196.83 # 0.asia.pool.ntp.org # 允許上層時間服務(wù)器主動修改本機時間 restrict ntp.api.bz nomodify notrap noquery restrict 210.72.145.44 nomodify notrap noquery restrict 202.112.10.36 nomodify notrap noquery restrict 59.124.196.83 nomodify notrap noquery# 外部時間服務(wù)器不可用時,以本地時間作為時間服務(wù) server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10
最終的參數(shù):
# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
或者
# cat /etc/ntp.conf | grep "^[^#]"
國內(nèi)常用的時間服務(wù)器列表:
在配置前,先使用ntpdate手動同步下時間,免得本機與外部時間服務(wù)器時間差距太大,讓ntpd不能正常同步
# ntpdate -u ntp.api.bz
13 Apr 14:45:05 ntpdate[11464]: step time server 61.153.197.226 offset 28824.742403 sec
ntp服務(wù)器配置好之后啟動服務(wù)# service ntpd start,它監(jiān)聽的端口是UDP的123端口、可以使用
# netstat -tnulp | grep ntp
udp ? ? ? ?0 ? ? ?0 192.168.8.102:123 ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 11484/ntpd ? ? ? ? ?
udp ? ? ? ?0 ? ? ?0 127.0.0.1:123 ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 11484/ntpd ? ? ? ? ?
udp ? ? ? ?0 ? ? ?0 0.0.0.0:123 ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 11484/ntpd ? ? ? ? ?
udp6 ? ? ? 0 ? ? ?0 fe80::4637:e6ff:fe5:123 :::* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?11484/ntpd ? ? ? ? ?
udp6 ? ? ? 0 ? ? ?0 ::1:123 ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?11484/ntpd ? ? ? ? ?
udp6 ? ? ? 0 ? ? ?0 :::123 ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?11484/ntpd ? ? ? ?
重啟完了之后要等幾分鐘或者十幾分鐘后就可以實現(xiàn)時間同步了,
# ntpdate 192.168.8.100
11 Aug 11:38:30 ntpdate[7619]: adjust time server 10.17.1.60 offset 0.000178 sec
如果重啟后馬上測試就會出現(xiàn)下面的情況:
# ntpdate 192.168.8.100
11 Aug 11:05:28 ntpdate[7326]: no server suitable for synchronization found
可以用這個命令參數(shù)來查看:
# ntpdate -d 192.168.8.100
ntpq -p 查看網(wǎng)絡(luò)中的NTP服務(wù)器,同時顯示客戶端和每個服務(wù)器的關(guān)系
# ntpq -p
? ? ?remote ? ? ? ? ? refid ? ? ?st t when poll reach ? delay ? offset ?jitter
==============================================================================
*61.153.197.226 ?216.218.192.202 ?2 u ? 37 ? 64 ? ?1 ? 47.274 ? 10.405 ? 0.000
?210.72.145.44 ? .INIT. ? ? ? ? ?16 u ? ?- ? 64 ? ?0 ? ?0.000 ? ?0.000 ? 0.000
?gus.buptnet.edu 10.3.9.9 ? ? ? ? 4 u ? 35 ? 64 ? ?1 ? 81.880 ? ?2.559 ? 0.000
?59-124-196-83.H .INIT. ? ? ? ? ?16 u ? ?- ? 64 ? ?0 ? ?0.000 ? ?0.000 ? 0.000
?LOCAL(0) ? ? ? ?.LOCL. ? ? ? ? ?10 l ? ?- ? 64 ? ?0 ? ?0.000 ? ?0.000 ? 0.000
?ntp2.aliyun.com 10.137.38.86 ? ? 2 u ? 22 ? 64 ? ?1 ? 35.176 ? ?4.490 ? 1.640
ntpstat 命令查看時間同步狀態(tài),這個一般需要5-10分鐘后才能成功連接和同步。所以,服務(wù)器啟動后需要稍等下。
剛啟動的時候,一般是:
# ntpstat?
unsynchronised
time server re-starting
polling server every 64 s
連接并同步后:
# ntpstat
synchronised to NTP server (110.75.186.248) at stratum 3?
? ?time correct to within 225 ms
? ?polling server every 64 s
NTP的配置文件:
/etc/ntp.conf ? ? ?NTP服務(wù)的配置文件。
/usr/share/zoneinfo/ ? ? 這個目錄下存放的是各個時區(qū)對應(yīng)的設(shè)定文件。
/etc/localtime ? ?本地系統(tǒng)時間設(shè)定文件。
/etc/sysconfig/clock ? 本機時區(qū)設(shè)定文件。
配置內(nèi)網(wǎng)NTP-Clients
linux-client
內(nèi)網(wǎng)其他設(shè)備作為NTP的客戶端配置,相對就比較簡單,而且所有設(shè)備的配置都相同。
首先需要安裝NTPD服務(wù),然后配置為自啟動(與NTP-Server完全一樣)。然后找其中一臺配置/etc/ntp.conf文件,配置完成驗證通過后,拷貝到其他客戶端機器,直接使用即可。
# yum install -y ntp
# chkconfig ntpd on
# vim /etc/ntp.conf
加入如下內(nèi)容:
# 配置時間服務(wù)器為本地的時間服務(wù)器
server 192.168.8.100
restrict 192.168.8.100 nomodify notrap noquery
server ?127.127.1.0 ? ? # local clock
fudge ? 127.127.1.0 stratum 10
請求服務(wù)器前,請先使用ntpdate手動同步下時間
# ntpdate -u 192.168.8.100
13 Apr 14:49:51 ntpdate[4114]: no server suitable for synchronization found
這里有可能出現(xiàn)同步失敗,一般情況下原因都是本地的NTPD服務(wù)器還沒有正常啟動起來,一般需要幾分鐘時間后才能開始同步。
錯誤判斷請參考后面的錯誤處理。
# service ntpd restart
啟動后,查看同步情況
# ntpq -p
# ntpstat
.....
因為是內(nèi)網(wǎng),一般ntpstat很快就可以同步上,幾分鐘需要等下.
本機客戶端配置完成后,使用SCP拷貝/etc/ntp.conf到其他需要同步的客戶端機器,啟動NTPD服務(wù)即可。
其他客戶端機器上操作配置如下:
# ?ntpdate -u 192.168.8.100
# scp 192.168.8.xxx:/etc/ntp.conf /etc/ntp.conf
# service ntpd start
解決ntp的錯誤 no server suitable for synchronization found
當(dāng)用ntpdate -d 來查詢時會發(fā)現(xiàn)導(dǎo)致 no server suitable for synchronization found 的錯誤的信息有以下2個: ?
錯誤1.Server dropped: Strata too high
在ntp客戶端運行ntpdate serverIP,出現(xiàn)no server suitable for synchronization found的錯誤。
在ntp客戶端用ntpdate –d serverIP查看,發(fā)現(xiàn)有“Server dropped: strata too high”的錯誤,并且顯示“stratum 16”。而正常情況下stratum這個值得范圍是“0~15”。
這是因為NTP server還沒有和其自身或者它的server同步上。
以下的定義是讓NTP Server和其自身保持同步,如果在/ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務(wù)提供給ntp客戶端。
server 127.127.1.0 fudge
127.127.1.0 stratum 8?
?
在ntp server上重新啟動ntp服務(wù)后,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鐘,在這個時間之內(nèi)在客戶端運行ntpdate命令時會產(chǎn)生no server suitable for synchronization found的錯誤。
那么如何知道何時ntp server完成了和自身同步的過程呢?
在ntp server上使用命令:
?
# watch ntpq -p
出現(xiàn)畫面:
?
Every 2.0s: ntpq -p ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Thu Jul 10 02:28:32 2008
? ? ?remote ? ? ? ? ? refid ? ? ?st t when poll reach ? delay ? offset jitter
==============================================================================
?192.168.30.22 ? LOCAL(0) ? ? ? ? 8 u ? 22 ? 64 ? ?1 ? ?2.113 179133. ? 0.001
?LOCAL(0) ? ? ? ?LOCAL(0) ? ? ? ?10 l ? 21 ? 64 ? ?1 ? ?0.000 ? 0.000 ?0.001
?
注意LOCAL的這個就是與自身同步的ntp server。
注意reach這個值,在啟動ntp server服務(wù)后,這個值就從0開始不斷增加,當(dāng)增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數(shù),是64秒*5=320秒的時間。
如果之后從ntp客戶端同步ntp server還失敗的話,用ntpdate –d來查詢詳細(xì)錯誤信息,再做判斷。
錯誤2.Server dropped: no data
從客戶端執(zhí)行netdate –d時有錯誤信息如下:
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出現(xiàn)這個問題的原因可能有2:
?
1。檢查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定義中使用了notrust的話,會導(dǎo)致以上錯誤。
使用以下命令檢查ntp的版本:
# ntpq -c version
下面是來自ntp官方網(wǎng)站的說明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".
In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd
解決:
把notrust去掉。
2。檢查ntp server的防火墻。可能是server的防火墻屏蔽了upd 123端口。
可以用命令
#service iptables stop
來關(guān)掉iptables服務(wù)后再嘗試從ntp客戶端的同步,如果成功,證明是防火墻的問題,需要更改iptables的設(shè)置。
centos7關(guān)閉防火墻方法;
# systemctl stop firewalld.service
禁止隨機啟動
# systemctl disable firewalld.service
windows-client配置
關(guān)于windows客戶端與centos ntp服務(wù)器同步的配置:
1、 打開“開始”,點擊運行輸入gpedit.msc,然后“確定”
2、 進(jìn)入組策略配置界面,單擊“計算機配置”下的管理模板/系統(tǒng)/windows時間服務(wù)/雙擊“時間提供程序”
3、 雙擊“啟動windows NTP 客戶端”,然后選擇”已啟用”選項,點擊“下一設(shè)置”
4、 將“設(shè)置”選項配置如下圖:192.168.8.102,公司內(nèi)部的centos7.2搭建的ntp時間服務(wù)器地址
5、 然后確定即可,雙擊桌面右下角的時間,然后選擇“internet時間”選項
更新組策略:開始/運行/gpupdate /force
報錯:windows在與192.168.8.102同步時出錯
如果沒有啟用windows time,啟動即可
如果已經(jīng)啟動,將服務(wù)重新啟動
實際案例:
美國的服務(wù)器時間和國內(nèi)不一致,交互的時候發(fā)生問題,現(xiàn)在將國內(nèi)和國外的服務(wù)器時間都調(diào)整為一致
1.先查看時區(qū),可以看到是東1區(qū)
# date -R
Thu, 25 May 2017 18:35:59 -0700
看一下時鐘
# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
查看系統(tǒng)默認(rèn)的時區(qū):
[root@u05mix06 Asia]# ll /etc/localtime
lrwxrwxrwx 1 root root 39 May 24 04:02 /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles
2.修改時區(qū)
將Asia/shanghai-上海時區(qū)寫入當(dāng)前時區(qū)
# cp -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y
提示是否覆蓋,輸入Y回車,
然后#date
查看時區(qū)和時間(CST,中國時區(qū))
# date -R
Fri, 26 May 2017 10:08:28 +0800
crontab -e
加入如下任務(wù):
00 6 * * * /usr/sbin/ntpdate -u us.pool.ntp.org | logger -t NTP
表示每天6點進(jìn)行同步一次也可以選擇us.ntp.org.cn 這個美國的時間同步服務(wù)器
將時間寫入硬件
hwclock -w
?
轉(zhuǎn)載于:https://www.cnblogs.com/reblue520/p/6239820.html
總結(jié)
以上是生活随笔為你收集整理的企业内部在centos7.2系统中必杀技NTP时间服务器及内网服务器时间同步(windows和linux客户端同步)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hybrid框架UI重构之路:一、师其长
- 下一篇: linux下C调用lua的第一个程序