业务需求:

企业中部署业务系统后,基本上需要让集群中的业务系统时间同步,这样就需要有一台或多台服务端做为ntp server作为其他服务器的时间同步服务器。让业务系统服务器同步到一台时间服务器上,这个服务器可以是公共的ntp服务器,也可以是本地局域网中部署的ntp服务器,为了避免所有的业务系统同步外网的ntp服务器而占用大量的网络带宽,最好的办法就是在本地的局域网中部署私有的ntp服务器,让所有的服务器同步到这台私有的ntp服务器即可。

部署私有ntp服务器

1、如果系统没有安装ntp工具,要先安装ntp

yum install ntp -y

2、配置ntp的配置文件

vim  /etc/ntp.conf 
    restrict default kod nomodify notrap nopeer noquery
    restrict -6 default kod nomodify notrap nopeer noquery
    restrict 114.80.81.1                            //没有任何何參數的話,這表示『该 IP或网段不受任何限制』
    restrict 127.0.0.1     //允许自身同步
    restrict -6 ::1
    restrict 192.168.1.0 mask 255.255.255.0 nomodify  //该网段可以进行校时
    server time5.aliyun.com prefer                  //prefer 该服务器优先
    server ntp1.aliyun.com 
    server 127.127.1.0        //如果公网NTP不可用时,将使用Local时间作为NTP服务提供给NTP Client

3、配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

#允许BIOS与系统时间同步,也可以通过hwclock -w 命令

[root@nagios-srv ~]# vim /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

4、启动ntp服务,并设置开机自启动

[root@nagios-srv ~]# /etc/init.d/ntp start
[root@nagios-srv ~]# chkconfig ntpd  on

5、查看ntp服务和上层ntp服务器连接状况

[root@node1-server ~]# ntpstat 
synchronised to NTP server (182.92.12.11) at stratum 3 
   time correct to within 972 ms
   polling server every 64 s
[root@node1-server ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u    3   64   37   29.584    1.452   0.263

6、ntp配置文件中权限说明:

权限设置:
restrict 控制相关权限。
语法为: restrict IP地址 mask 子网掩码 参数
其中IP地址也可以是default ,default 就是指所有的IP
    参数有以下几个:
    ignore  :关闭所有的 NTP 联机服务
    nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
    notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
    noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
    notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
    nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
    kod : 访问违规时发送 KoD 包。
    restrict -6 表示IPV6地址的权限设置。

7、客户端连接私有ntp  server

[root@lnmp01 ~]# crontab   -e
########connect ntpserver###############
*/5 * * * *  /usr/sbin/ntpdate 192.168.1.102 >/dev/null 2>&1