• 校时,搭建自己的ntp服务器并且建立client


    1,ubuntu自带的校时方法

    以前Linux时间同步基本是使用 ntpdate 和 ntpd 这两个工具实现的,但是这两个工具已经很古老了,好像不不再使用了,注意哈,这俩工具是互斥的。

    目前的Linux ( ubuntu,debian,openSUSE) 等使用 systemd-timesyncd 服务进行校时。

    1) systemd-timesyncd是个啥?

        systemd-timesyncd是断点式更新时间,也就是时间不同立即更新,这样会对某些服务产生影响,所以在生产环境尽量不要用,在桌面环境或者是系统刚开机时来进行时间同步还是很好的。timesyncd 替代了 ntpd 的客户端的部分。默认情况下 timesyncd 会定期检测并同步时间。它还会在本地存储更新的时间,以便在系统重启时做时间单步调整。如果是虚拟机环境,应该把与主机时间同步功能关闭后在启用systemd-timesyncd,否则可能会有问题,systemd-timesyncd只能作为客户端,不能作为NTP服务器,要成为NTP服务器,可以安装chrony、ntpd,或者open-ntp。推荐chrony。顺带说一句,ubuntu里的命令timedatectl设置的就是这个服务里的参数:

    #输入timedatectl,得到输出
    Local time: Tue 2020-05-05 10:38:53 CST      ##本地时间
    Universal time: Tue 2020-05-05 02:38:53 UTC    ##协调世界时
    RTC time: Tue 2020-05-05 02:38:51          ##硬件时间
    Time zone: Asia/Shanghai (CST, +0800)        ##时区,我这里为东8区
    Network time on: yes       ##NTP时间同步是否开启,yes表示是
    NTP synchronized: yes        ##如果和远程NTP服务器成功同步,显示为yes
    RTC in local TZ: no                        ##no表示硬件时钟设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间

    你可以这样查看systemd-timesyncd服务的状态(注意,开启重启关闭不要使用sudo service这个系列,这会造成问题!!!):

    sudo service systemd-timesyncd status

    你可以看到这样的数据:

    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: active (running) since Fri 2021-01-29 00:00:38 CST; 15h ago
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 761 (systemd-timesyn)
       Status: "Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com)."
        Tasks: 2 (limit: 4915)
       CGroup: /system.slice/systemd-timesyncd.service
               └─761 /lib/systemd/systemd-timesyncd
    
    Jan 29 00:00:38 center-OptiPlex-7060 systemd[1]: Starting Network Time Synchronization...
    Jan 29 00:00:38 center-OptiPlex-7060 systemd[1]: Started Network Time Synchronization.
    Jan 29 00:01:18 center-OptiPlex-7060 systemd-timesyncd[761]: Timed out waiting for reply from 91.189.94.
    Jan 29 00:01:19 center-OptiPlex-7060 systemd-timesyncd[761]: Synchronized to time server 91.189.89.198:1

    好吧,被我发现了,我的确是在00:00:01秒的时候重启了机器,大约00:00:21秒的时候重启完毕,很明显,还没重启完的时候它就自动运行了校时服务,校时服务器是 91.189.89.198:123,尝试访问这个网址但是打不开,机智的我尝试nmap这个网址的这个端口,嘿嘿嘿,被我发现了端倪哦。

    nmap 91.189.89.198 -p 123
    
    Starting Nmap 7.60 ( https://nmap.org ) at 2021-01-29 15:36 CST
    Nmap scan report for chilipepper.canonical.com (91.189.89.198)
    Host is up (0.0015s latency).
    
    PORT    STATE    SERVICE
    123/tcp filtered ntp
    
    #端口的服务里备注了,这就是个ntp的servce,所以,如果我们自己搭建ntp服务器,起码得搭建一个类似这样的哦。

    那么如何重启这个systemd-timesyncd服务呢?请用这句命令

    systemctl restart systemd-timesyncd.service

     禁用systemd-timesyncd服务

    systemctl disable systemd-timesyncd

    2),修改systemd-timesyncd的配置也是可以的

    配置文件:/etc/systemd/timesyncd.conf

    里面的内容和具体含义是什么?

    [Time]
    #NTP=
    #FallbackNTP=ntp.ubuntu.com
    #RootDistanceMaxSec=5
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048
    
    #NTP:一个空格分隔的NTP服务器列表, 可以使用主机名,也可以使用IP地址。在运行时, 此处设置的列表将与 systemd-networkd.service中已配置的NTP服务器列表合并在一起。 systemd-timesyncd 将会依次尝试列表中的每个NTP服务器, 直到同步成功为止。
    #FallbackNTP:一个空格分隔的NTP服务器列表,用作备用NTP服务器。 可以使用主机名,也可以使用IP地址。 如果所有已配置在 systemd-networkd.service中的NTP服务器以及上述 NTP= 中设置的NTP服务器都尝试失败, 那么将尝试此处设置的备用NTP服务器。
    #RootDistanceMaxSec:最大可接受的"root distance"秒数(最大误差)。 默认值为 5 秒。
    #剩下的俩参数:NTP消息的 最小/最大轮询间隔秒数。 PollIntervalMinSec= 必须不小于 16 秒。 PollIntervalMaxSec= 必须大于 PollIntervalMinSec= 。 PollIntervalMinSec= 默认为 32 秒, PollIntervalMaxSec= 默认为 2048 秒。

    本人如何配置:

    #以下配置证明是可以的,经过测试,好像不需要指明123端口就能校时
    [Time]
    NTP=114.118.7.163
    #FallbackNTP=ntp.ubuntu.com
    #RootDistanceMaxSec=5
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048
    
    
    #以下配置貌似也可
    [Time]
    NTP=ntp.ntsc.ac.cn
    #FallbackNTP=ntp.ubuntu.com
    #RootDistanceMaxSec=5
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048
    
    #ps:ntp.ntsc.ac.cn是一个国内的校时服务器哈。那么问题来了,如果电脑不设置dns,能不能解析域名呢?经过本人测试,确实有问题!

     建议,首先sudo vim /etc/systemd/timesyncd.conf,设置好你想要的校时服务以后,执行systemctl restart systemd-timesyncd.service重启这个服务,然后我们来查看状态:sudo service systemd-timesyncd status,如果你能在status这一行里看到你刚刚设置的那个ntp地址,那么基本你的设置是正确的,如果你想确保你的设置正确无误,建议改完配置以后重启电脑,然后再查看sudo service systemd-timesyncd status,成功的大约这样:

    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: active (running) since Fri 2021-01-29 16:30:35 CST; 4min 12s ago
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 802 (systemd-timesyn)
       Status: "Synchronized to time server 114.118.7.163:123 (ntp.ntsc.ac.cn)."
        Tasks: 2 (limit: 4915)
       CGroup: /system.slice/systemd-timesyncd.service
               └─802 /lib/systemd/systemd-timesyncd
    
    Jan 29 16:30:34 center-OptiPlex-7060 systemd[1]: Starting Network Time Synchronization...
    Jan 29 16:30:35 center-OptiPlex-7060 systemd[1]: Started Network Time Synchronization.
    Jan 29 16:31:07 center-OptiPlex-7060 systemd-timesyncd[802]: Synchronized to time server 114.118.7.163:123 (ntp.ntsc.ac.cn).
    
    #尤其注意一下status那一行,那一行是不是你设置的ntp服务器地址

    不成功的大约这样:

    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:systemd-timesyncd.service(8)
    #具体原因是因为我安了个apt-get了一个ntp,开机以后ntp服务把systemd-timesyncd服务给挤飞了,所以软件这个东西,最好别乱装

    不成功大约还这样:

    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: active (running) since Fri 2021-01-29 16:52:22 CST; 10s ago
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 1641 (systemd-timesyn)
       Status: "Idle."
        Tasks: 2 (limit: 4915)
       CGroup: /system.slice/systemd-timesyncd.service
               └─1641 /lib/systemd/systemd-timesyncd
    
    Jan 29 16:52:22 center-OptiPlex-7060 systemd[1]: Starting Network Time Synchronization...
    Jan 29 16:52:22 center-OptiPlex-7060 systemd[1]: Started Network Time Synchronization.
    
    #注意,status显示的是idle,闲。那我们继续查看timedatectl状态吧?
    
                          Local time: Fri 2021-01-29 16:52:47 CST
                      Universal time: Fri 2021-01-29 08:52:47 UTC
                            RTC time: Fri 2021-01-29 08:52:47
                           Time zone: Asia/Shanghai (CST, +0800)
           System clock synchronized: no
    systemd-timesyncd.service active: yes
                     RTC in local TZ: no
    
    #注意看 System clock synchronized,表示ntp没有授时

    所以,如果你没有设置dns,那么请你写授时机器的ip地址,不要写域名,不要写域名,不要写域名!!

    2,搭建ntp服务器

    环境:

    ntp局域网服务器:192.168.3.155

    ntp局域网用户:192.168.3.142

    安装

    sudo apt-get install ntp

    找几个ntp服务器地址用一下:

    我国授时中心服务器ip:210.72.145.44

    国内校时服务器ntp.ntsc.ac.cn:114.118.7.163

     ps:尝试扫描了一下上面的这些ip地址,123端口都是有的,但是上海交通和深圳阿里云的123端口都显示closed状态,其他的都是filtered,原因不知,但是我还是不用了吧。

    首先需要选一下时区:

    date -R
    #查看当前时间
    tzselect
    #系统会提示你选择你所在的洲,国,根据实际情况选择完成以后确认即可,不需要图形界面
    
    方法1,直接复制:
    sudo mv /etc/localtime /etc/localtime_old
    sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    方法2,建立软连接
    sudo rm localtime
    sudo mv localtime_old localtime
    sudo ln -snf /usr/share/zoneinfo/Asia/Shanghai localtime
    #先把我刚刚copy过来的localtime删掉,然后把原本的localtime(现在叫localtime_old)的名字改回来,最后把软连接链接到上海所在的时区

    然后我们需要配置ntp.conf文件

    自带的ntp.conf是这样的,为了防止有问题,我们复制一份备用:

    # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
    
    driftfile /var/lib/ntp/ntp.drift
    
    # Leap seconds definition provided by tzdata
    leapfile /usr/share/zoneinfo/leap-seconds.list
    
    # Enable this if you want statistics to be logged.
    #statsdir /var/log/ntpstats/
    
    statistics loopstats peerstats clockstats
    filegen loopstats file loopstats type day enable
    filegen peerstats file peerstats type day enable
    filegen clockstats file clockstats type day enable
    
    # Specify one or more NTP servers.
    
    # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
    # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
    # more information.
    pool 0.ubuntu.pool.ntp.org iburst
    pool 1.ubuntu.pool.ntp.org iburst
    pool 2.ubuntu.pool.ntp.org iburst
    pool 3.ubuntu.pool.ntp.org iburst
    
    # Use Ubuntu's ntp server as a fallback.
    pool ntp.ubuntu.com
    
    # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
    # details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
    # might also be helpful.
    #
    # Note that "restrict" applies to both servers and clients, so a configuration
    # that might be intended to block requests from certain clients could also end
    # up blocking replies from your own upstream servers.
    
    # By default, exchange time with everybody, but don't allow configuration.
    restrict -4 default kod notrap nomodify nopeer noquery limited
    restrict -6 default kod notrap nomodify nopeer noquery limited
    
    # Local users may interrogate the ntp server more closely.
    restrict 127.0.0.1
    restrict ::1
    
    # Needed for adding pool entries
    restrict source notrap nomodify noquery
    
    # Clients from this (example!) subnet have unlimited access, but only if
    # cryptographically authenticated.
    #restrict 192.168.123.0 mask 255.255.255.0 notrust
    
    
    # If you want to provide time to your local subnet, change the next line.
    # (Again, the address is an example only.)
    #broadcast 192.168.123.255
    
    # If you want to listen to time broadcasts on your local subnet, de-comment the
    # next lines.  Please do this only if you trust everybody on the network!
    #disable auth
    #broadcastclient
    
    #Changes recquired to use pps synchonisation as explained in documentation:
    #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
    
    #server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS
    #fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware
    
    #server 127.127.22.1                   # ATOM(PPS)
    #fudge 127.127.22.1 flag3 1            # enable PPS API

    架设自己的ntp服务器:

    driftfile /var/lib/ntp/ntp.drift
    leapfile /usr/share/zoneinfo/leap-seconds.list    #tzdata提供的跳跃秒定义,表示不知这是啥
    
    #如下这几行不知道是什么意思,没动
    statistics loopstats peerstats clockstats
    filegen loopstats file loopstats type day enable
    filegen peerstats file peerstats type day enable
    filegen clockstats file clockstats type day enable
    
    
    restrict -4 default kod notrap nomodify nopeer noquery limited
    restrict -6 default kod notrap nomodify nopeer noquery limited
    #拒绝ipv4和ipv6的用户:ignore表示拒绝所有ntp链接;nomodify客户端不可以修改服务器时间参数,但是可以查询。noquery,不提供ntp网络校时 ;notrust,没有认证的都拒绝。notrap:不提供trap这个远程登录功能,具体啥功能,不知。
    
    # Local users may interrogate the ntp server more closely.
    # restrict 210.72.145.44    #daqing 放行两个ip进入本ntp服务器,也就是我的上层服务器,不放行好像也可以的
    # restrict 114.118.7.163
    restrict 127.0.0.1    #daqing 放行本机来源
    restrict ::1
    restrict 192.168.3.0 mask 255.255.255.0 nomodify    #daqing 放行局域网来源的校时
    
    # Needed for adding pool entries,具体不知,猜测是其他ip的权限问题
    restrict source notrap nomodify noquery    
    
    #设置上层服务器,prefer优先
    server 210.72.145.44 prefer
    server 114.118.7.163

      server 127.127.1.0 #没有外部服务器的时候用本机时间
      fudge 127.127.1.0 stratum 10 #stratum是根据上层的server而设定的

      server 0.asia.pool.ntp.org    #设置一堆ntp服务池,为啥要有ntp服务池呢,个人发现哈,ntp上层服务器设置比较少的时候,下层的ntp用户校时会出现错误,提示timeout,可能ntp校时没那么简单,并不是只要简单的post和get就能解决的,所以ntp服务器也蛮忙的
      server 1.asia.pool.ntp.org
      server 2.asia.pool.ntp.org
      server 3.asia.pool.ntp.org

    还有个注意的事情,是有关这个的,如下所示,这是ubuntu默认的ntp服务池,我们可以不动它

    pool 0.ubuntu.pool.ntp.org iburst
    pool 1.ubuntu.pool.ntp.org iburst
    pool 2.ubuntu.pool.ntp.org iburst
    pool 3.ubuntu.pool.ntp.org iburst

    经过本人初步查看,写了这个的就是写了ntp服务连接池,就近选择几个ntp服务器自己去校时,校时服务器的ip有可能变哈,好处是连接池里有一堆服务器可用,很健壮,不好的地方是,这不是你想要的那一个。

    当然了,我们也可以选择不适用默认的校时服务器,我们用自己的,那么请访问:这里,进去以后根据自己的需要选择,亚洲或者亚洲-中国。

    大约这样:

    按照提示把重点copy到ntp.conf中就可以了。顺带提一嘴哈,个人怀疑上面这种网址而非ip的形式,需要配置电脑的dns,不然很可能解析不了网址。

    设置完成以后我们就可以启动ntp服务了

    sudo service ntp start

    1)检查:123端口

    sudo netstat -tlunp |grep ntp
    
    #得到:
    udp        0      0 192.168.3.155:123       0.0.0.0:*                           9589/ntpd           
    udp        0      0 127.0.0.1:123           0.0.0.0:*                           9589/ntpd           
    udp        0      0 0.0.0.0:123             0.0.0.0:*                           9589/ntpd           
    udp6       0      0 fe80::3730:c076:650:123 :::*                                9589/ntpd           
    udp6       0      0 ::1:123                 :::*                                9589/ntpd           
    udp6       0      0 :::123                  :::*                                9589/ntpd    

     2)检查校时情况

    ntpq -p
    
    #你将会得到
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     210.72.145.44   .INIT.          16 u    - 1024    0    0.000    0.000   0.000
    -114.118.7.163   123.139.33.3     2 u  123  128  377   27.024    5.601   1.697
     dns1.synet.edu. .INIT.          16 u    - 1024    0    0.000    0.000   0.000
    *time.neu.edu.cn .PTP.            1 u   47  128  377   57.993   -2.018   4.137
     42.96.167.209   .INIT.          16 u    - 1024    0    0.000    0.000   0.000
     LOCAL(0)        .LOCL.          10 l  65m   64    0    0.000    0.000   0.000
    +time.cloudflare 10.21.8.19       3 u   43  128  377  229.535    0.600   3.596
    +ntp.xtom.com.hk 223.255.185.3    3 u  103  128  177   96.228   -0.297   2.505
    -ntp.hkg10.hk.le 130.133.1.10     3 u   97  128  277  194.752  -15.462   1.344
    -time.cloudflare 10.71.14.51      3 u   34  128  377  209.012    4.036   2.319

    注意哈,如果你刚刚开机或者刚刚重启了ntp,那么你有可能要等十多分钟才能连上某台校时服务器。具体原因不知

    来,解释一下:

    服务器前面的参数
    * : 它告诉我们远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供
    + : 它将作为辅助的NTP Server和带有*号的服务器一起为我们提供同步服务.当*号服务器不可用时它就可以接管
    - : 远程服务器被clustering algorithm认为是不合格的NTP Server
    x : 远程服务器不可用

    列含义:

    remote : 本地机器所连接的远程NTP服务器,有的是ip有的是网址,都是正常的
    refid : 指的是参考的上一层NTP主机的地址
    st : 远程服务器的级别。由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16.为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的
    when : 用做计时,用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步
    poll : 本地主机和远程服务器多少时间进行一次同步(单位为秒)
    reach : 这是一个八进制值,表示已经向上层NTP服务器要求更新的次数。每成功连接一次,它的值就加1
    delay : 网络传输过程中延迟的时间,单位为微秒
    offset : 我们本地机和服务器之间的时间差别。单位为毫秒
    jitter : Linux系统时间与BIOS硬件时间的差异时间,单位为微秒

    3)用客户机nmap检查一下端口:

    nmap 192.168.3.155 -p 123
    
    Starting Nmap 7.60 ( https://nmap.org ) at 2021-04-13 11:03 CST
    Nmap scan report for 192.168.3.155
    Host is up (0.00082s latency).
    
    PORT    STATE  SERVICE
    123/tcp closed ntp
    
    Nmap done: 1 IP address (1 host up) scanned in 0.02 seconds
    
    #没想到我自己也整了一个closed状态的ntp服务器,,无奈.jpg

    顺带提一嘴:有个命令ntpstat瞅着贼好使,找了半天没找到,原来得装一下,无奈.jpg

    sudo apt-get install ntpstat
    ntpstat
    #得到返回:
    synchronised to NTP server (202.118.1.81) at stratum 2 
       time correct to within 46 ms
       polling server every 512 s
    含义是:已经和202.118.1.81服务器连上了,这是一个二级的服务器,时间已经校正约46ms,每512秒会去主动更新时间

    把ntp加入开机自启,然后把systemd-timesyncd的开机自启取消掉

    systemctl disable systemd-timesyncd
    #禁用systemd-timesyncd,这个方法和普通service取消开机自启不大一样哦
    sudo update-rc.d ntp enable
    #ntp加入开机自启
    
    sudo service --status-all
    #前面带加号的表示开机自启的,你能找到ntp,但是抱歉你找不到systemd-timesyncd,原因不明

    话说,为啥要把systemd-timesyncd取消掉呢?因为我发现,它能修改本机时间。而作为ntp服务器,我觉得ntp应该拥有对本机时间的独占权。

    3,校时客户端配置

    校时客户端有很多种方法,但是我个人倾向于使用自带的轻量级的systemd-timesyncd的这个,我们把它的配置改为局域网内的服务器155:

    sudo vim /etc/systemd/timesyncd.conf
    #改这里:加上155,考虑到局域网内也没有备选服务器,就直接一个校时服务器就好了
    [Time]
    NTP=192.168.3.155
    #FallbackNTP=ntp.ubuntu.com
    #RootDistanceMaxSec=5
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048

    然后重启systemd-timesyncd服务并且查看状态:

    systemctl restart systemd-timesyncd.service
    sudo service systemd-timesyncd status

     4,校时测试

    校时服务器:192.168.3.155

    校时客户端:192.168.3.142

    1),ntp服务器上时间错误,能否ntp能否给我们校正过来?

    我们把155上的时间改为2021-01-01 11:11:11,等着看客户端上是不是会同步错误的时间

    修改服务器上的时间:

    sudo timedatectl set-time "2021-01-01 11:11:11"

    改完以后,呵呵n分钟,ntp进程挂了,看来配置好ntp服务器以后,不能乱改时间,系统时间的认为改动有可能会导致ntp进程挂掉。

    手动启动ntp:

    sudo service ntp start

    据说ntp要连上上层服务器得需要5-15分钟左右,我们等它,最终证明,ntp确实把时间改成正确的了。正式环境不建议进行这种操作哈,timedatectl一改以后,系统log的日志都改了,所以谨慎使用

    ps:还有一种改时间发方法如下,我尝试了一下,根本改不了时间,改完了能维持个1秒吧

    #格式date MMddhhmmyyyy
    date 010112002021
    hwclock -w    #软件时间写到bios里
    date    #查看当前软件时间
    hwclock -r    #查看bios系统时间

    所以结论是,ntp确实能把时间给你改过来,但是ntp已经运行好了以后,你乱改时间有可能导致ntp进程挂掉。

    2),客户端上的时间能否和服务器保证同步?

    服务器上修改时间:

    sudo service ntp stop    #ntp校时服务先关掉
    sudo timedatectl set-ntp false    #关闭ntp服务器上的校时服务,省的把时间给我们改对咯
    sudo timedatectl set-time '2021-01-01 00:00:00'     #改时间
    date    #查看系统时间
    sudo reboot #直接重启一下吧

    客户端上:

    systemctl restart systemd-timesyncd.service
    #重启systemd校时
    sudo service systemd-timesyncd status
    #查看systemd校时状态
    
    #拿到返回:
    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2021-04-13 14:35:35 CST; 20s ago
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 6141 (systemd-timesyn)
       Status: "Idle."    #此处说明校时没成功,有可能是服务器那边还没连上上层服务器的原因,我们等它一下
        Tasks: 2 (limit: 4915)
       CGroup: /system.slice/systemd-timesyncd.service
               └─6141 /lib/systemd/systemd-timesyncd
    
    Apr 13 14:35:35 center-OptiPlex-7060 systemd[1]: Starting Network Time Synchronization...
    Apr 13 14:35:35 center-OptiPlex-7060 systemd[1]: Started Network Time Synchronization.
    date    #时间还是最新时间,没改

    5分钟以后,我们发现服务器上的时间被ntp改成了正确的。。。。无语.jpg

    那我们,再改一次,ps:生产环境请不要这么粗暴哈,因为强制改时间确实导致ntp又挂了一次

    sudo timedatectl set-time "2021-01-01 11:11:11"

    这次好了,客户端改了:

    sudo service systemd-timesyncd status
    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2021-04-13 14:35:35 CST; 3 months 10 days left
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 6141 (systemd-timesyn)
       Status: "Synchronized to time server 192.168.3.155:123 (192.168.3.155)."    #表示已经和ntp服务器同步成功
        Tasks: 2 (limit: 4915)
       CGroup: /system.slice/systemd-timesyncd.service
               └─6141 /lib/systemd/systemd-timesyncd
    
    Jan 01 11:12:58 center-OptiPlex-7060 systemd-timesyncd[6141]: Synchronized to time server 192.168.3.155:123 (192.168.3.155).
    
    date
    #显示的是和服务器上一样的时间

    结论:ntp校时服务器上时间改了,客户端确实也会改,但是延迟多久改,是不是等到ntp服务器连上上层服务器才会改,目前没看出什么规律。个人猜测,依照我们的配置,只有当客户端和服务器时间差大于5秒,并且被32-2048秒的systemd-timesyncd发动一次的监控程序发现的时候,才会和ntp校时服务器同步,当然重启的时候也会同步,重启systemd-timesyncd的时候应该也会同步,但是有时候并不成功,原因目前不知。但是个人发现,当ntp服务器时间正常,ntpq -p能看到它连上上层服务器的时候,重启systemd-timesyncd一般能成功同步到服务器的时间。

  • 相关阅读:
    Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
    一种Unity2D多分辨率屏幕适配方案
    Unity 2D骨骼动画2:创建真实动画
    Unity2d 骨骼动画1:介绍
    Unity2d 骨骼动画3:介绍Mecanim和脚本
    Unity2D研究院之自动生成动画、AnimationController、Prefab(一)
    mongochef如何链接有权限的mongodb3.x数据库
    moogodb3.x总结
    mongodb3.x用户角色
    MongoDB 基础 -安全性-(权限操作)
  • 原文地址:https://www.cnblogs.com/0-lingdu/p/14345653.html
Copyright © 2020-2023  润新知