Linux配置NTP時間同步
一、ntp和ntpdate區別
兩個服務都是centos自帶的(centos7中不自帶ntp)
ntp守護進程為ntpd,配置文件是/etc/ntp.conf
ntpdate用於客戶端的時間矯正,非NTP服務器可以不啟動NTP。
二、搭建
- NTP(network time protocol 網絡時間協議)用於同步計算機和網絡設備的內部的時間一種協議
- NTP Server服務器IP:192.168.1.111
- NTP Client客戶端IP:192.168.1.112
2.1、搭建NTP服務器
yum install -y ntp
2.1.1、修改配置文件
[root@master ~]# vim /etc/ntp.conf
把配置文件下面四行注釋掉:
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 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
2.2.2、啟動ntp服務,並開機自啟動
啟動ntp服務,並開機自啟動
[root@master ~]# systemctl start ntpd
[root@master ~]# systemctl enable ntpd
2.2.3、查詢ntp是否同步
【命令】ntpq -p
- remote:本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先
- refid:參考上一層ntp主機地址
- st:stratum階層
- when:多少秒前曾經同步過時間
- poll:下次更新在多少秒后
- reach:已經向上層ntp服務器要求更新的次數
- delay:網絡延遲
- offset:時間補償
- jitter:系統時間與bios時間差
[root@master ~]# date
Tue Nov 17 21:24:24 CST 2020
[root@master ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
time.cloudflare .STEP. 16 u - 64 0 0.000 0.000 0.000
ntp7.flashdance .STEP. 16 u - 64 0 0.000 0.000 0.000
202.118.1.130 .STEP. 16 u - 64 0 0.000 0.000 0.000
*ntp5.flashdance 194.58.202.20 2 u 11 64 1 372.035 -17.860 28.092
[root@master ~]# ntpstat
synchronised to NTP server (193.182.111.14) at stratum 3
time correct to within 244 ms
polling server every 64 s
[root@master ~]# timedatectl
Local time: Tue 2020-11-17 22:37:02 CST
Universal time: Tue 2020-11-17 14:37:02 UTC
RTC time: Tue 2020-11-17 14:37:03
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@master ~]# ntpdate -u 193.182.111.14
17 Nov 22:38:10 ntpdate[7787]: adjust time server 193.182.111.14 offset 0.039557 sec
2.3、NTP客戶端配置
yum install -y ntp
2.3.1、修改配置文件
[root@slave1 ~]# vim /etc/ntp.conf
#配置允許NTP Server時間服務器主動修改本機的時間
restrict 192.168.1.111 nomodify notrap noquery
#配置時間服務器為本地搭建的NTP Server服務器
server 192.168.1.111 iburst
#配置允許上游時間服務器主動修改本機的時間
restrict 192.168.1.111 nomodify notrap noquery
#注釋掉其他時間服務器
#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.3.2、啟動ntp服務,並開機自啟動
啟動ntp服務,並開機自啟動
systemctl start ntpd
systemctl enable ntpd
2.3.3、查看同步的主服務器,顯示如下表示成功
# 啟動后查看
[root@slave1 ~]# ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.1.111 202.12.97.45 2 u 20 64 0 0.000 0.000 0.000