ntp時間同步服務
ntp是網絡時間協議,它是用來同步網絡中各個計算機的時間的協議
時間是一個很重要的單位概念,很多服務部署,因為時間的不同步,都會導致出錯,增加排錯難度。如:
- 定時任務的執行
- 應用數據的同步
- 必須保證服務器之間的時間一致,非常重要
timedatectl
- 修改Linux的時間、日期
- date 改時間日期
- hwclock 改硬件時間
status Show current time settings 查看當前狀態
set-time TIME Set system time 設置當前的時間
set-timezone ZONE Set system time zone 設置當前的時區
list-timezones Show known time zones 查看系統支持哪些時區
查看當前時間
[root@zz ~]# timedatectl
Local time: Mon 2022-03-21 16:43:38 CST
Universal time: Mon 2022-03-21 08:43:38 UTC
RTC time: Mon 2022-03-21 08:43:38
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@zz ~]# timedatectl status #查看當前狀態
Local time: Mon 2022-03-21 16:46:12 CST
Universal time: Mon 2022-03-21 08:46:12 UTC
RTC time: Mon 2022-03-21 08:46:11
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
用法
查看時間
[root@zz ~]# timedatectl set-time '2000-10-28 19:00'
查看系統支持多少時區
[root@zz ~]# timedatectl list-timezones|wc -l
425
找出某個時區,並設置其為當前時區
[root@zz ~]# timedatectl list-timezones|grep -i 'shanghai'
Asia/Shanghai
[root@zz ~]# timedatectl set-timezone Asia/Shanghai
[root@zz ~]#
[root@zz ~]# timedatectl
Local time: Mon 2022-03-21 17:03:08 CST
Universal time: Mon 2022-03-21 09:03:08 UTC
RTC time: Mon 2022-03-21 09:03:08
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
ntp時間同步
- ntpdate 強制性更新整個系統的時間,不友好的強制同步
- 搭建ntp服務,自動的,友好的更新校正系統時間
ntpdate
找到時間服務器地址,強制更新
[root@zz ~]# ntpdate -u ntp.aliyun.com
21 Mar 17:12:06 ntpdate[7398]: adjust time server 203.107.6.88 offset -0.000448 sec
搭建ntpd服務
1.安裝ntp軟件
[root@zz ~]# ntpdate -u ntp.aliyun.com
2.查看軟件信息
[root@zz ~]# ls /usr/lib/systemd/system/ | grep ntp
3.找到ntp軟件的配置文件
rpm -ql ntp |grep conf
4.修改ntp配置文件
vim /etc/ntp.conf
4 driftfile /var/lib/ntp/drift
5
6 #添加ntp的運行日志
7 logfile /var/log/my_ntp.log
8
9 #記錄程序運行進程號,可以用於寫腳本,讀取這個文件,就找到了程序的進程ID
10 pidfile /var/run/ntpd.pid
11
12 # Permit time synchronization with our time source, but do not
13 # permit the source to query or modify the service on this system.
14 restrict default nomodify notrap nopeer noquery
15
16 # Permit all access over the loopback interface. This could
17 # be tightened as well, but to do so would effect some of
18 # the administrative functions.
19 restrict 127.0.0.1
20 restrict ::1
21
22 # Hosts on local network are less restricted.
23 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
24
25 # Use public servers from the pool.ntp.org project.
26 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
27 #server 0.centos.pool.ntp.org iburst
28 #server 1.centos.pool.ntp.org iburst
29 #server 2.centos.pool.ntp.org iburst
30 #server 3.centos.pool.ntp.org iburst
31
32 server ntp.aliyun.com iburst prefer
33 server cn.pool.ntp.org iburst
添加6、7、9、10、32、33行
5.修改機器的時間為錯誤時間
[root@zz ~]# timedatectl set-time '2000-11-23 19:00'
[root@zz ~]# timedatectl status
Local time: Thu 2000-11-23 19:00:02 CST
Universal time: Thu 2000-11-23 11:00:02 UTC
RTC time: Thu 2000-11-23 11:00:02
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
6.啟動ntpd服務,等待時間是否同步
[root@zz ~]# systemctl start ntpd
[root@zz ~]#
[root@zz ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-03-21 17:40:34 CST; 21 years 3 months left
Main PID: 7765 (ntpd)
CGroup: /system.slice/ntpd.service
└─7765 /usr/sbin/ntpd -u ntp:ntp -g
Mar 21 17:40:34 zz systemd[1]: Starting Network Time Service...
Mar 21 17:40:34 zz systemd[1]: Started Network Time Service.
Mar 21 17:40:34 zz ntpd[7765]: proto: precision = 0.040 usec
Mar 21 17:40:34 zz ntpd[7765]: 0.0.0.0 c01d 0d kern kernel time sync enabled
7.查看ntp是否與上游服務器同步
[root@zz ~]# ntpstat
synchronised to unspecified (203.107.6.88) at stratum 3
time correct to within 957 ms
polling server every 64 s
8.查看時間同步狀態
[root@zz ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
203.107.6.88 10.137.55.181 2 u 12 64 57 14.129 -0.910 4405024
ntp1.flashdance 192.36.143.152 2 u 5 64 43 272.912 6728785 5827299
* 這樣一個可當做上游時間服務器的機器就配完了