NTP服务和 chrony 配置时钟源
1 ntp
NTP(Network Time Protocol)即网络时间协议,它是用来同步网络中各个计算机的时间的协议;时间的同步对计算机集群协同完成项目起关键作用。
查看时间
[root@fda ~]# date '+%F %T'
2019-01-13 13:29:56
同步时间
data -s "2009-08-08 9:57:00"
1.1 ntp时间同步实验
1.1.1 NTPserver端部署演示:
#第一步:下载ntp
╭─root@localhost.localdomain /var/www/html/wordpress
╰─➤ yum install ntp -y
#第二步:配置文件
╭─root@localhost.localdomain ~
╰─➤ vim /etc/ntp.conf
...
server 127.127.1.0 #本地时钟地址
server time1.aliyun.com #可以指定多个server,网络时钟地址,可加可不加
restrict 127.0.0.1 #restrict限制 指定那些人可以使用
restrict 192.168.80.4 mask 255.255.255.0
...
#第三步:重启ntp服务
╭─root@localhost.localdomain ~
╰─➤ systemctl restart ntpd
#第四步:查看ntp服务状态
╭─root@localhost.localdomain ~
╰─➤ ntpstat
synchronised to local net at stratum 6
time correct to within 3948 ms
polling server every 64 s
1.1.2 NTPclient端:
#第一步:下载同步时间的命令行工具
╭─root@localhost.localdomain ~
╰─➤ yum install ntpdate -y
#第二步:同步时间
╭─root@localhost.localdomain ~
╰─➤ ntpdate 192.168.80.3
24 May 18:50:43 ntpdate[10261]: adjust time server 192.168.80.3 offset -0.026284 sec
#第三步:编写计划任务
╭─root@localhost.localdomain ~
╰─➤ crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
╭─root@localhost.localdomain ~
╰─➤ crontab -l
*/5 * * * * /usr/sbin/ntpdate 192.168.80.3
1.2 同步网络时间
╭─root@localhost.localdomain ~
╰─➤ ntpdate time1.aliyun.com
24 May 06:56:21 ntpdate[4577]: the NTP socket is in use, exiting
2 chrony 时钟源
2.1 配置 chrony 配置时钟源实验
时钟源服务器 (e.g. 10.10.20.30 )
2.1.1 修改 chrony 配置文件
vi /etc/chrony.conf
2.1.2 修改或增加配置如下:
allow 10.10.20.30
allow 10.10.20.21
allow 10.10.20.22
allow 10.10.20.23
server 10.10.20.30 iburst
示例配置文件:
allow 10.10.20.30
allow 10.10.20.21
allow 10.10.20.22
allow 10.10.20.23
server 10.10.20.30 iburst
local stratum 10
# Ignore stratum in source selection.
stratumweight 0.05
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Enable kernel RTC synchronization.
rtcsync
# In first ten updates step the system clock instead of slew
# if the adjustment is larger than 1000 seconds.
makestep 5 10
# Allow NTP client access from local network.
#allow 192.168/16
# Listen for commands only on localhost.
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
# Disable logging of client accesses.
noclientlog
# Send a message to syslog if a clock adjustment is larger than 0.5 seconds.
logchange 0.5
logdir /var/log/chrony
#log measurements statistics tracking
2.1.3 重启 chronyd 服务
systemctl status chronyd.service
systemctl restart chronyd.service
2.1.4 修改所有其他服务器 chrony 配置文件
vi /etc/chrony.conf
#添加
server 10.10.20.30 iburst minpoll 4 maxpoll 10
2.1.5 重启chronyd服务
systemctl status chronyd.service
systemctl restart chronyd.service
2.1.6 在各节点查看时钟源配置
chronyc sources -v
#如看到有 ^* 开关的,则为实际⽣效的时钟源服务器
^* 10.10.20.30 ...
2.1.7 通过 clockdiff 查看时间差
clockdiff 10.10.20.30
#如看到时间差 delta 在 100ms 以内则时钟同步
delta=0ms/0ms
#如 clockdiff 访问不通,可尝试
strace clockdiff 10.10.20.30
2.2 相关命令
chronyc sources -v #查看时间同步的详细信息
clockdiff 10.10.20.30 #查看时间差
strace clockdiff 10.10.20.30 #查看时间差
vi /etc/chrony.conf #编辑配置文件
systemctl status chronyd.service #查看chronyd服务状态
systemctl restart chronyd.service #重启chronyd服务
2.2.1 chronyc sources -v 输出结果部分解读如下
M:Server的角色,"^"的含义,就是上级服务器
S:Server的状态,"*"的含义是以这个服务器的时间为准
"+"的含义是参考,"-"的含义是可参考
"?"的意思是未知,⼀般就是连不上服务器啦
2.2.2 举例
chronyc sources -v
clockdiff 10.10.20.30
strace clockdiff 10.10.20.30