Linux 时间同步服务

130 阅读4分钟

Linux 时间同步服务

# ntp一次性的同步

 #yum provides ntp (centos 7以下使用)

ntp-4.2.6p5-29.el7.centos.x86_64

#ntpdate time1.aliyun.com

#ntpdate ntp.aliyun.com

#ntpdate time.windows.com

#ntpdate ntp.aliyun.com

#ntpdate time.windows.com

#ntpdate time1.aliyun.com

#ntpdate -u  10.0.0.6     #-u可以越过防护墙与主机同步

#ntpdate   10.0.0.6

客户端同步检查

#客户端查询详细错误信息

ntpdate -d  10.0.0.6

查看ntp服务器与上层ntp的状态
#ntpq -p

    remote           refid     st t when poll reach   delay   offset jitter
==============================================================================
*10.0.0.6        202.112.29.82    3 u   16   64    1    0.424    1.956   1.771

查看ntp服务器有无和上层ntp连通
#ntpstat

如果防火墙没有开放UDP端口号123,有可能出现下面情况。
# /usr/sbin/ntpq -c rv | grep stratum

stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,

chkconfig --list ntpd
chkconfig --level 35 ntpd on

remote:响应这个请求的NTP服务器的名称。

refid:NTP服务器使用的上一级ntp服务器。

st :remote远程服务器的级别.由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16.为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.

when: 上一次成功请求之后到现在的秒数。

poll : 本地机和远程服务器多少时间进行一次同步(单位为秒).在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小

reach:这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加

delay:从本地机发送同步要求到ntp服务器的round trip time

offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近

jitter:这是一个用来做统计的值.它统计了在特定个连续的连接数里offset的分布情况.简单地说这个数值的绝对值越小,主机的时间就越精确

 

主机状态标识

      " "  无状态,表示: 没有远程通信的主机
"LOCAL" 即本机(未被使用的)高层级服务器 远程主机使用的这台机器作为同步服务器
“x” 已不再使用  
“-”   已不再使用
“#”  良好的远程节点或服务器但是未被使用 (不在按同步距离排序的前六个节点中,作为备用节点使用)
“+”  良好的且优先使用的远程节点或服务器(包含在组合算法中)
“*”  当前作为优先主同步对象的远程节点或服务器
“o”   PPS 节点 (当优先节点是有效时)。实际的系统同步是源于秒脉冲信号(pulse-per-second,PPS),可能通过PPS 时钟驱动或者通过内核接口。

 

阿里云NTP时间源服务器

ntp1.aliyun.com  
ntp2.aliyun.com  
ntp3.aliyun.com  
ntp4.aliyun.com  
ntp5.aliyun.com  
ntp6.aliyun.com  
ntp7.aliyun.com

国内一些大学NTP时间源服务器

s1a.time.edu.cn   北京邮电大学  
s1b.time.edu.cn   清华大学  
s1c.time.edu.cn   北京大学  
s1d.time.edu.cn   东南大学  
s1e.time.edu.cn   清华大学  
s2a.time.edu.cn   清华大学

 

#安装时间同步工具chrony

[root@centos8 ~]#yum -y install chrony
[root@centos8 ~]#systemctl start chronyd.service

#更改时间
[root@centos8 ~]#date -s '-1 year'
Sat Sep 7 18:59:31 CST 2019
[root@centos8 ~]#date
Sat Sep 7 18:55:35 CST 2019

#时间同步
[root@centos8 ~]#chronyc sources -vn
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? time.cloudflare.com 0 6 0 - +0ns[ +0ns] +/- 0ns
^? ntp7.flashdance.cx 2 6 173 11 -8784h[ -8784h] +/- 182ms
^? time.cloudflare.com 3 6 177 13 -8784h[ -8784h] +/- 62ms

^? time.cloudflare.com 3 6 177 18 -8784h[ -8784h] +/- 62ms

或 

#chronyc

chronyc> clients

chronyc> activity 

chronyc> sources -v  

^? 不可达不能同步
^+
^* 校正成功

**[root@centos8 ~]#date**
**Mon Sep 7 18:56:25 CST 2020**

时间工具

查看时区

# ll /etc/localtime  
lrwxrwxrwx. 1 root root 35 Jul 9 2021 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

#查看日期时间、时区及NTP状态: 
timedatectl 

#查看时区列表: 
timedatectl list-timezones 

#修改时区: 
timedatectl set-timezone Asia/Shanghai 

#修改日期时间: 
timedatectl set-time "2017-01-23 10:30:00" 

#开启NTP: 
timedatectl set-ntp true/false