https://www.cnblogs.com/freeweb/p/12980841.html
redhat/centos 7.x默认使用的时间同步服务为ntp服务,但是从redhat/centos 8开始在官方的仓库中移除了ntp软件,换成默认的chrony进行时间同步的服务,虽然也可以通过添加第三方的源安装ntp,但是毕竟还是使用官方推荐的更好一些,使用和ntp基本上一样,个人感觉比ntp还更简单,下面简单的叙述一下配置过程. 默认centos8安装系统后已经存在chrony的软件包了,可以通过 rpm -q chrony 确认是否存在,如果不存在使用yum安装即可: yum install chrony chrony本身既可以作为客户端向其他时间服务器同步时间又可以提供时间同步的服务,也就是说同时作为客户端和服务端,配置文件统一都是:/etc/chrony.conf,通常在一个集群中,总会有服务端和客户端的区分,下面来看一下具体的配置: 1. 服务端配置 编辑配置文件/etc/chrony.conf,首先可以看到有个pool的配置如下: 这个表示要请求的时间服务器,这里服务端可以向互联网的公共服务器请求同步时间,然后其他集群内服务器可以向这台服务器同步时间,这样集群内的时间就一致了,所以这里保持默认即可,也可以换成比如阿里云的ntp服务器;需要注意的是这并不是1个时间服务器,而是本地请求这个服务会返回一些可用的列表,然后本地程序选择列表中的某个时间服务器进行同步,这个待会可以查看资源看到具体的列表. 然后就是要打开allow配置,设置允许请求的服务器网段,比如这里配置如下: 这样表示允许172.16.0.0网段的所有机器访问,即172.16.0.x的ip都可以向当前服务器请求同步,具体根据实际的集群进行配置.
就以上这两条,ntp服务端就配置完了,没错,就是这么简单!
2. 客户端配置 同样编辑配置文件/etc/chrony.conf,这个更简单,直接把pool这里改成上面的服务端节点就可以: 如果hosts中机器都配置了,那这里写主机名也可以.
最后服务端和客户端都要启动服务: systemctl start chronyd ,查看服务状态: systemctl status chronyd ,状态中可以看到同步的记录,服务启动后查看服务器当前时间一般就能看出来都是同步的了 设置服务开机自动启动: systemctl enable chronyd chronyd的服务端口和NTP一样是udp的123端口,使用 netstat -an | grep 123 或 ss -a | grep 123 都可以看到端口监听的情况 之前我的疏忽将端口搞成323了,323是绑定回环网卡用于chronyc监视操作使用的默认端口,而非服务端口,这个配置可以支持通过网络运行chronyc客户端命令,比如: chronyc -h 127.0.0.1 -p 323 sources -v ,默认本地使用chronyc走的是unix sock。非常感谢网友@Enke指出错误~ 然后可以使用命令: chronyc sources -v 查看当前的同步源,比如刚才的服务端可以看到多个源: 其他客户端就只有1个源如下: 可以使用命令 chronyc sourcestats -v 查看当前的同步状态 最后还可以使用命令: chronyc -a makestep 手动同步一次时间,返回200 OK表示同步成功;使用 chronyc tracking 可以显示系统时间信息;直接输入chronyc可以进入交互式界面进行相关的操作,具体可以输入help查看对应的帮助. 另外还有一点需要说一下就是centos中的工具timedatectl 里面也有个ntp service,开启时状态为active: 这里需要关闭才可以手动修改本机时间,需要注意的就是这个工具不做任何时间同步的具体操作而是依赖系统的服务是什么,比如系统使用了ntp那么这里就依赖ntp的配置,系统使用了chrony这里就是chrony的配置,而这个timedatectl就是做了一层包装,后端的服务还是ntp/chrony这些组件.
- # Use public servers from the pool.ntp.org project.
- # Please consider joining the pool (http://www.pool.ntp.org/join.html).
- pool 2.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.11.0/24
- # Serve time even if not synchronized to a time source.
- local stratum 10
- # Specify file containing keys for NTP authentication.
- keyfile /etc/chrony.keys
- # Get TAI-UTC offset and leap seconds from the system tz database.
- leapsectz right/UTC
- # Specify directory for log files.
- logdir /var/log/chrony
- # Select which information is logged.
- #log measurements statistics tracking
复制代码上面是我的配置,这样配置以后重启服务 systemctl restart chronyd就可以让下面的客户端同步了
|