Linux NTP服務器搭建部署與NTP時間同步配置


Linux系統時區設置

timedatectl set-timezone Asia/Shanghai (這里是設置亞洲上海上海)

Linux NTP服務器部署安裝及配置

一、linux NTP服務器部署安裝

1、驗證服務器端NTP服務是否已安裝

[root@vm ~]# rpm -qa ntp*    
ntpdate-4.2.6p5-12.el6.centos.2.x86_64
ntp-4.2.6p5-12.el6.centos.2.x86_64

2、若系統為安裝ntp服務 ,可通過rpm或yum進行安裝

[root@vm ~]# yum install ntp -y
[root@vm ~]# rpm -ql ntp   
/etc/ntp.conf    # ntp服務器的主配置文件
/etc/rc.d/init.d/ntpd    # 開機啟動ntpd腳本文件

3、國內穩定NTP時間同步服務器域名

times.aliyun.com
ntp.aliyun.com

4、使用ntpdate同步時間服務器

[root@vm ~]# ntpdate times.aliyun.com
17 Oct 10:05:27 ntpdate[26878]: step time server 120.25.115.19 offset 4.115593 sec

二、linux NTP服務配置

1、NTP服務 ntp.conf 參數詳解
默認ntp.conf內容如下

[root@vm ~]# vi /etc/ntp.conf

#系統時間與BIOS事件的偏差記錄
driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery    # 拒絕所有IPv4的client連接此NTP服務器
restrict -6 default kod nomodify notrap nopeer noquery    # 拒絕所有IPv6的client連接此NTP服務器


restrict 127.0.0.1    # 放行本機localhost對NTP服務的訪問
restrict -6 ::1


# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap    # 放行192.168.1.0網段主機與NTP服務器進行時間同步

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

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

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys
----
各項參數詳解

利用restrict 來管理權限控制
Restrict [IP] mask [netmask_IP] [parameter]

Parameter 的
ignore :拒絕所有類型的NTP聯機。
nomodify: 客戶端不能使用ntpc與ntpq這兩個程序來修改服務器的時間參數,但客戶端可透過這部主機來進行網絡校時;
noquery:客戶端不能夠使用ntpc與ntpq等指令來查詢時間服務器,不提供NTP的網絡校時。
notrap:不提供trap 這個運程事件登入的功能。
notrust:拒絕沒有認證的客戶端。
Kod:kod技術可以阻止“Kiss of Death “包對服務器的破壞。
Nopeer:不與其他同一層的NTP服務器進行時間同步。

利用server 設定上層NTP服務器,格式如下:
server [IP or hostname] [prefer]

參數主要如下:
perfer:表示優先級最高
burst :當一個運程NTP服務器可用時,向它發送一系列的並發包進行檢測。
iburst :當一個運程NTP服務器不可用時,向它發送一系列的並發包進行檢測。


2、配置NTPserver
調整NTP配置文件\

[root@vm ~]# vi /etc/ntp.conf

Hosts on local network are less restricted.

允許局域網內設備與這台服務器進行同步時間.但是拒絕讓他們修改服務器上的時間

restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

允許上層時間服務器修改本機時間

restrict times.aliyun.com nomodify
restrict ntp.aliyun.com nomodify
restrict cn.pool.ntp.org nomodify

定義要同步的時間服務器

server times.aliyun.com iburst prefer # prefer表示為優先,表示本機優先同步該服務器時間
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst

logfile /var/log/ntpstats/ntpd.log # 定義ntp日志目錄
pidfile /var/run/ntp.pid # 定義pid路徑

Key file containing the keys and key identifiers used when operating

with symmetric key cryptography.

keys /etc/ntp/keys

ntp服務啟動

[root@vm ~]# chkconfig ntpd on

[root@vm ~]# service ntpd start
Starting ntpd: [ OK ]

