linuxf服务管理--时间同步服务

一、server端配置

安装时间同步服务器,没有特别要求,不用改配置。服务端ip:192.168.91.159

[root@www zoneinfo]# yum -y install ntp
[root@www zoneinfo]# systemctl start ntpd
#更改配置文件,加入如下两条
[root@www ntp]# vi /etc/ntp.conf
# 如果无法与上层ntp server通信以本地时间为标准时间
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10

#日志文件
logfile /var/log/ntp.log

[root@www ntp]# systemctl restart ntpd

注:服务端安装完成

做时间同步的监控需要了解

[root@localhost ~]# timedatectl
      Local time: 一 2022-02-07 15:06:35 CST    <==当地时间
  Universal time: 一 2022-02-07 07:06:35 UTC   <==伦敦时间
        RTC time: 一 2022-02-07 07:06:34       <==硬件时间
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no       <==做zabbix监控时,可能会监控这两个参数为yes,NTP可能尚未启用
NTP synchronized: no     <==做zabbix监控时,可能会监控这两个参数为yes,尚未完成过一次NTP同步
 RTC in local TZ: no
      DST active: n/a

#  NTP enabled开启
[root@localhost ~]# timedatectl set-ntp true

# NTP synchronized: no 做同步以后会自己变为YES
[root@localhost ~]# timedatectl
      Local time: 一 2022-02-07 15:17:06 CST
  Universal time: 一 2022-02-07 07:17:06 UTC
        RTC time: 一 2022-02-07 07:17:05
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

查看是否和上层的时间同步服务器进行自己的时间同步

[root@www zoneinfo]# ntpstat   <==是否和上层的时间同步服务器进行自己的时间同步
synchronised to NTP server (185.209.85.222) at stratum 3
   time correct to within 209 ms    <==矫正 109 * 10^(-3)
   polling server every 64 s        <==每64秒校准一次

ntpq -p 可以列出目前我们的 NTP 与相关的上层 NTP 的状态

[root@www zoneinfo]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+119.28.183.184  100.122.36.4     2 u   20   64  137   48.450  -48.957  17.425
-time.cloudflare 10.4.3.52        3 u   12   64  373  177.269  -18.407  23.787
*stratum2-1.ntp. 195.91.239.8     2 u   12   64  373  133.322  -38.504  25.433
+ntp7.flashdance 192.36.143.150   2 u   15   64  107  302.819  -27.658  27.421
  • remote:亦即是 NTP 主机的 IP 或主机名,注意最左边的符号
    - 如果有『 * 』代表目前正在作用当中的上层 NTP
    - 如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者。
  • refid:参考的上一层 NTP 主机的地址
  • st:就是 stratum 阶层
  • when:几秒钟前曾经做过时间同步化更新的动作;
  • poll:下一次更新在几秒钟之后;
  • reach:已经向上层 NTP 服务器要求更新的次数
  • delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
  • offset:时间补偿的结果,单位与 10^(-3) 秒
  • jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

二、client端配置

[root@master ~]# yum -y install ntpdate
[root@master ~]# ntpdate  192.168.91.159
20 Jan 11:04:41 ntpdate[1323]: no server suitable for synchronization found <==这是因为服务端还没有上层同步完成
[root@master ~]# ntpdate  192.168.91.159
20 Jan 11:07:17 ntpdate[1324]: step time server 192.168.91.159 offset -0.560287 sec  <==同步成功

注:ntpdate写入计划任务,然后定时同步即可。也可以使用另外一种方式,就是在client安装时间同步服务,使192.168.91.159为他的上层时间同步服务器,即在配置文件中加入:

server   192.168.91.159  prefer   <==有限使用这台时间同步服务

三、ntp服务器软件结构

软件相关

  1. ntp: 就是 NTP 服务器的主要软件啦,包括配置文件以及执行档等等。
  2. tzdata: 软件名称为『 Time Zone data 』的缩写,提供各时区对应的显示格式。

配置相关

  1. /etc/ntp.conf: 就是 NTP 服务器的主要配置文件,也是唯一的一个;
  2. /usr/share/zoneinfo/:由 tzdata 所提供,为各时区的时间格式对应档。
  3. /etc/sysconfig/clock:设定时区与是否使用 UTC 时间钟的配置文件。每次开机后 Linux 会自动的读取这个档案来设定自己系统所默认要显示的时间说!举个例子来说, 在我们台湾地区的本地时间设定中,这个档案内应该会出现一行『ZONE=“Asia/Taipei”』的字样, 这表示我们的时间配置文件案『要取用/usr/share/zoneinfo/Asia/Taipei 那个档案』的意思!
  4. /etc/localtime: 这个档案就是『本地端的时间配置文件』

案例:你要去美国了,如何更改时间

[root@www ntp]# date
2022年 01月 20日 星期四 11:43:27 CST  <==CST是时区

#1、更改/etc/sysconfig/clock里面为美国时区,现在都不用这个文件,直接做下一步就可以
[root@www ntp]# vi /etc/sysconfig/clock
ZONE="America/New_York" <==改的是这里

2、更改/etc/localtime本地时间
[root@www zoneinfo]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 35 7月  20 2021 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
[root@www zoneinfo]# rm -f /etc/localtime 
[root@www zoneinfo]# ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
[root@www zoneinfo]# date
2022年 01月 19日 星期三 21:05:51 EST

四、配置文件解释

1、利用 restrict 来管理权限控制

restrict  IP或者IP段  mask   [parameter]

parameter的参数

  • ignore: 拒绝所有类型的 NTP 联机;
  • nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
  • noquery: 客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时
  • notrap: 不提供 trap 这个远程事件登录 (remote event logging) 的功能。
  • notrust: 拒绝没有认证的客户端。
restrict default kod nomodify notrap nopeer noquery <==拒绝 IPv4 的用户
restrict -6 default kod nomodify notrap nopeer noquery <==拒绝 IPv6 的用户
restrict   192.168.91.177   <==对192.168.91.177没有任何限制。
restrict 127.0.0.1 <==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行区网来源

2、利用 server 设定上层 NTP 服务器

server [IP or hostname] [prefer]
  • perfer 表示『优先使用』的服务器
server 220.130.158.71 prefer <==以这部主机为最优先
server 59.124.196.83

# 如果无法与上层ntp server通信以本地时间为标准时间
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10

3、以 driftfile 记录时间差异

driftfile  文件绝对路径
  1. driftfile 后面接的档案需要使用完整路径文件名;
  2. 该档案不能是连结文件;
  3. 该档案需要设定成 ntpd 这个 daemon 可以写入的权限。
  4. 该档案所记录的数值单位为:百万分之一秒 (ppm)。

4、keys [key_file]

除了以 restrict 来限制客户端的联机之外,我们也可以透过密钥系统来给客户端
认证,如此一来可以让主机端更放心了。不过在这个章节里面我们暂不讨论这个部分,
有兴趣的朋友可以参考 ntp-keygen 这个指令的相关说明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值