方法一
NTP時間同步
NTP基於UDP報文進行傳輸,使用的UDP端口號為123
一、服務端:
假設ip是192.168.100.100
1、安裝ntp
外網:sudo apt install ntp (內網拷貝離線包安裝)
2、修改配置文件:vim /etc/ntp.conf
注釋默認的時間同步源
#pool 127.0.0.1 iburst
#pool 1.debian.pool.ntp.org iburst
#pool 2.debian.pool.ntp.org iburst
#pool 3.debian.pool.ntp.org iburst
增加以下2行
server 127.127.1.0
fudge 127.127.1.0 stratum 8
修改后,保存退出
3、啟動服務並設置服務開機自啟
systemctl start ntp && systemctl enable ntp
4、查看偵聽端口
netstat -tunlp | grep ntp 或者ss -ntulp | grep ntp 一般是在123端口
timedatectl 查看當前機器時間
ip a 查看服務端ip
二、客戶端
客戶端需要安裝ntpdate的包:sudo apt install ntpdate
輸入 ntpdate serverip(服務端配置ntp的ip地址)去同步時間
結果顯示類似: 18 Aug 16:04:24 ntpdate[30646]: step time server 10.20.xx.xx offset 47606319.594686 sec
PS:
1、如果上述操作不成功,可以 ntpdate -d 192.168.100.100 查看報錯詳情,然后再去ntpdate 服務端ip 同步時間
2、ntp客戶端用ntpdate -d serverip查看,發現有192.168.100.100: Server dropped: strata too high
的錯誤,這是因為ntp server還沒有和其自身或者它的server同步上。可以用ntptrace 和ntpd -s -d 命令,再去ntpdate serverip即可。
no server suitable for synchronization found沒有找到適合同步的服務器(在ntp server上重新啟動ntp服務后,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鍾,在這個時間之內在客戶端運行ntpdate命令時會產生no server suitable for synchronization found的錯誤。)
方法二
chrony時間同步
chronyd實現了NTP協議並且可以作為服務器或客戶端。
一、服務端:
假設ip是192.168.18.25
1、安裝chrony
外網:sudo apt install chrony(內網拷貝離線包安裝)
2、修改配置文件
vim /etc/chrony/chrony.conf
#pool 2.debian.pool.ntp.org iburst
server 192.168.18.25 iburst
allow all #允許所有客戶端主機
local stratum 10 #即使自己未能通過網絡時間服務器同步到時間,也允許將本地時間作為標准時間授時給其它客戶端。
3、啟動服務並設置服務開機自啟
systemctl restart chrony
systemctl enable chrony
4、
root@ysl:~# chronyc sources 檢查自己能同步自己
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
^* ysl 10 6 17 9 +599ns[+2836ns] +/- 3857ns
二、客戶端
修改配置文件:
vim /etc/chrony/chrony.conf
#pool 2.debian.pool.ntp.org iburst
server 192.168.18.25 iburst
systemctl restart chrony
systemctl restart chronyd
chronyc sources 檢查是否同步成功,同步成功會有*,如果未同步成功,可以用sudo chronyc -a makestep 強制同步
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
^* 192.168.18.25 11 6 377 30 +629ns[+2835ns] +/- 5801us
注意:使用sudo chronyc -a makestep命令強制同步,在第二次使用這個命令時需要重啟下chrony服務,不然同步不了
問題
說明:剛剛使用systemctl start chrony命令開啟服務的時候,卻開不成功,出現failed to enable unit file /etc/systemd/system/chrony.service is masked的錯誤,經過搜索下后,發現是chrony服務被鎖定了,這里說下方法。
方法
執行命令,即可實現取消firewalld的鎖定
systemctl unmask chrony
解鎖后,就可以再開啟chrony服務了。
方法三
Windows端當作NTP服務器
搭建NTP服務
Windows 10本身是可以作NTP時間同步服務器的,無需安裝其它軟件,只需要修改一些配置。
1.修改注冊表
使用win + R 組合鍵在運行窗口中輸入regedit,打開注冊表編輯器。
依次展開數據項目,計算機\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer,把Enabled設置為1,為打開NTP服務,操作如下圖所示。
依次打開,計算機\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config,並把AnnounceFlags的值設置為5(系統默認為a)。
2.啟動NTP服務
使用win + R 組合鍵在運行窗口中輸入services.msc,打開服務。
在服務項中找到Windows Time
重啟WindowsTime服務,打開開始菜單輸入“CMD”在命令行模式下輸入
net stop w32time
net start w32time 重啟一下win32time服務。
設置為自動后確定,點擊右鍵重新啟動此服務,至此服務啟動成功。
3.本機測試
在cmd窗口中輸入w32tm /stripchart /computer:127.0.0.1 ,如果有回顯則服務正常。
4.部署客戶端。。。