Linux的NTP深度学习

NTP服务器:

对于Linux服务器来说,需要进行时间校对。而NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。Linux虚拟机采用的时区制(UTC),我国东西相跨5个时区(东五区、东六区、东七区、东八区、东九区5个时区),所以北京时间=UTC+8。

时间更改:

//可以在这个目录下查看时区信息
[root@lxb ~]# ls /usr/share/zoneinfo/

//可以使用zdump查看其它时区
[root@lxb ~]# zdump Hongkong
Hongkong  Wed Dec 22 13:43:24 2021 HKT

//修改时区可以通过timedatectl
[root@lxb ~]# timedatectl set-timezone Asia/Shanghai 

//此时查看/etc/localtime 发现此时是链接到shanghai时区
[root@lxb ~]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 35 12月 22 13:47 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

//此时查看时间,发现为上海时间
[root@lxb ~]# date
2021年 12月 22日 星期三 13:48:20 CST

//查看当前时区信息
[root@lxb ~]# timedatectl 
               Local time: 三 2021-12-22 13:50:15 CST
           Universal time: 三 2021-12-22 05:50:15 UTC
                 RTC time: 三 2021-12-22 05:50:14
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

//列出所有的时区信息
[root@lxb ~]# timedatectl list-timezones 

ntp服务器的配置信息:

//下载ntp服务器(centos7镜像):
[root@localhost /]# yum install ntp -y

//而centos8镜像下:
[root@localhost /]# yum install chrony -y

//centos7镜像下,主配置文件在/etc/ntp.conf
[root@localhost /]# vim /etc/ntp.conf 

driftfile /var/lib/ntp/drift   
    用driftfile记录时间driftfile后面指定的文件需要使用完整路径文件名
restrict default nomodify notrap nopeer noquery
    设置restrict权限
restrict [ip地址] mask [255.255.255.0]  parameter
parameter的参数如下:
ignore   --- 拒绝所有类型的连接
nomodify --- 客户端不能用ntpc与ntpq这来修改服务器的时间参数,但可通过主机来进行网络校时
noquery  --- 客户端不能够使用ntpc、ntpq等命令来查询时间服务器,等于不提供NTP的网络校时
notrap   --- 不提供trap这个远程事件登录的功能
notrust  --- 拒绝没有认证的客户端
如果在parameter的地方没有加上任何参数的话,这表示该ip或网段不受任何限制
 
server 0.rhel.pool.ntp.org iburst   设置联网的ntp服务器
    server的NTP服务器设置
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秒
 
//查看时间ntp服务器结果
[root@lxb /]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 120.25.115.20                 2   6     5     1    +13ms[+2931us] +/-   21ms
^* 203.107.6.88                  2   6     7     1  +1413us[-8277us] +/-   35ms

*            代表目前正在使用的上层NTP
+            连接成功,可提供时间更新的候补服务器
-            该NTP服务器被认为不合格
Name/IP      上级NTP服务器的地址
st           上级NTP服务器的层级,0-15
t :The type of the peer (local, unicast, multicast or broadcast) when the last packet was received
l    local (such as a GPS clock) 
u   unicast (this is the common type) (单播,即NTP client向NTP server发送NTP请求,NTP server回复的模式)
m  multicast (多播,可跨子网。)
b   broadcast (广播,不可跨子网。NTP server定时向广播地址发送NTP包,NTP client通过广播地址获取NTP包,同步本地时钟)
 
Steatum         几秒钟前曾做过时间同步化
poll            下一次更新在几秒钟之后
reach           八进制数,已经向上层服务器要求更新的次数
lastRx            网络传输过程中的延迟时间
last          本地和服务器之间的时间差别,越接近0,说明和服务器的时间越接近
jitter          系统时间与硬件时间的差异

搭建本地的时间服务器(基于centos8):

//先修改ntp服务器的默认计时器
[root@lxb /]# vim /etc/chrony.conf 
#pool 2.centos.pool.ntp.org iburst
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool ntp3.aliyun.com iburst

//设置为阿里云的计时器
//此时重启服务器
[root@lxb /]# systemctl stop firewalld
[root@lxb /]# setenforen 0
[root@lxb /]# systemctl restart chronyd

//此时date设置一个错误的时间
[root@lxb /]# date  -s "14:00:00"
2021年 12月 22日 星期三 14:00:00 CST

//然后重启获取时间
[root@lxb /]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 120.25.115.20                 2   6    17    35   -480us[+9239us] +/-   19ms
^- 203.107.6.88                  2   6    17    35  -9510us[-9510us] +/-   31ms

//此时时间正常
[root@lxb /]# date
2021年 12月 22日 星期三 16:08:31 CST

搭建获取时间的ntp服务器(基于centos8):

//首先需要在ntp服务器上设置允许访问
[root@lxb /]# vim /etc/chrony.conf 
#pool 2.centos.pool.ntp.org iburst
#配置ntp时间地址
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool ntp3.aliyun.com iburst

# Allow NTP client access from local network.
#设置允许获取服务的ip
allow 192.168.220.0/24

# Serve time even if not synchronized to a time source.
#设置更新时间
local stratum 12

//重启ntp服务器
[root@lxb /]# systemctl restart chronyd


//配置ntp客户端,需要获取时间为服务端的ip
[root@localhost /]# vim /etc/chrony.conf 
pool 192.168.220.234 iburst

//然后重启客户端服务
[root@localhost /]# systemctl restart chronyd
//然后重新设置更新服务(更新的为服务器ip)
[root@localhost /]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.220.234               3   6   377     4   +110us[ +130us] +/-   20ms

//此时查看时间
[root@localhost /]# date
Wed Dec 22 16:44:18 CST 2021



//  !!!此时需要注意的是,如果服务器和客户端不在一个时区内,是无法同步时间的!!!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值