Linux NTP工具的基本使用

2023-02-15,,

NTP 时间同步

NTP(Network Time Protocol)协议,网络时间协议。利用ntp协议可以实现网络中的计算机时间同步。
实现NTP协议的工具

ntpdate:只能同步一次时间

ntp:持久同步时间

chrony:持久同步时间,精度比上面的ntp更好,速度更快。

使用ntpdate同步本机时间的方法:

格式:ntpdate host

范例:使用ntpdate同步时间

root@ubuntu1804:~# apt install ntpdate

root@ubuntu1804:~# date
Tue Dec 13 15:12:07 CST 2022 root@ubuntu1804:~# date -d '-1 day'
Mon Dec 12 15:12:22 CST 2022 root@ubuntu1804:~# ntpdate ntp.aliyun.com
13 Dec 15:12:36 ntpdate[15040]: adjust time server 203.107.6.88 offset 0.002588 sec root@ubuntu1804:~# date
Tue Dec 13 15:12:40 CST 2022 #同步后的时间

范例:使用ntp实现本机时间的同步:

#安装ntp工具
root@ubuntu1804:~# sudo apt install apt -y #将本机时间设置为两天前,便于后面测试
root@ubuntu1804:~# date -d '-2 day'
Sun Dec 11 15:15:51 CST 2022 #修改配置文件
root@ubuntu1804:~# vim /etc/ntp.conf
pool ntp.aliyun.com #指定时间服务器是阿里云的时间服务器 #重启服务
root@ubuntu1804:~# systemctl restart ntp.service #查看同步情况
root@ubuntu1804:~# date
Tue Dec 13 15:16:56 CST 2022 #查看同步的过程
root@ubuntu1804:~# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp.aliyun.com .POOL. 16 p - 64 0 0.000 0.000 0.000
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
203.107.6.88 10.137.55.181 2 u 49 64 1 49.907 11.370 0.000
91.189.91.157 ( 194.58.200.20 2 u 47 64 1 249.332 6.425 0.000
185.125.190.57 167.28.20.25 2 u 58 64 1 290.552 21.957 0.000
185.125.190.58 167.28.20.25 2 u 60 64 1 284.391 27.227 0.000
pugot.canonical 17.253.34.253 2 u 60 64 1 275.512 31.489 0.000 #参数说明
remote: 表示NTP主机的IP或者是主机名,最左边的
refid: 表示参考的上一层NTP主机的地址;
st: 表示的含义是startnum,一般范围是从0-->15;
when: 表示几秒前做过时间同步化更新操作;
poll: 表示下一次更新在几秒钟之后;
reach: 表示已经向上一层服务器要求更新的次数;
delay: 表示数据在传输过程中延迟的时间;
offset: 是源时钟与本地时钟的时间差(毫秒)。
jitter: Linux系统时间和BIOS硬件时间的差异;

范例:使用chrony工具实现本机的时间同步:

chrony会监听udp的两个端口,如果是作为客户端,就监听udp的323端口,如果是服务端就监听123端口。

# 安装服务
root@ubuntu1804:~# apt install chrony #修改配置文件
root@ubuntu1804:~# vim /etc/chrony/chrony.conf
pool ntp.aliyun.com #将时间设置错,便于测试
root@ubuntu1804:~# date -s '20211010'
Sun Oct 10 00:00:00 CST 2021 #启动服务
root@ubuntu1804:~# systemctl start chrony #查看有多少个ntp源在线:
root@ubuntu1804:~# chronyc activity
200 OK
5 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address #显示本机时间和远程服务器同步的情况
root@ubuntu1804:~# chronyc sources
210 Number of sources = 5
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ prod-ntp-3.ntp1.ps5.cano> 2 6 17 16 -4318us[-2258us] +/- 136ms
^- pugot.canonical.com 2 6 27 12 -3890us[-3890us] +/- 163ms
^+ alphyn.canonical.com 2 6 17 16 +18ms[ +20ms] +/- 176ms
^* prod-ntp-5.ntp4.ps5.cano> 2 6 17 15 -6154us[-4094us] +/- 138ms
^? 203.107.6.88 2 6 1 23 +13ms[-10312h] +/- 33ms
root@ubuntu1804:~# date
Tue Dec 13 15:36:54 CST 2022 #等待几分钟,时间恢复正常
root@ubuntu1804:~# date
Tue Dec 13 15:36:54 CST 2022

使用ntp搭建私有的时间服务器:

通过将一台主机搭建为时间服务器,其它局域网内的主机可以和本机的时间进行同步。

范例:使用ntp实现时间服务器:

#只需要再ntp的配置文件中指定允许谁和本机进行同步就行。

#修改配置文件
root@ubuntu1804:~# vim /etc/ntp.conf
server 127.0.0.1 #以本机的时间为准
restrict default #表示允许所有主机与本机进行ntp同步
# restrict 10.0.0.0 mask 255.255.255.0 #允许这个网段的主机与本机进行ntp同步 #启动服务
root@ubuntu1804:~# systemctl restart ntp # 客户机测试;
root@ubuntu1804:/etc/nginx/conf.d# ntpdate 10.0.0.44
13 Dec 15:57:09 ntpdate[3364]: adjust time server 10.0.0.44 offset 0.028476 sec

使用chrony搭建时间服务器:

# chrony默认是作为客户端使用的,要想使用服务端的功能,就需要打开123这个端口
# 将chrony配置为服务端的方法:allow指令 # allow 0.0.0.0/0 表示的是任何主机都可以和本机进行时间的同步
#安装chrony
root@ubuntu1804:~# apt install chrony -y #修改改配置文件
root@ubuntu1804:~# vim /etc/chrony/chrony.conf
server 127.0.0.1 #以本地的时间为准
local stratum 10 #互联网无法连接,仍然可提供时间同步服务 # 将当前服务器设置为第十层(把自己设置为内部服务器的意思)
allow 0.0.0.0/0 #指定允许同步的网段 #重启服务
root@ubuntu1804:~# systemctl restart chronyd #修改一个本地时间
root@ubuntu1804:~# date -s '20221010'
Mon Oct 10 00:00:00 CST 2022 #客户端测试
root@ubuntu1804:/etc/nginx/conf.d# ntpdate 10.0.0.44
10 Oct 00:00:18 ntpdate[3410]: step time server 10.0.0.44 offset -5588217.989475 sec #时间同步为和服务端一样了
root@ubuntu1804:/etc/nginx/conf.d# date
Tue Dec 13 16:17:22 CST 2022

说明:

(1)ntp和chrony是客户端和服务端一体的工具,通过不同的设置配置为客户端或者服务端。

(2)server和pool指定的区别:

server   #用于指定单个NTP服务器,iburst:让同步时间的时候更快,开启多个并行任务来进行同步

pool    #用于指定NTP服务器池而不是单个NTP服务器。池名称应解析为随时间可能会变化的多个地址

(3)配置为客户端时,指定时间服务器的相关参数;

例如:server 192.168.7.49 prefer    #prefer表示如果指定了多个时间服务器的情况下优先用这个 iburst:表示加速时间同步

https://zhuanlan.zhihu.com/p/451555297

Linux NTP工具的基本使用的相关教程结束。

《Linux NTP工具的基本使用.doc》

下载本文的Word格式文档,以方便收藏与打印。