centos7多主机--实现时间同步chrony服务

1、chronyc命令

chronyc–设置时间与时钟服务器同步

chronyc sources -v		//查看 ntp_servers
chronyc	sourcestats		//查看时间同步源状态
chronyc activity -v		//查看 ntp_servers 是否在线
chronyc tracking -v		//查看 ntp 详细信息
chronyc -a makestep		//强制同步下系统时钟

chrony.conf 默认配置说明

# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# 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

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

# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3

# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通过使用 hwtimestamp 指令启用硬件时间戳
# 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

# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# 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

# 指定包含 NTP 身份验证密钥的文件。
# 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

2、timedatectl命令

timedatectl–控制系统时间和日期

timedatectl									//查看日期时间、时区及 NTP 状态
timedatectl list-timezones					//查看已知时区列表
timedatectl set-timezone Asia/Shanghai		//修改时区
timedatectl set-time "2021-04-15 15:50:20"	//修改日期时间(可以只修改其中一个)
timedatectl set-ntp true/flase				//开启 NTP服务

3、常见时区

1) UTC 整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。

(2)GMT 格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的×××格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)

(3)CST 中国标准时间 (China Standard Time) GMT + 8 = UTC + 8 = CST

(4)DST夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用

3、实现多主机时间同步

1、环境准备

1.环境Centos7
2.主机:
	master:
		192.168.8.156
	slave:
		192.168.8.157
		192.168.8.158
3.确定本机是否默认安装chrony服务
4.防火墙添加ntp服务或者关闭防火墙(NTP服务使用123/UDP端口协议)
5.检查时区
1、确认是否含有chrony
>>> rpm -qa chrony
chrony-3.4-1.el7.x86_64
2、没有就下载
[root@szx network-scripts]# yum install -y chrony

3、防火墙操作
systemctl stop firewalld		//停止firewalld服务
systemctl disable firewalld	//禁止firewalld开机启动
或者
firewall-cmd --add-service=ntp --permanent		//firewalld防火墙内放行NTP服务
firewall-cmd --reload

4、检查时区
>>> timedatectl
       Time zone: America/Los_Angeles (PDT, -0700) 如果显示Asia/Shanghai,即正常
这里明显是美国时区,进行修改:
	timedatectl list-timezones |  grep  -E "Asia/Sha.*"	// 查看亚洲上海可用时区
	timedatectl set-timezone "Asia/Shanghai"			//设置当前系统为上海时区
	chronyc -a makestep									//手动同步系统时钟
此时在此查看timedatectl,显示Time zone: Asia/Shanghai (CST, +0800)正常

2、主机配置:192.168.8.156

1、修改 /etc/chrony.conf配置文件
>>> vim /etc/chrony.conf
server s1a.time.edu.cn iburst			// 北京邮电大学授时中心
server ntp.aliyun.com iburst			//  阿里云授时中心
#server 0.centos.pool.ntp.org iburst	//注释掉默认的ntp服务器,该服务器同步时间略慢
#server 1.centos.pool.ntp.org iburst	//格式为:server 服务器ip地址 iburst 
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
allow 192.168.0.0/16					//指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器

2、启动chrony服务
systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd

3、手动同步并查看同步源,确保同步成功
chronyc -a makestep		// 手动同步系统时钟
chronyc sources -v		//查看时间同步源
===============================================================================
^? st0-bupt-1.ntp.edu.cn         0   7     0     -     +0ns[   +0ns] +/-    0ns
^* 203.107.6.88                  2   6    17    37   +965us[+1411us] +/-   21ms

3、客户端配置–两台机器同步进行以下操作即可

1、修改 /etc/chrony.conf配置文件
>>> vim /etc/chrony.conf
server 192.168.8.158 iburst		// 默认的四个注释掉
allow 192.168.8.0/24				//添加master的ip连接到扮演时钟服务器的机器

2、启动chrony服务
systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd

4、查看时间同步状态(三台主机都需要查看)

>>> timedatectl status		//两个yes说明同步成功
      Local time: 五 2022-08-26 19:25:53 CST
  Universal time: 五 2022-08-26 11:25:53 UTC
        RTC time: 五 2022-08-26 11:25:53
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes			
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

如果遇到没开启的,手动开启网络时间同步
>>> timedatectl set-ntp true

此时三台服务器应该时间同步完成,可以查看date命令,显示当前系统时间

5、如果需要修改系统时间,需要先关闭NTP时间同步服务,再去修改系统时间,最后再开启NTP时间同步服务

1、关闭NTP时间同步服务
timedatectl set-ntp flase
2、修改系统时间
timedatectl set-time "2021-08-15 15:30:20"
当然也可以修改其中一部分,如修改年月日 或者 时分秒
或者
timedatectl set-time "2021-08-15"
timedatectl set-time "15:30:20"

3、开启NTP时间同步服务
timedatectl set-ntp true

4、时间相关命令(date/clock)

date -s 16:03:00		// 修改时间--设置时分秒
date -s "2012-05-23 01:01:01"    // 这样可以设置全部时间
date -s "20201021 18:30:50"

clock -w	// 硬件时间同步系统时间
clock -s	//系统时间同步硬件时间,hwclock --systohc

参考chrony 详解

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS 7.9版本(CentOS-7-i386-Everything-2009)适用于i386,必须集齐9个文件才能一起解压缩使用,9个文件下载地址: CentOS-7-i386-Everything-2009.part09.rar https://download.csdn.net/download/weixin_43800734/20433341 CentOS-7-i386-Everything-2009.part08.rar https://download.csdn.net/download/weixin_43800734/20433316 CentOS-7-i386-Everything-2009.part07.rar https://download.csdn.net/download/weixin_43800734/20433271 CentOS-7-i386-Everything-2009.part06.rar https://download.csdn.net/download/weixin_43800734/20433250 CentOS-7-i386-Everything-2009.part05.rar https://download.csdn.net/download/weixin_43800734/20433222 CentOS-7-i386-Everything-2009.part04.rar https://download.csdn.net/download/weixin_43800734/20433129 CentOS-7-i386-Everything-2009.part03.rar https://download.csdn.net/download/weixin_43800734/20433111 CentOS-7-i386-Everything-2009.part02.rar https://download.csdn.net/download/weixin_43800734/20433025 CentOS-7-i386-Everything-2009.part01.rar https://download.csdn.net/download/weixin_43800734/20432977 CentOS是免费的、开源的、可以重新分发的开源操作系统,CentOS(Community Enterprise Operating System,中文意思是社区企业操作系统)是Linux发行版之一。 CentOS Linux发行版是一个稳定的,可预测的,可管理的和可复现的平台,源于Red Hat Enterprise Linux(RHEL)依照开放源代码(大部分是GPL开源协议 )规定释出的源码所编译而成。自2004年3月以来,CentOS Linux一直是社区驱动的开源项目,旨在与RHEL在功能上兼容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值