[root@vm ~]# ss -tunlp | grep ntp
udp UNCONN 0 0 192.168.23.100:123 : users:(("ntpd",24250,19))
udp UNCONN 0 0 127.0.0.1:123 : users:(("ntpd",24250,18))
udp UNCONN 0 0 :123 : users:(("ntpd",24250,16))
udp UNCONN 0 0 fe80::92b1:1cff:fe10:3a44:123 :::
users:(("ntpd",24250,21))
udp UNCONN 0 0 ::1:123 ::😗 users:(("ntpd",24250,20))
udp UNCONN 0 0 :::123 ::😗 users:(("ntpd",24250,17))


3、實現clock時間與system時間同步,配置/etc/sysconfig/ntpd文件

ntp服務,默認只會同步系統時間。如果想要讓ntp同時同步硬件時間,可以設置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可以讓硬件時間與系統時間一起同步。

[root@vm ~]# vi /etc/sysconfig/ntpd # 實現硬件時間與系統時間同步

Drop root to id 'ntp:ntp' by default.

OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes


4、查看ntp服務器同步狀態

[root@vm ~]# ntpstat # 確認本地NTP與上層NTP服務器是否聯通
synchronised to NTP server (120.25.115.19) at stratum 10 #本NTP服務器層次為10,已向120.25.115.19 NTP同步過
time correct to within 33 ms # 時間校正到相差33ms之內
polling server every 256 s # 每256秒會向上級NTP輪詢更新一次時間

[root@vm ~]# ntpq -p # 查看本地NTP需進行同步的公網NTP服務器狀態
remote refid st t when poll reach delay offset jitter

*203.107.6.88 100.107.25.114 2 u 306 1024 377 15.067 -0.283 1.171
+120.25.115.19 10.137.53.7 2 u 979 1024 377 36.893 -0.671 1.219
+119.28.183.184 100.122.36.196 2 u 1036 1024 377 49.942 7.536 18.648


參數詳解

remote :本地主機所連接的上層NTP服務器,最左邊的符號如下:
如果有[*] 代表目前正在使用當中的上層NTP服務器。
如果有[+] 代表也有連上上層NTP服務器,可以作為提高時間更新的候選NTP服務器
如果有[-] 代表同步的該NTP服務器被認為是不合格的NTP Server
如果有[x] 代表同步的外網NTP服務器不可用
refid :指的是給上層NTP服務器提供時間校對的服務器。
St:上層NTP服務器的級別。
When: 上一次與上層NTP服務器進行時間校對的時間(單位:s)
Poll :本地主機與上層NTP服務器進行時間校對的周期(單位:s)
reach:已經向上層 NTP 服務器要求更新的次數
delay:網絡傳輸過程當中延遲的時間,單位為 10^(-6) 秒
offset:時間補償的結果,單位為10^(-6) 秒
jitter:Linux 系統時間與 BIOS 硬件時間的差異時間, 單位為 10^(-6) 秒。


**NTP client 與NTP Server時間同步配置**
------------------------------------------------
**一、linux 客戶端與ntp服務器時間同步部署**
可通過兩種方法實現,第一種配置周期性任務計划時間同步,第二種方法配置ntp服務 

1、配置周期性時間同步任務

[root@zabbix ~]# yum install crontabs -y

[root@zabbix ~]# chkconfig crond on

[root@zabbix ~]# service crond start # 啟動crond服務

[root@zabbix ~]# vi /etc/crontab # 配置周期性時間同步任務

Example of job definition:

.---------------- minute (0 - 59)

| .------------- hour (0 - 23)

| | .---------- day of month (1 - 31)

| | | .------- month (1 - 12) OR jan,feb,mar,apr ...

| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

| | | | |

* * * * * user-name command to be executed

0 1 * * * root /usr/sbin/ntpdate 192.168.23.100 &> /dev/null ;/sbin/hwclock -w &> /dev/null

表示每天的1:00執行/usr/sbin/ntpdate 192.168.23.100 NTP服務器,同時同步硬件時鍾,但不顯示任何信息


2、linux客戶端配置ntp服務

