时间同步

ntpdate时间同步
# 1 调整时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 2 手动调整时间
yum -y install ntpdate
ntpdate -u 202.112.10.36  (北邮)
hwclock -w #将系统时间同步到硬件

# 3 直接加入计划任务
crontab –e
30 03 * * * /usr/sbin/ntpdate -u 202.120.2.101 && hwclock -w
systemctl restart crond


# 4 编写同步脚本并加入计划任务
vim /usr/local/ntpdateup.sh

#! /bin/bash
 /usr/sbin/ntpdate 202.112.10.36 &> /dev/null
 /usr/sbin/hwclock -w &> /dev/null #将系统时间同步到硬件
 /usr/sbin/hwclock -s &> /dev/null  #将硬件时间同步到系统
 chmod +x /usr/local/ntpdateup.sh # 加权限

crontab –e
# time sync
30 03 * * * /bin/bash /usr/local/ntpdateup.sh
systemctl restart crond


# 时间简写区别
UTC  世界协调时间  地球的标准时间 原子时间
GMT  格林尼治标准示时间  零时区的时间
CST 中央标准时间 指世界不同时区的地方各自地方时
RTC 系统的硬件时间 集成电路时钟芯片的时间

 

centos7和 ubuntu18.04 以后时间同步软件成为chrony 

# chrony是C/S架构


# 1 安装服务端
yum install -y chrony
systemctl enable chronyd && systemctl start chronyd

# 2 配置server端

# 2.1 外网服务端
# 备份
cp /etc/chrony.conf /etc/chrony.conf.bak# 编写配置文件
cat >/etc/chrony.conf<< EOF
# 配置 本地IP为ntpd服务器
server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
# 记录系统时钟增益/损耗时间的速率
driftfile /var/lib/chrony/drift
# 如果其偏移量大于1秒,允许在前三个更新中步进系统时钟
makestep 1.0 3
# 允许all client端同步时间
allow all
# server指定的时间服务器同步失败时返回当前时间服务器的时间给客户端
local stratum 10
# 启用内核的实时时钟(RTC)同步
rtcsync
# 日志
logdir /var/log/chrony
EOF
# 配置生效
systemctl restart chronyd


# 2.2 配置内网服务端
# 备份
cp /etc/chrony.conf /etc/chrony.conf.bak
# 编写配置文件
cat >/etc/chrony.conf<< EOF
# 配置 本地IP为ntpd服务器
server 192.168.180.79 iburst
# 记录系统时钟增益/损耗时间的速率
driftfile /var/lib/chrony/drift
# 如果其偏移量大于1秒,允许在前三个更新中步进系统时钟
makestep 1.0 3
# 允许all client端同步时间
allow all
# server指定的时间服务器同步失败时返回当前时间服务器的时间给客户端
local stratum 10
# 启用内核的实时时钟(RTC)同步
rtcsync
# 日志
logdir /var/log/chrony
EOF

systemctl restart chronyd


# 2.3 检查服务端状态
# timedatectl 状态

               Local time: Fri 2021-11-19 15:06:04 CST
         Universal time: Fri 2021-11-19 07:06:04 UTC
                 RTC time: Fri 2021-11-19 07:06:04
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes#开启NTP同步
             NTP service: active#服务激活状态
        RTC in local TZ: no
# 查看ntp_servers状态  chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address             Stratum Poll Reach LastRx Last           sample               
===============================================================================
^* master                       10      7   377   27m    -51ns[-2430ns] +/- 5695ns

# "*"    ""   ""

# 查看ntp详细信息  chronyc tracking -v

Reference ID    : C0A8B44F (master)
Stratum         : 11
Ref time (UTC)  : Thu Jan 28 03:15:48 2021
System time     : 0.000000003 seconds slow of NTP time
Last offset     : -0.000002379 seconds
RMS offset      : 0.000002379 seconds
Frequency       : 11.547 ppm slow
Residual freq   : +0.462 ppm
Skew            : 80.292 ppm
Root delay      : 0.000011053 seconds
Root dispersion : 0.166260049 seconds
Update interval : 0.0 seconds
Leap status     : Normal
# 查看 ntp_server 是否在线 chronyc activity -v
chronyc activity -v
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
# 查看ntp_sync 状态 chronyc sourcestats -v
210 Number of sources = 1
                             .- Number of sample points in measurement set.
                            /    .- Number of residual runs with same sign.
                           |    /    .- Length of measurement set (time).
                           |   |    /      .- Est. clock freq error (ppm).
                           |   |   |      /           .- Est. error in freq.
                           |   |   |     |           /         .- Est. offset.
                           |   |   |     |          |          |   On the -.
                           |   |   |     |          |          |   samples. \
                           |   |   |     |          |          |             |
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
master                      3   3     4     +0.462    113.016    -51ns   385n



# 3 配置chrony 客户端 yum install -y chrony systemctl enable chronyd && systemctl start chronyd # 备份 cp /etc/chrony.conf /etc/chrony.conf.bak # 配置客户端配置文件 cat >/etc/chrony.conf<<EOF server 192.168.180.79 iburst driftfile /var/lib/chrony/drift makestep 1.0 3 allow all rtcsync logdir /var/log/chrony EOF systemctl restart chronyd

 

posted on 2023-07-04 17:21  luokeli  阅读(43)  评论(0)    收藏  举报

导航