linux运维笔记:NTP时间同步

Linux运维:NTP服务器

Ago linux运维群:https://hankbook.cn|个人QQ:578843228


到新公司,检查一下时间是否同步。

1. NTP简介

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

NTP服务器就是利用NTP协议提供时间同步服务的。

2. NTP服务器安装

# 系统自带ntp
[root@aige ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64
# 如果没有就安装
yum -y install ntp

修改配置文件

[root@aige ~]# vim /etc/ntp.conf
# restrict default kod nomodify notrap nopeer noquery
restrict default nomodify
# nomodify客户端可以同步
# 将默认时间同步源注释改用可用源
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com
server time.nist.gov

直接用命令解决

sed -i 's#server 3.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 2.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 1.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#server 0.centos.pool.ntp.org iburst##g' /etc/ntp.conf
sed -i 's#restrict default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf
sed -i 's#restrict -6 default kod nomodify notrap nopeer noquery##g' /etc/ntp.conf
echo -e "server ntp1.aliyun.com\nserver time.nist.gov\nrestrict default nomodify" >> /etc/ntp.conf

对比

[root@m01 ~]# diff /etc/ntp.conf*
8,9c8
< #restrict default kod nomodify notrap nopeer noquery
< restrict default nomodify
---
> restrict default kod nomodify notrap nopeer noquery
23,28c22,25
< #server 0.centos.pool.ntp.org iburst
< #server 1.centos.pool.ntp.org iburst
< #server 2.centos.pool.ntp.org iburst
< #server 3.centos.pool.ntp.org iburst
< server ntp1.aliyun.com
< server time.nist.gov
---
> server 0.centos.pool.ntp.org iburst
> server 1.centos.pool.ntp.org iburst
> server 2.centos.pool.ntp.org iburst
> server 3.centos.pool.ntp.org iburst

启动并检查

[root@m01 ~]# /etc/init.d/ntpd start
Starting ntpd:                                             [  OK  ]
[root@m01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 time5.aliyun.co 10.137.38.86     2 u    7   64    1   10.942  -10.887   0.000
 nist1-lnk.binar .INIT.          16 u    -   64    0    0.000    0.000   0.000
 [root@m01 ~]# ntpstat
位置 标志 含义
remote之前 * 响应的NTP服务器和最精确的服务器
+ 响应这个查询请求的NTP服务器
blank(空格) 没有响应的NTP服务器
列表上方 remote 响应这个请求的NTP服务器的名称
refid NTP服务器使用的更高一级服务器的名称
st 正在响应请求的NTP服务器的级别
when 上一次成功请求之后到现在的秒数
poll 本地和远程服务器多少时间进行一次同步,单位秒,在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach 用来测试能否和服务器连接,是一个八进制值,每成功连接一次它的值就会增加
delay 从本地机发送同步要求到ntp服务器的往返时间
offset 主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter 统计了在特定个连续的连接数里offset的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确

客户机测试

[root@CentOS6 ~]# ntpdate 172.16.1.61
14 Apr 15:07:54 ntpdate[31792]: adjust time server 172.16.1.61 offset -0.001944 sec
restrict default nomodify命令说明:
表示设置时间服务器的权限设置:
权限的设定主要以 restrict 这个参数来设定,主要的语法为:
restrict IP地址 mask 子网掩码 参数
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
参数有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询
注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!

错误,待找

[root@m01 ~]# ntpstat 
unsynchronised
  time server re-starting
   polling server every 8 s

等待几分钟后,就OK了。

[root@m01 ~]# ntpstat 
synchronised to NTP server (182.92.12.11) at stratum 3 
   time correct to within 947 ms
   polling server every 64 s

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下

posted @ 2017-04-14 15:10  汉克书  阅读(1897)  评论(0编辑  收藏  举报