【Linux部署】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
1.什么是NTP
百度百科:網絡時間協議,英文名稱:Network Time Protocol(NTP)是用來使計算機時間同步化的一種協議,它可以使計算機對其服務器或時鐘源(如石英鐘,GPS等等)做同步化,它可以提供高精準度的時間校正(LAN上與標準間差小于1毫秒,WAN上幾十毫秒),且可介由加密確認的方式來防止惡毒的協議攻擊。NTP的目的是在無序的Internet環境中提供精確和健壯的時間服務。應用場景:
- 局域內網:由于不能訪問互聯網時間,需要建立ntp服務器用于時間的同步。
- 系統集群:對時間敏感的計算處理系統內不同設備之間控制、計算、處理、應用等數據或操作都具有時序性,需要同步的標準時間用于記錄各種事件發生的時序,這對后續的數據分析
注意:在CentOS-8中默認不再支持ntp軟件包,時間同步將由chrony來實現,以下內容系統環境為CentOS Linux release 7.9.2009 (Core)。
2.NTP時間服務器端搭建
2.1 安裝
CentOS Linux release 7.9.2009 (Core)自帶ntp軟件包的,首先要確認是否已安裝ntp和ntpdate服務:
# 查看是否安裝 [root@aliyun ~]# yum list | grep ntp ntp.x86_64 4.2.6p5-29.el7.centos.2 ntpdate.x86_64 4.2.6p5-29.el7.centos.2 # 未安裝則使用命令進行安裝 yum install ntp # 啟動服務 systemctl start ntpd # 開機自啟動 systemctl enable ntpd # 查看ntp服務器狀態 [root@aliyun ~]# ntpstat synchronised to NTP server (100.100.61.88) at stratum 2time correct to within 14 mspolling server every 1024 s2.2 服務器端配置
# 修改ntp配置文件 vim /etc/ntp.confntp.conf修改前(已去掉注釋信息):
driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1server 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 iburstincludefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitorntp.conf修改后:
driftfile /var/lib/ntp/drift# 1.default表示對所有的計算機進行控制 restrict default nomodify notrap nopeer noquery # 局域網可放行可以指定網段或列出單獨IP # restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify # restrict xxx.xxx.xxx.x nomodify # 1.end restrict 127.0.0.1 restrict ::1# 2.注釋掉公網NTP服務器 # 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 # 2.end# 3.開啟自身同步 server 127.127.1.0 Fudge 127.127.1.0 stratum 8 # 3.end includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor2.3 服務器端測試
# 重啟服務 systemctl restart ntpd # 測試 [root@aliyun ~]# ntpstat synchronised to local net (127.127.1.0) at stratum 6time correct to within 949 mspolling server every 64 s3.Linux客戶端配置
3.1 安裝
安裝過程跟服務器端一致,這里不再贅述。
3.2 客戶端配置
ntp.conf修改前不再贅述,修改后:
driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1# 1.注釋掉公網NTP服務器 # 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 # 1.end# 2.開啟服務器同步 server aliyun Fudge aliyun stratum 8 # 2.end includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor3.3 客戶端測試
# 查看NTP服務是否開啟 [root@tcloud ~]# timedatectlLocal time: Fri 2022-03-25 15:29:25 CSTUniversal time: Fri 2022-03-25 07:29:25 UTCRTC time: Fri 2022-03-25 07:29:24Time zone: Asia/Shanghai (CST, +0800)NTP enabled: no NTP synchronized: noRTC in local TZ: noDST active: n/a # 開啟NTP服務 [root@tcloud ~]# timedatectl set-ntp yes [root@tcloud ~]# timedatectlLocal time: Fri 2022-03-25 15:52:30 CSTUniversal time: Fri 2022-03-25 07:52:30 UTCRTC time: Fri 2022-03-25 07:52:29Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes NTP synchronized: yesRTC in local TZ: noDST active: n/a # 重啟服務 systemctl restart ntpd # 測試 [root@tcloud ~]# ntpstat unsynchronisedtime server re-startingpolling server every 8 s # 配置后一般要等待幾分鐘才能與/etc/ntp.conf中的服務器端完成同步 [root@tcloud ~]# ntpstat synchronised to NTP server (101.201.69.99) at stratum 7time correct to within 968 mspolling server every 64 s3.4 另一種方式
使用ntpdate命令結合定時任務實現時間同步。此時要停止ntpd服務器,測試ntpdate aliyun報錯,查看報錯信息:
[root@tcloud ~]# ntpdate -d aliyun 28 Mar 15:33:22 ntpdate[17145]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1) Looking for host aliyun and service ntp host found : aliyun transmit(101.201.69.99) 101.201.69.99: Server dropped: no data server 101.201.69.99, port 123 28 Mar 15:33:30 ntpdate[17145]: no server suitable for synchronization found解決:關閉防火墻,配置aliyun服務器的安全組放開端口123。
# 端口開放后 [root@tcloud ~]# ntpdate aliyun 28 Mar 16:06:57 ntpdate[2330]: adjust time server 101.201.69.99 offset 0.000112 sec # 添加定時任務 vim /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root# /etc/crontab 內容如下 # For details see man 4 crontabs# 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 # 每分鐘同步一次 */1 * * * * root ntpdate aliyun4.Windows客戶端配置
4.1 配置并啟用客戶端
Win鍵+r輸入gpedit.msc打開本地組策略編輯器,找到配置 Windows NTP 客戶端(計算間配置-管理模板-系統-Windows 時間服務-時間提供程序)進行客戶端配置:
配置頁面:
啟用客戶端配置:
4.2 使用客戶端配置
5.ntp服務其他參數配置
5.1 查看ntp服務器有無和上層ntp連通
[root@aliyun ~]# ntpstat synchronised to NTP server (139.199.214.202) at stratum 3time correct to within 1010 mspolling server every 64 s5.2 查看ntp服務器與上層ntp的狀態
[root@aliyun ~]# ntpq -premote refid st t when poll reach delay offset jitter ============================================================================== +skitty.itu.ch 84.16.67.12 2 u 49 64 17 174.891 20.582 5.712 +139.199.214.202 100.122.36.196 2 u 47 64 37 46.355 43.764 9.636 -time.cloudflare 10.28.12.207 3 u 49 64 37 216.628 31.101 12.642 *tick.ntp.infoma .GPS. 1 u 49 64 37 156.665 12.054 11.269# 或者使用以下命令查看實時狀態 [root@aliyun ~]# watch "ntpq -p"- remote:列出源的 IP 地址或主機名。第一列中的字符指示源的質量。星號 ( * ) 表示該源是當前引用。
- refid:參考上一層ntp主機地址。
- st:stratum階層。
- when:多少秒前曾經同步過時間。指出從輪詢源開始已過去的時間(秒)。
- poll:下次更新在多少秒后。指出輪詢間隔時間。該值會根據本地時鐘的精度相應增加。
- reach:已經向上層ntp服務器要求更新的次數。是一個八進制數字,指出源的可存取性。值 377 表示源已應答了前八個連續輪詢。
- delay:網絡延遲。
- offset:時間補償。是源時鐘與本地時鐘的時間差(毫秒)。
- jitter:系統時間與bios時間差。
5.3 從其他博客收集的信息
5.3.1 /etc/ntp.conf 配置內容
driftfile /var/lib/ntp/drift# 1. 權限設置 包括放行上層服務器以及開放局域網用戶來源 restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1 # 放行局域網用戶來源 或者列出單獨IP restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify # 2. 設定主機來源 請先將原本的 [0|1|2|3].centos.pool.ntp.org 注掉 #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 # 以這部主機為最優先的server server xxx.xxx.xxx.xx prefer # 3. 默認的一個內部時鐘數據 用在沒有外部NTP服務器時 使用它為局域網用戶提供服務 server 127.127.1.0 Fudge 127.127.1.0 stratum 8includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor5.3.2 restrict選項格式
restrict [ 客戶端IP ] mask [ IP掩碼 ] [參數]
客戶端IP和IP掩碼指定了對網絡中哪些范圍的計算機進行控制,如果使用default關鍵字,則表示對所有的計算機進行控制,參數指定了具體的限制內容,常見的參數如下:
- ignore:拒絕連接到NTP服務器。
- nomodiy: 客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。
- noquery: 不提供客戶端的時間查詢。
- notrap: 不提供trap遠程登錄功能,trap服務是一種遠程時間日志服務。
- notrust: 客戶端除非通過認證,否則該客戶端來源將被視為不信任子網 。
- nopeer: 提供時間服務,但不作為對等體。
- kod: 向不安全的訪問者發送Kiss-Of-Death報文。
5.3.3 server選項格式
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上層NTP服務器的IP地址或域名,隨后所跟的參數解釋如下所示:
- key: 表示所有發往服務器的報文包含有秘鑰加密的認證信息,n是32位的整數,表示秘鑰號。
- version: 表示發往上層服務器的報文使用的版本號,n默認是3,可以是1或者2。
- prefer: 如果有多個server選項,具有該參數的服務器有限使用。
- mode: 指定數據報文mode字段的值。
- minpoll: 指定與查詢該服務器的最小時間間隔為2的n次方秒,n默認為6,范圍為4-14。
- maxpoll: 指定與查詢該服務器的最大時間間隔為2的n次方秒,n默認為10,范圍為4-14。
- iburst: 當初始同步請求時,采用突發方式接連發送8個報文,時間間隔為2秒。
5.3.4 層次(stratum)
stratum根據上層server的層次而設定(+1)。對于提供network time service provider的主機來說,stratum的設定要盡可能準確。而作為局域網的time service provider,通常將stratum設置為10。
0層的服務器采用的是原子鐘、GPS鐘等物理設備,stratum 1與stratum 0 是直接相連的,往后的stratum與上一層stratum通過網絡相連,同一層的server也可以交互。ntpd對下層client來說是service server,對于上層server來說它是client。ntpd根據配置文件的參數決定是要為其他服務器提供時鐘服務或者是從其他服務器同步時鐘。所有的配置都在/etc/ntp.conf文件中。
5.3.5 同步硬件時鐘
ntp服務,默認只會同步系統時間。如果想要讓ntp同時同步硬件時間,可以設置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加【SYNC_HWCLOCK=yes】這樣,就可以讓硬件時間與系統時間一起同步。
允許BIOS與系統時間同步,也可以通過hwclock -w 命令。
5.3.6 ntpd、ntpdate的區別
ntpd不僅僅是時間同步服務器,它還可以做客戶端與標準時間服務器進行同步時間,而且是平滑同步,并非ntpdate立即同步,在生產環境中慎用ntpdate,兩者不可同時運行。時鐘的躍變,對于某些程序會導致很嚴重的問題。
許多應用程序依賴連續的時鐘,畢竟這是一項常見的假定,即取得的時間是線性的,一些操作,例如數據庫事務,通常會地依賴這樣的事實:時間不會往回跳躍。不幸的是,ntpdate調整時間的方式就是所謂的“躍變”:在獲得一個時間之后,ntpdate使用settimeofday(2)設置系統時間,這有幾個非常明顯的問題。
-
不安全:ntpdate的設置依賴于ntp服務器的安全性,攻擊者可以利用一些軟件設計上的缺陷,拿下ntp服務器并令與其同步的服務器執行某些消耗性的任務。由于ntpdate采用的方式是跳變,跟隨它的服務器無法知道是否發生了異常(時間不一樣的時候,唯一的辦法是以服務器為準)。
-
不精確:一旦ntp服務器宕機,跟隨它的服務器也就會無法同步時間。與此不同,ntpd不僅能夠校準計算機的時間,而且能夠校準計算機的時鐘。
-
不優雅:由于是跳變,而不是使時間變快或變慢,依賴時序的程序會出錯(例如,如果ntpdate發現你的時間快了,則可能會經歷兩個相同的時刻,對某些應用而言,這是致命的)。
因而,唯一一個可以令時間發生跳變的點,是計算機剛剛啟動,但還沒有啟動很多服務的那個時候。其余的時候,理想的做法是使用ntpd來校準時鐘,而不是調整計算機時鐘上的時間。
NTPD在和時間服務器的同步過程中,會把BIOS計時器的振蕩頻率偏差——或者說Local Clock的自然漂移(drift)——記錄下來。這樣即使網絡有問題,本機仍然能維持一個相當精確的走時。
5.3.7 常用NTP服務器地址及IP
210.72.145.44 國家授時中心服務器IP地址 133.100.11.8 日本 福岡大學 time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington nist1.symmetricom.com 69.25.96.13 Symmetricom, San Jose, California nist1-dc.glassey.com 216.200.93.8 Abovenet, Virginia nist1-ny.glassey.com 208.184.49.9 Abovenet, New York City nist1-sj.glassey.com 207.126.98.204 Abovenet, San Jose, California nist1.aol-ca.truetime.com 207.200.81.113 TrueTime, AOL facility, Sunnyvale, California nist1.aol-va.truetime.com 64.236.96.53 TrueTime, AOL facility, Virginia ntp.sjtu.edu.cn 202.120.2.101 上海交通大學網絡中心NTP服務器地址 s1a.time.edu.cn 北京郵電大學 s1c.time.edu.cn 北京大學 s2m.time.edu.cn 北京大學 s1d.time.edu.cn 東南大學 s1e.time.edu.cn 清華大學 s2a.time.edu.cn 清華大學 s2b.time.edu.cn 清華大學 s1b.time.edu.cn 清華大學 s2c.time.edu.cn 北京郵電大學 s2d.time.edu.cn 西南地區網絡中心 s2e.time.edu.cn 西北地區網絡中心 s2f.time.edu.cn 東北地區網絡中心 s2g.time.edu.cn 華東南地區網絡中心 s2h.time.edu.cn 四川大學網絡管理中心 s2j.time.edu.cn 大連理工大學網絡中心 s2k.time.edu.cn CERNET桂林主節點5.3.8 查看網關方法
[root@aliyun ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.20.111.253 0.0.0.0 UG 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 172.20.96.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0[root@aliyun ~]# ip route show default via 172.20.111.253 dev eth0 169.254.0.0/16 dev eth0 scope link metric 1002 172.20.96.0/20 dev eth0 proto kernel scope link src 172.20.111.49[root@aliyun ~]# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default gateway 0.0.0.0 UG 0 0 0 eth0 link-local 0.0.0.0 255.255.0.0 U 0 0 0 eth0 172.20.96.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0總結
以上是生活随笔為你收集整理的【Linux部署】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python【算法中心 03】Docke
- 下一篇: 取消挂载点可以节省磁盘么_Linux下N