时间同步服务器

时间同步服务器

[toc]
因搭建 ceph 集群要求时间稳定.再次回顾下两种时间服务器的部署.推荐使用chrony部署

ntp

安装

yum install ntp -y

配置

修改 /etc/ntp.conf

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery

# 此处需要保留 ntpq -p 需要调用
restrict 127.0.0.1
restrict -6 ::1

# 配置允许访问的服务器,可以是网段和ip
restrict 192.168.1.0/24

# 配置上层 时间同步服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst

# local clock 如果上面的服务器都无法同步时间,就和本地系统时间同步。127.127.1.0在这里是一个IP地址,不是网段
server 127.127.1.0
fudge 127.127.1.0

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

启动

systemctl start ntpd
systemctl enable ntpd

netstat -ln |grep 123
udp        0      0 192.168.1.111:123       0.0.0.0:*                         
udp        0      0 127.0.0.1:123           0.0.0.0:*                         
udp        0      0 0.0.0.0:123             0.0.0.0:*                         
udp6       0      0 ::1:123                 :::*                               
udp6       0      0 :::123                  :::*  

测试

server 端输入 ntpq -p

ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 120.25.115.20   10.137.53.7      2 u    6   64    1   59.221    1.023   0.000
 203.107.6.88    .INIT.          16 u    -   64    0    0.000    0.000   0.000
*LOCAL(0)        .LOCL.           5 l    5   64    1    0.000    0.000   0.000

指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:

命令 说明
remote 本机和上层 ntp 的 ip 或主机名,“+”表示优先,“*” 表示次优先。
refid 参考的上一层NTP主机的地址
st stratum阶层
poll 下次更新在几秒之后
offset 时间补偿的结果

client 端测试

ntpdate 192.168.1.107
19 Feb 01:23:50 ntpdate[3116]: step time server 192.168.1.107 offset -2.524532 sec

可写入crontab 定时同步

* * * * * /usr/sbin/ntpdate server IP

chrony ( 推荐 )

安装

server 端和 client 端都需安装

yum install chrony -y

配置

server 修改 /etc/chrony.conf

# 配置上层 时间同步服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst

driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync

# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
allow 192.168.1.0/24

# 如果上面的服务器都无法同步时间,就和本地系统时间同步
local stratum 10

logdir /var/log/chrony

client 修改 /etc/chrony.conf

# 配置 server端 ip 为上层时间同步服务器
server 192.168.1.107 iburst

driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony

启动

server 端和 client 端都按如下启动

systemctl start chronyd.service
systemctl enable chronyd.service

测试

# 查看时间同步源
chronyc sources
210 Number of sources = 1
MS Name/IP address     Stratum Poll Reach LastRx Last sample               
=======================================================================
^* 192.168.1.111       10   6    37    36  +2086us[+7684us] +/-  122us

常用命令

# 查看时间同步源
chronyc sources -v

# 查看时间同步源状态:
chronyc sourcestats -v

# 设置硬件时间,硬件时间默认为UTC:
timedatectl set-local-rtc 1

# 启用NTP时间同步
timedatectl set-ntp yes

# 校准时间服务器:
chronyc tracking

你可能感兴趣的:(时间同步服务器)