centos7 ntp 服务器时间同步【ntp版本ntpq [email protected] Thu Aug 8 11:48:03 UTC 2019 (1)】

环境:centos7,
           时间服务器:192.168.66.102
           时间客户端:192.168.66.103   192.168.66.104

1.安装ntp
      yum install ntp
2.安装完毕之后,启动服务
      systemctl start ntpd
3.设置开机自启动
      systemctl enable ntpd
4.ntp服务端设置
  注释如下命令
 centos7 ntp 服务器时间同步【ntp版本ntpq [email protected] Thu Aug 8 11:48:03 UTC 2019 (1)】
   添加如下命令
   restrict 192.168.66.0 mask 255.255.255.0    #设置该机器支持192.168.66.0至192.168.66.255网段机器访问
   server 127.127.1.0 iburst                               #上游服务器为自身

   设置后,重启ntpd服务,用ntpstat来检查效果【大概几秒钟就可以看到效果】
    systemctl restart ntpd
    ntpstat
   centos7 ntp 服务器时间同步【ntp版本ntpq [email protected] Thu Aug 8 11:48:03 UTC 2019 (1)】
5.ntp客户端设置

#vim /etc/ntp.conf

#配置上游时间服务器为本地的ntpd Server服务器
server 192.168.66.102
##配置允许上游时间服务器主动修改本机的时间
restrict 192.168.66.102 nomodify notrap noquery

#以下四行注释掉
# 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
截图如下:
centos7 ntp 服务器时间同步【ntp版本ntpq [email protected] Thu Aug 8 11:48:03 UTC 2019 (1)】
6.编写时间同步脚本
   cd /usr/local/bin
   vim ntpsycstime

   systemctl stop ntpd        #停止ntpd服务
   ntpdate -u hadoop102    #同步时间服务器时间
   systemctl start ntpd        #启动ntpd服务
 
保存退出[:wq]

chmod 777 ntpsycstime   #赋权
7.配置任务调度
crontab -e

*/10 * * * * /usr/local/bin/ntpsycstime

8.另外的时间客户端机器重复【5-7】。