1.NTPD服务端

安装NTP软件:

  1. # yum install -y ntp

检查远程ntp服务器是否可用:

  1. # ntpdate -q ntp5.aliyun.com
  2. server 182.92.12.11, stratum 2, offset 189.609764, delay 0.02899
  3. 8 May 09:30:57 ntpdate[17695]: step time server 182.92.12.11 offset 189.609764 sec

配置ntp服务之前,先手动同步一下时间:

  1. # ntpdate ntp5.aliyun.com
  2. 8 May 09:35:37 ntpdate[17715]: step time server 182.92.12.11 offset 189.609636 sec

编辑NTP配置文件:(文件位置在/etc/ntp.conf,编辑前备份)

  1. # cp /etc/ntp.conf /etc/ntp.conf.ori
  1. # vim /etc/ntp.conf
  2. restrict 172.17.101.0 mask 255.255.255.0 nomodify notrap
  3. server ntp1.aliyun.com
  4. server ntp2.aliyun.com
  5. server ntp3.aliyun.com
  6. server ntp4.aliyun.com
  7. server ntp5.aliyun.com
  8. server ntp6.aliyun.com
  9. server ntp7.aliyun.com
  10. # UndisciplinedLocal Clock. This is a fake driver intended for backup
  11. # # and when nooutside source of synchronized time is available.
  12. # # 外部时间服务器不可用时,以本地时间作为时间服务
  13. server 127.127.1.0 # local clock
  14. fudge 127.127.1.0 stratum 10

把之前的参数注释掉:

  1. #server 0.centos.pool.ntp.org iburst
  2. #server 1.centos.pool.ntp.org iburst
  3. #server 2.centos.pool.ntp.org iburst
  4. #server 3.centos.pool.ntp.org iburst

启动ntpd

  1. 当前启动ntpd服务
  2. # /etc/init.d/ntpd start
  3. Starting ntpd: [ OK ]
  4. 下次开机自启ntpd服务
  5. # chkconfig ntpd on

检查服务状态:

  1. # ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. time5.aliyun.co 10.137.38.86 2 u 13 64 7 3.446 -0.276 0.078
  5. 120.25.115.19 10.137.53.7 2 u 13 64 7 39.774 0.001 0.123
  6. 203.107.6.88 10.137.55.181 2 u 9 64 7 13.564 -0.408 0.066
  7. *LOCAL(0) .LOCL. 10 l 13 64 7 0.000 0.000 0.000

如果服务器当前时间和远程ntp服务器时间相差很多或者服务器时间比远程ntp服务器时间要快的话,那么服务器是不会与远程ntp服务器同步时间的

需要在ntp.conf中加上一行参数:

  1. tinker panic 0

将同步的时间与硬件时钟进行同步:

  1. [root@LNMP ~]# vim /etc/sysconfig/ntpd
  2. 添加一行
  3. SYNC_HWCLOCK="yes"
  4. 保存退出,重启ntp服务

至此,NTP-server时间服务器配置完成!

2.配置内网NTP-client

首先需要安装NTPD服务,然后配置为自启动(与NTP-Server完全一样)。然后找其中一台配置/etc/ntp.conf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。

假若ntpd服务器172.17.101.22

  1. # vim /etc/ntp.conf
  2. server 172.17.101.22
  3. restrict 172.17.101.22 nomodify notrap noquery
  4. fudge 127.127.1.0 stratum 10

注释

  1. #restrict default kod nomodify notrap nopeer noquery
  2. #restrict -6 default kod nomodify notrap nopeer noquery
  3. #server 0.centos.pool.ntp.org iburst
  4. #server 1.centos.pool.ntp.org iburst
  5. #server 2.centos.pool.ntp.org iburst
  6. #server 3.centos.pool.ntp.org iburst

将同步的时间与硬件时钟进行同步:

  1. [root@LNMP ~]# vim /etc/sysconfig/ntpd
  2. 添加一行
  3. SYNC_HWCLOCK="yes"
  4. 保存退出,重启ntp服务

启动ntpd

  1. 当前启动ntpd服务
  2. # /etc/init.d/ntpd start [ OK ]
  3. Starting ntpd:

检查ntp服务状态

  1. # ntpq -p
  2. # ntpstat