1、简介
NTP(Network Time Protocol)服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器与时间服务器的时间保持一致,应用于对时间实时性要求高的、必须统一时间事件。
2、时间同步服务端搭建
2.1、安装ntp服务
yum install -y ntp
2.2、修改配置文件
driftfile /var/lib/ntp/drift # 与上级时间服务器联系时所花费的时间,记录在driftfile文件内
restrict default kod nomodify notrap nopeer noquery #我 对于默认的客户端拒绝所有的操作
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1 # 允许本机地址一切的操作
restrict -6 ::1
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 允许局域网内所有客户端连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间
service 192.168.75.132 # 手动增加的,指明局域网中作为NTP服务器的IP
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# servers
server ntpupdate.tencentyun.com iburst #互联网上的ntp服务
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
2.3、常用操作命令
# 1、启动
systemctl start ntpd
# 2、停止
systemctl stop ntpd
# 3、查看状态
systemctl status ntpd
# 4、开机启停
systemctl enable/disable ntpd
# 5、检查时间服务器是否正确同步
ntpq -p
# remote:响应这个请求的NTP服务器的名称
# refid:NTP服务器使用的上一级ntp服务器
# when: 上一次成功请求之后到现在的秒数
# offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms),offset越接近于0,主机和ntp服务器的时间越接近
# jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小,主机的时间就越精确
# poll:本地机和远程服务器多少时间进行一次同步(单位为秒)
# reach:这是一个八进制值,用来测试能否和服务器连接,每成功连接一次它的值就会增加
# st:remote远程服务器的级别,由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16。为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的。值越小表示ntp serve的精准度越高。
# 6、
3、客户端时间同步命令
3.1、手动同步
# 1、使用ntp服务器同步
ntpdate 服务器ip/域名
# 2、手动更改
date -s 'yyyy-mm-dd hh:mm:ss'
注:客户端在进行时间同步时使用ntpdate命令,客户端服务器不要启动ntp服务。
3.2、自动同步
# 使用定时任务10分钟同步一次
crontab -e
# 增加如下内容
*/10 * * * * ntpdate ntp服务器ip/域名
4、总结
在工作中遇到两个集群服务器时间不同步,导致两边日志时间不一致,出现,使用采集重复问题,经过排查,将两个集群服务器时间进行同步的已解决,由此记录。
本人是一个从小白自学计算机技术,对运维、后端、各种中间件技术、大数据等有一定的学习心得,想获取自学总结资料(pdf版本)或者希望共同学习,关注微信公众号:it自学社团。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)