ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

centos8与centos7不同环境下时间同步

2022-08-03 20:04:52  阅读:200  来源: 互联网

标签:同步 timedatectl ntp server centos7 org com pool centos8


centos7:

最常见的使用ntpdate同步主机时间:

yum install -y ntpdate

设置时间同步

/usr/sbin/ntpdate cn.pool.ntp.org

加入定时任务crontab

执行:crontab -e
在文件末尾添加:
*/* 1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
然后输入::x, 保存
这样服务器就会每天1点去矫正服务器的时间了。

centos8:

CentOS8系统中,原有的时间同步服务 ntp/ntpdate服务已经无法使用,使用yum安装,提示已不存在。

安装chrony并设置开机启动

yum install -y chrony
systemctl start chronyd
systemctl enable chronyd

修改时区方式

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai

临时同步时间方法:

chronyd -q ‘server ntp.ntsc.ac.cn iburst’

永久配置

$ cat /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.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
# 阿里
server ntp.aliyun.com
# 腾讯
server time1.cloud.tencent.com
server time2.cloud.tencent.com
server time3.cloud.tencent.com
server time4.cloud.tencent.com
server time5.cloud.tencent.com
# 苹果
server time.asia.apple.com
# 微软
server time.windows.com
# 其他
server cn.ntp.org.cn

# 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

# 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

时间同步

chronyc sources -v

启动服务及时区设置

# 启动服务
$ systemctl start chronyd

# 开机启动
$ systemctl enable chronyd

# 查看当前状态
$ systemctl status chronyd

# 查看亚洲时区
$ timedatectl list-timezones | grep Asia

# 设置时区
$ timedatectl set-timezone Asia/Shanghai

验证服务

$ 查看现有的时间服务器
$ chronyc sources -v

# 查看时间服务器状态
$ chronyc sourcestats -v

# 显示时钟同步相关参数
$ chronyc tracking

# 查看当前时区及时间
$ timedatectl 

手动同步时间

# 使用 ntpdate 同步时间
$ ntpdate ntp.aliyun.com
这里也可以如上文centos7一样加入定时任务
# chronyd 未启动时,如下命令同步时间
$ chronyd -q 'server pool.ntp.org iburst'
# chronyd 启动时,使用如下命令同步时间
$ chronyc -a 'burst 4/4' && sleep 10 && chronyc -a makestep

手动设置时间

# date 设置时间
$ date -s '2021-06-03 19:00:00'
# 关闭 ntp 同步后,才可以使用 timedatectl 进行时间设置
$ timedatectl set-ntp false
# 设置日期和时间
$ timedatectl set-time '2021-06-03 19:00:00'
# 设置日期
$ timedatectl set-time '2021-06-03'
# 设置时间
$ timedatectl set-time '19:00:00'
# 设置完成后,再开启
$ timedatectl set-ntp true

标签:同步,timedatectl,ntp,server,centos7,org,com,pool,centos8
来源: https://www.cnblogs.com/khtt/p/16548521.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有