linux服务之NTP及chrony时间同步

linux服务之NTP及chrony时间同步

 

一、NTP时间同步

NTP(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步。NTP基于UDP报文进行传输,使用的UDP端口号为123。

目的:使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。

对于运行NTP的本地系统,既可以接受来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以和其他设备互相同步。

项目:NTP 时间服务器
centos两台以上
@1 192.168.99.101
@2 192.168.99.102

@1:
yum –y install ntp
同步本机时间:
date
2019年 02月 28日 星期五 11:28:02 CST
date -s 23:41
date
2019年 02月 28日 星期五 23:41:16 CST
ntpdate time.windows.com
28 Feb 11:29:49 ntpdate[9849]: step time server 52.173.193.166 offset -43916.758152 sec
date
2019年 02月 28日 星期五 11:30:30 CST

vim /etc/ntp.conf
restrict 192.168.99.0 mask 255.255.255.0 nomodify notrap #允许访问的网段
server 127.127.1.0 #添加本机作为服务器
fudge 127.127.1.0 stratum 10 #将优先级改为最高

或者

vim /etc/ntp.conf

server 127.127.1.0 #本地时钟地址,以本机作为时间服务器,也可以根据需要选择阿里时间服务器

restrict 127.0.0.1 #允许本机使用时间服务器

restrict 172.20.10.7 mask 255.255.255.240 #允许172.20.10.7使用本机的时间服务器

启动服务
systemctl start ntpd
systemctl enable ntpd

添加防火墙端口
netstat -anp|grep ntp #查询端口
firewall-cmd --add-port=123/udp
firewall-cmd --add-port=123/udp --permanent

查看ntp状态

ntpstat

@2使用ntp同步时间
date -s 00:00

ntpdate 192.168.99.101

在crontab 加入定时任务
crontab -e
0,30 * * * * ntpdate 192.168.99.101

扩展 :

hwclock/clock 显示硬件时钟
hwclock --systohc 系统时钟同步至硬件
hwclock --hctosys 硬件时钟同步至系统

二、chrony时间同步

yum -y install chrony

vim /etc/chrony.conf
###################
allow 192.168.10.0/24
###################
systemctl enable chronyd
systemctl restart chronyd
firewall-cmd --add-port=123/udp
firewall-cmd --add-port=123/udp --permanent
firewall-cmd --add-port=323/udp
firewall-cmd --add-port=323/udp --permanent

yum -y install chrony
echo "server 192.168.10.100 iburst" > /etc/chrony.conf
systemctl enable chronyd
systemctl restart chronyd
chronyc sources

原文地址:https://www.cnblogs.com/xuanbjut/p/12991586.html