centos8 时间不一致问题解决

5 篇文章 0 订阅
3 篇文章 0 订阅

由于centos7以上版本对如下命令已经不能用:

yum install ntp
# 报如下错,
Error: Unable to find a match
# 使用如下方法 安装ntp服务
# 添加源
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
# 安装ntp服务
dnf  install wntp
# 时间同步
ntpdate ntp1.aliyun.com
# 将系统时间写入硬件时间
hwclock --systohc
# 强制系统时间写入CMOS中防止重启失效
hwclock -w

通过查询发现,本地搭建chrony服务器+所有客户端同步服务器时间,7以上提供了chrony,安装与配置方法如下:

# 安装
yum install -y chrony
# 启动
systemctl start chronyd
# 注册成开机启动
systemctl enable chronyd

如上配置好了以后,现行配置文件的修改

vim  /etc/chrony.conf

配置文件如下:

# 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 注释掉 server 0.centos.pool.ntp.org iburst
# 新增
server cn.ntp.org.cn iburst
server time.windows.com iburst

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
driftfile /var/lib/chrony/drift

# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3

# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync

# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *

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

# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#allow 192.168.0.0/16
#deny 192.168/16
allow 0.0.0./0 # 允许所有

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

# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys

#从system tz数据库中获取TAI-UTC偏移量和闰秒。
leapsectz right/UTC

# 指定日志文件的目录。
logdir /var/log/chrony

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

centos7以上新加命令timedatectl

# 查看
timedatectl 
# 删除 localtime
rm /etc/locatime
# 从/usr/share/zoneinfo/中创建软连接以替换当前的时区信息
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 再次查看系统时间
timedatectl

至此,时间解决:
如果此时服务器上装有mysql,可以使用select now()查看,此时时间已经改成正确的了。

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A吴广智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值