• linux服务-配置ntp时间服务


    参考博文:
    如何部署NTP服务器
    NTP时间服务器安装部署文档
    NTP时间服务器部署以及时间同步设置
    Centos7搭建NTP服务器及客户端同步时间

    参考文档:
    GB_T 20520-2006 信息安全技术 公钥基础设施 时间戳规范;
    GM_T0033-2014 时间戳接口规范;
    GB_T 20985.2-2020 信息安全技术 信息安全时间管理 第2部分:时间响应规划和准备指南;

    参考百度百科:时间戳服务器

    关于通信网中基准时间的架构(来自一建通信教材):

    同步网是现代通信网运行的支持系统之一,处于通信网的最底层,负责实现网络节点
    设备之间和节点设备与传输设备之间信号的时钟同步、帧同步以及全网的网同步,保证地
    理位置分散的物理设备之间的数字信号的正确接收和发送。
    我国数字同步网采用由单个基准时钟控制的分区式主从同步网结构,分为四个等级
    1.第一级是基准时钟(PRC),由3个铯原子钟组成,它是我国数字网中精度最高的
    时钟,是其他所有时钟的基准。

    2.第二级是长途交换中心时钟,设置在长途交换中心,构成高精度区域基准时钟
    (LPR),该时钟分为A类和B类。设置于一级(C1)和二级(C2)长途交换中心的时钟
    属于A类时钟,它通过同步链路直接与基准时钟同步。设置于三级(C3)和四级(C4)
    长途交换中心的时钟属于B类时钟,它通过同步链路受A类时钟控制,间接地与基准时钟
    同步。

    3.第三级是有保持功能的高稳定度晶体时钟,其频率偏移率可低于二级时钟。通过
    同步锌路与二级时钟或同等级时钟同步。设置在汇接局(Tm)和端局(CS)。

    4.第四级是一般晶体时钟,通过同步链路与第三级时钟同步,设置于远端模块、数
    字终端设备和数字用户交换设备

    操作系统环境信息:
    [root@centos7 ~]# cat /etc/redhat-release
    CentOS Linux release 7.9.2009 (Core)

    通过data命令查看当前系统时间,时区;
    [root@centos7 ~]# date
    2023年 10月 19日 星期四 16:39:29 CST

    如果时区不对,修改方式为:

    查看本机是否安装ntp模块,如果没有则进行安装
    在这里插入图片描述
    如果没有安装,则通过yum install ntp方式安装;

    配置ntp服务,ntp服务配置文件路径/etc/ntp.conf
    添加ntp服务地址 server ntp.aliyun.com
    配置文件强调:
    在这里插入图片描述
    来自讯飞星火ai的回复:

    NTP服务和上游时间服务器同步时间周期的设置通常在/etc/ntp.conf文件中进行。你可以根据需要调整以下参数:
    
    restrict default nomodify notrap:这个选项表示默认情况下不允许修改、报告或通知任何主机。你可以将其更改为允许修改,例如:restrict default nomodify notrap allow-query。
    
    server time.pool.aliyun.com iburst:这个选项表示使用阿里云的时间服务器进行同步。你可以将其更改为其他上游时间服务器,例如:server ntp.example.com iburst。
    
    fudge 127.127.1.0 stratum 10:这个选项表示当外部时间服务器不可用时,使用本地时间作为时间服务。你可以根据需要调整stratum值,例如:fudge 127.127.1.0 stratum 5。
    
    driftfile /var/lib/ntp/ntp.drift:这个选项表示用于存储NTP客户端与服务器之间的时间偏差的文件。你可以根据需要调整文件路径,例如:driftfile /data/ntp/ntp.drift。
    
    makestep 1 -1:这个选项表示在每次同步之间等待的时间(以秒为单位)。你可以根据需要调整等待时间,例如:makestep 2 -2。
    
    rtcsync:这个选项表示是否启用实时时钟同步。你可以根据需要启用或禁用它,例如:rtcsync yes。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    重启服务,使配置生效,设置服务为开机自启动
    systemctl restart ntpd
    在这里插入图片描述
    [root@centos7 ~]# systemctl is-enabled ntpd #检查开机自启动是否配置
    enabled
    [root@centos7 ~]# systemctl enable ntpd #配置开机自启动

    功能验证:

    [root@centos7 ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *203.107.6.88    10.137.55.181    2 u  706 1024  377   28.118   11.494  25.554
    [root@centos7 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    该remote列告诉您NTP守护进程正在使用的服务器的主机名,refid列告诉您的服务器使用的源代码。因此,对于Stratum 1服务器,refid字段应显示GPS,PPS,ACTS或PTB,而Stratum 2和更其他辅助服务器将显示上游服务器的IP地址。在ST列显示层级,和delay,offset和jitter告诉你时间源的质量。这三个字段的值越低越好。

    手工同步时间进行验证

    [root@centos7 ~]# ntpdate  -q 203.107.6.88
    server 203.107.6.88, stratum 2, offset 0.006598, delay 0.05267
    19 Oct 17:04:02 ntpdate[7273]: adjust time server 203.107.6.88 offset 0.006598 sec
    [root@centos7 ~]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    一个彩蛋:manage.ntppool.org 注册成为NTP池项目的一部分;(专门的硬件授时同步模块)

    另一个彩蛋:timedatectl,来自B站-2023年网络建设与运维国赛-Linux部分-时间服务器

    通过timedatectl status查看本机时间配置

    [root@centos7 ~]# timedatectl status
          Local time: 四 2023-10-19 17:15:26 CST
      Universal time: 四 2023-10-19 09:15:26 UTC
            RTC time: 四 2023-10-19 09:15:26
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: yes
    NTP synchronized: yes
     RTC in local TZ: no
          DST active: n/a
    [root@centos7 ~]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    常见timedatectl使用案例
    如果需要手工调整时间,需要先关闭ntp同步功能;

    [root@centos7 ~]# timedatectl set-time "2023-10-11 18:00:00" #本地时间手工调整
    Failed to set time: Automatic time synchronization is enabled
    [root@centos7 ~]# timedatectl set-ntp false #关闭NTP服务,0表示false,1表示true
    [root@centos7 ~]# timedatectl status
          Local time: 四 2023-10-19 17:24:21 CST
      Universal time: 四 2023-10-19 09:24:21 UTC
            RTC time: 四 2023-10-19 09:24:21
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: no
    NTP synchronized: yes
     RTC in local TZ: no
          DST active: n/a
    [root@centos7 ~]# timedatectl set-time "2023-10-11 18:00:00" #本地时间手工调整
    [root@centos7 ~]# date
    20231011日 星期三 18:00:05 CST
    [root@centos7 ~]# timedatectl 
          Local time: 三 2023-10-11 18:00:20 CST
      Universal time: 三 2023-10-11 10:00:20 UTC
            RTC time: 三 2023-10-11 10:00:20
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: no
    NTP synchronized: no
     RTC in local TZ: no
          DST active: n/a
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    手工进行同步
    [root@centos7 ~]# /usr/sbin/ntpdate -u ntp.aliyun.com
    19 Oct 17:56:47 ntpdate[7630]: adjust time server 203.107.6.88 offset 0.003531 sec

    Q:Linux如何修改系统时间

    A:可以通过date命令修改系统时间,然后需要用hwclock命令将系统时间同步到硬件时间。命令如下:

    [root@localhost ~]# date -s “2015-7-30 16:59:00”(修改时钟为2015年7月30日16点59分)
    Thu Jul 30 16:59:00 CST 2015

    [root@localhost ~]# hwclock -w --systohc(同步到硬件时钟)

    Q:Linux如何修改时区为东8区

    A:按以下步骤执行
    1.使用 vi 命令修改/etc/sysconfig/clock

    [root@localhost Asia]# vi /etc/sysconfig/clock

    2.修改内容:ZONE=“Asia/Shanghai”

    3.保存退出

    4.拷贝shanghai时区到/etc/localtime
    #cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    cp: overwrite `/etc/localtime’? y

  • 相关阅读:
    sql生成两个时间区间的所有日期
    计算机视觉小项目—基于RGB颜色特征的火焰识别
    vue实现水平switch多个切换按钮
    七天进阶elasticsearch[one]
    【论文阅读】Anchor-Free Person Search
    Qt6.3学习笔记 QFrame::HLine与QFrame::VLine 改变线条颜色
    365天搞定八股文——Day 005 MQ中的重要概念
    git(代码冲突解决(客户端操作和命令行操作))
    【Python3】基础 - 基本数据类型
    Docker入门学习笔记
  • 原文地址:https://blog.csdn.net/qinaide56/article/details/133929925