NTP时间同步搭建

一,简介

NTP(Network Time Protocol,网络时间协议)是用来在分布时间服务器和客户端之间时间同步。NTP基于UTP报文进行传输,使用的UDP端口为123.

使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的是时钟保持一致,从而使设备能够提供基于统一的多种应用。

对于时间错误会导致服务器宕机,所以运行NTP的本地系统,既可以接受来自其他时钟源的同步,又可以作为时钟同步其他的时钟,并且可以和其他设备互相同步。

二,搭建NTP时间同步服务(服务端)

1,搭建环境 centos6.5    2.6.32

server:192.168.43.249

client:192.168.43.102

2,安装NTP rpm -qa ntp 

      yum  install ntp -y

3,修改配置文件 

打开ntp服务配置文件,修改server地址,添加这几行。

vim /etc/ntp.conf

16 server 127.127.1.0 
 17 fudge 127.127.1.0 stratum 10
 19 # 允许ntpserver主动修改客户端时间
 21 restrict 192.168.0.0 nomodify notrap noquery

4,启动NTP检查端口并把NTP加入到开机启动

/etc/init.d/ntpd start

echo “/etc/init.d/ntpd start” >>/etc/rc.local

netstat  -an  |grep  123

5,防火墙关闭或防火墙允许NTP服务端口123

 -A INPUT -s 192.168.31.0/24 -j ACCEP

三,客户端安装(client)

1,安装NTP  rpm -qa ntp 

           yum  install ntp -y

2,手工执行或用crontab来执行

手工:ntpdate  ip

定时任务crontab  -e

 0  21  *  *  * ntpdate ip  >> /root/ntpdate.log 2>&1

3,关闭防火墙或开启

/etc/init.d/iptables  stop

-A  INPUT  -m  state  --state  NEW  -m  tcp  -p  tcp  --dport  123  -j ACCEPT

四,测试

client:192.168.43.102

[root@centos602 ~]# date -s "2017-11-11 2:00:00"
Sat Nov 11 02:00:00 CST 2017
[root@centos602 ~]# date +%F
2017-11-11

server:192.168.43.249

[root@centos6 ~]# date +%F:%T
2018-11-02:17:55:51

client:

[root@centos602 ~]# ntpdate 192.168.43.249
 2 Nov 17:57:08 ntpdate[2174]: step time server 192.168.43.249 offset 30814726.007401 sec
[root@centos602 ~]# date +%F
2018-11-02
[root@centos602 ~]# date +%F:%T
2018-11-02:17:57:35

五,配置文件相关说明

1.server 127.127.1.0  

#NTPD把本机主机的时钟也看作外部时钟源来处理,分配的地址是127.127.1.0

2.fudge  127.127.1.0  stratum  1

#设置本地时钟的层次为1,这样如果NTPD服务从本地时钟源获取时间的话,NTPD对外宣布的时间层次为2

3.restrict  default  nomodify

#允许任何IP的客户机都可以进行时间同步

4.restrict  192.168.0.0  mask  255.255.0.0  nomidify

#只允许192.168.0.0网段的客户机进行时间同步

5.让本服务器时间与time.nist.gov时间同步,使服务器为标准时间

ntpdate  time.nist.gov

六:的转载作者:suer0101
来源:CSDN
原文:https://blog.csdn.net/suer0101/article/details/7868813

# 1. 关于权限设定部分
#   权限的设定主要以 restrict 这个参数来设定,主要的语法为:
#   restrict IP mask netmask_IP parameter
#   其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0
#   至于 paramter 则有:
#   ignore :关闭所有的 NTP 联机服务
#   nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,
#   Client 端仍然可以透过 Server 端来进行网络校时。
#   notrust :该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
#   noquery :不提供 Client 端的时间查询
#   notrap :不提供trap这个远程事件登入
#  如果 paramter 完全没有设定,那就表示该 IP (或网域)“没有任何限制

原文地址:https://www.cnblogs.com/rui517hua20/p/9897692.html