[root@zabbix ~]# yum install ntp ntpdate -y

[root@zabbix ~]# more /etc/sysconfig/clock
ZONE="Asia/Shanghai"

[root@zabbix ~]# date -R
Wed, 17 Oct 2018 13:53:34 +0800

[root@zabbix ~]# ntpdate 192.168.23.100
17 Oct 13:55:08 ntpdate[11772]: adjust time server 192.168.23.100 offset 0.463749 sec

[root@zabbix ~]# vi /etc/ntp.conf # 調整同步的時間服務器
server 192.168.23.100 prefer
restrict 192.168.23.100 nomodify notrap noqurey

[root@zabbix ~]# vi /etc/sysconfig/ntpd # 實現硬件時間與系統時間同步

Drop root to id 'ntp:ntp' by default.

OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

[root@zabbix ~]# service ntpd start
Starting ntpd: [ OK ]

[root@zabbix ~]# ntpq -p # 查看時間同步狀態
remote refid st t when poll reach delay offset jitter

bogon 120.25.115.19 3 u 3 64 1 0.651 35.932 0.000

[root@zabbix ~]# ntpstat
synchronised to NTP server (192.168.23.100) at stratum 4 #本NTP服務器層次為4,已向203.107.6.88 NTP同步過
time correct to within 37 ms # 時間校正到相差37ms之內
polling server every 128 s # 每128秒會向上級NTP輪詢更新一次時間


**二、Windows Server客戶端與ntp服務器時間同步部署**
1、進入日期與時間,選擇更改設置
![](https://img2020.cnblogs.com/blog/1784001/202112/1784001-20211201140726624-2107018835.png)



2、輸入ntp服務器地址,並點擊“立即更新”,確定即可。
![](https://img2020.cnblogs.com/blog/1784001/202112/1784001-20211201140748501-1866758013.png)


**注意:ntp與ntpdate**

ntpd、ntpdate的區別,下面是網上關於ntpd與ntpdate區別的相關資料。如下所示所示:
使用之前得弄清楚一個問題,ntpd與ntpdate在更新時間時有什么區別。ntpd不僅僅是時間同步服務器,它還可以做客戶端與標准時間服務器進行同步時間,而且是平滑同步,並非ntpdate立即同步,在生產環境中慎用ntpdate,也正如此兩者不可同時運行。
時鍾的躍變,對於某些程序會導致很嚴重的問題。許多應用程序依賴連續的時鍾,例如數據庫事務。ntpdate調整時間的方式就是我們所說的”躍變“:在獲得一個時間之后,ntpdate使用settimeofday(2)設置系統時間,這有幾個非常明顯的問題:
第一,這樣做不安全。ntpdate的設置依賴於ntp服務器的安全性,***者可以利用一些軟件設計上的缺陷,拿下ntp服務器並令與其同步的服務器執行某些消耗性的任務。由於ntpdate采用的方式是跳變,跟隨它的服務器無法知道是否發生了異常(時間不一樣的時候,唯一的辦法是以服務器為准)。
第二,這樣做不精確。一旦ntp服務器宕機,跟隨它的服務器也就會無法同步時間。與此不同,ntpd不僅能夠校准計算機的時間,而且能夠校准計算機的時鍾。
第三,這樣做不夠優雅。由於是跳變,而不是使時間變快或變慢,依賴時序的程序會出錯(例如,如果ntpdate發現你的時間快了,則可能會經歷兩個相同的時刻,對某些應用而言,這是致命的)。因而,唯一一個可以令時間發生跳變的點,是計算機剛剛啟動,但還沒有啟動很多服務的那個時候。其余的時候,理想的做法是使用ntpd來校准時鍾,而不是調整計算機時鍾上的時間。
NTPD 在和時間服務器的同步過程中,會把 BIOS 計時器的振盪頻率偏差——或者說 Local Clock 的自然漂移(drift)——記錄下來。這樣即使網絡有問題,本機仍然能維持一個相當精確的走時。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM