CentOS时间同步

Sakura 发布于 2023-08-23 161 次阅读


内网环境(无外网)

  1. 每台机器均修改当前时区
timedatectl set-timezone Asia/Shanghai
  1. 每台机器均安装chrony
yum install -y chrony
# 开机自启
systemctl enable chronyd
  1. 服务端配置
/etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
# 表示与本机IP同步时间,其他server注释或删除
# 注:内网环境必须这么做,否则其他会无法同步成功。 如果通公网,可以使用公网的服务器时间
server 127.127.1.0

# Record the rate at which the system clock gains/losses time.
# 为了在下次启动时稳定的同步,系统时钟的预估漂移需要保存到指定的文件中
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 如果系统时钟由于某种原因与启动后的服务器时间相差甚远,允许 chronyd 
# 通过步进而不是回转来快速纠正它,这个过程将花费很长时间
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 为了使客户端实时时钟接近服务器的时钟,以便下次时钟启动时更接近真实的时间
# 增加了一种 rtcsync 模式,该模式下,系统时间会定期的拷贝到实时时钟里
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
# 允许指定网络的主机同步时间,不指定就是拒绝所有,且默认不开启。
# 如果要允许所有则:  allow 0.0.0.0/24
# 如下,允许一个网段,如果有多个网段,则添加多个allow
allow 192.168.59.0/24
#allow ...

# Serve time even if not synchronized to a time source.
# 不去同步任何人的时间,时间同步服务级别
# 开启后,即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授予其他客户端
#注:内网环境必须开,否则其他虚拟机无法同步时间
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
# chrony日志存放路径
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

# 重启
systemctl restart chronyd

# 查看时间同步源状态
chronyc sourcestats -v

# 查看客户端
chronyc clients
  1. 客户端配置
vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 同步时间的服务器 IP 或 域名
# 这直接使用服务端的ip即可
server 192.168.59.128 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
# 系统时钟的预估漂移保存到指定的文件中,是为了在下次启动时能稳定的同步
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 如果系统时钟由于某种原因与启动后的服务器时间相差甚远,允许 chronyd 
# 通过步进而不是回转来快速纠正它
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 为了使客户端实时时钟接近服务器的时钟,以便下次时钟启动时更接近真实的时间
# 增加了一种 rtcsync 模式,该模式下,系统时间会定期的拷贝到实时时钟里
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
# chrony日志存放路径
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

# 重启
systemctl restart chronyd

# 如果没有自动同步 则可以执行立即同步
chronyc tracking

# 查看连接状态
chronyc sources -v

^?同步失败
^* 同步成功

外网同步时间

  1. 国内NTP时间服务器
ntp.ntsc.ac.cn #国家授时中心 网络授时服务器的域名
cn.ntp.org.cn #中国
edu.ntp.org.cn #中国教育网
ntp1.aliyun.com #阿里云
ntp2.aliyun.com #阿里云
cn.pool.ntp.org #最常用的国内NTP服务器
  1. 查看/修改时区
# 查看时区
timedatectl
# 修改时区
timedatectl set-timezone Asia/Shanghai
  1. 安装chrony
yum install -y chrony
  1. 配置
vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#一定要先ping,要确保地址能ping通才可以正常同步啊!!!!
server cn.ntp.org.cn iburst
server cn.pool.ntp.org iburst
#server 0.cn.pool.ntp.org iburst
#server 1.cn.pool.ntp.org iburst
#server 2.cn.pool.ntp.org iburst
#server 3.cn.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 1.2.105.0/24
allow 1.2.0.0/16

# Serve time even if not synchronized to a time source.
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

# 重启
systemctl restart chronyd
# 如果没有自动同步 则可以执行立即同步
chronyc tracking

ps:抄自https://blog.csdn.net/cuichongxin/article/details/123178318