在linux中,有些服务必须依靠准确的时间,才可以在运行的时候不出差错,例如DNS,LVS,HTTPS等,都须要后台的服务器之间保持时间的同步。而Centos系统中自带的有安装对应的同步时间的服务。centos7中默认安装的是chrony,而Centos6相比在时间服务有所不一样,使用了ntp服务来同步时间,而在Centos7上则使用的是chrony服务来同步时间,相较与ntp服务。chrony服务有以下几点优点:linux
1,更快的同步只须要数分钟而非数小时时间,从而最大程度减小了时间和频率偏差,对于并不是全天 24 小时运行 的虚拟计算机而言很是有用 2,可以更好地响应时钟频率的快速变化,对于具有不稳定时钟的虚拟机或致使时钟频率发生变化的节能技术而言 很是有用 3,在初始同步后,它不会中止时钟,以防对须要系统时间保持单调的应用程序形成影响 4,在应对临时非对称延迟时(例如,在大规模下载形成连接饱和时)提供了更好的稳定性 5,无需对服务器进行按期轮询,所以具有间歇性网络链接的系统仍然能够快速同步时钟
323/udp,123/udp centos
/etc/chrony.conf服务器
yum -y install chrony #系统默认已经安装,如未安装,请执行如下命令安装
systemctl enable chronyd.service systemctl restart chronyd.service systemctl status chronyd.service
systemctl stop firewalld systemctl disable firewalld setenforce 0 #临时关闭SElinux
cat /etc/chrony.conf #下方是默认的四个同步时间的服务器,不过该服务器同步时间略慢,能够按照格式修改为本地的时间服务器 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 ...... # 指定一台主机、子网,或者网络以容许或拒绝NTP链接到扮演时钟服务器的机器 #allow 192.168.0.0/16 #deny 192.168/16 ...... # 指定日志文件的目录 logdir /var/log/chrony ......
timedatectl #查看系统的当前的时区 Local time: 日 2019-05-26 10:42:00 CST Universal time: 日 2019-05-26 02:42:00 UTC RTC time: 日 2019-05-26 02:42:00 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a
若是当前时区不是亚洲/上海时区,能够用以下命令设置markdown
timedatectl set-timezone Asia/Shanghai
服务器端: 注释掉默认的时间服务器 #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 并添加如下内容:(内网的时间同步器或互联网中的时间同步器) server 172.22.0.1 iburst 配置完成后重启服务,这样咱们需求的一台内网时间服务器已经配置完毕。
客户端: 一样注释掉其余server,并在客户端(192.168.1.10)添加刚配置的时间服务器的IP地址: server 192.168.1.9 iburst #将这一行取消注释,当没法从时间服务器中得到正确的时间时,可让本服务器充当网络内的时间服务器 # Allow NTP client access from local network. allow 192.168.0.0/24 到此已经完成系统时间的同步。若有多台机器,操做亦是如此
查看时间同步源: chronyc sources -v 查看时间同步源状态: chronyc sourcestats -v 设置硬件时间 硬件时间默认为UTC: timedatectl set-local-rtc 1 启用NTP时间同步: timedatectl set-ntp yes 校准时间服务器: chronyc tracking