第1章 内网NTP服务搭建
1.1 NTP简介
NTP
(网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC
,其精度在局域网内可达0.1ms
,在互联网上绝大多数的地方其精度可以达到1-50ms
NTP
服务器就是利用NTP
协议提供时间同步服务的NTP
服务器通信采用UDP
协议,端口是123
1.2 NTP服务安装
#系统自带ntp
[root@oldboyedu ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64
#如果没有就安装
yum -y install ntp
#启动ntp,并设置开机自启动
/etc/init.d/ntpd start
chkconfig ntpd on
192.168.89.11是服务端
192.168.89.10是客户端
1.3 NTP服务端主配置
[root@mysql ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default nomodify notrap nopeer noquery
restrict default nomodify
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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 127.127.1.0
fudge 127.127.1.0 stratum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
############################################################
#配置文件详解
[root@mysql ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default nomodify notrap nopeer noquery
#restrict 控制相关权限:
#ignore:关闭所有的NTP联机服务
#nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
#notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
#noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
#notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
#nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
restrict default nomodify
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
#确保localhost(这个常用的IP地址用来指linux服务器本身)有足够权限,使用没有任何限制关键词的语法
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#限制你允许的这些服务器的访问类型,在这个列子中的服务器是不容许修改运行时配置或查询您的linux NTP服务器,但是可以时间同步,详情请查看前面的(restrict 控制相关权限)
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#设定NTP主机来源,127.127.1.0 是指已本地的服务器为NTP服务器
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 127.127.1.0
fudge 127.127.1.0 stratum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
1.4 重启NTP服务
/etc/init.d/ntpd restart
1.5 检查时间服务器是否正确同步
[root@mysql ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+makaki.miuku.ne 218.186.3.36 2 u 47 64 1 79.040 4651437 18.347
+ntp8.flashdance 192.36.143.151 2 u 46 64 1 321.483 4651438 1.101
-119.79-161-57.c 129.242.4.241 2 u 44 64 1 342.352 4651439 1.897
*static-5-103-13 .GPS. 1 u 44 64 1 280.109 4651438 1.134
LOCAL(0) .LOCL. 10 l 53 64 1 0.000 0.000 0.000
1.6 客户端设置时间同步
#安装ntp
yum install ntp -y
#定时任务,同步时间服务器
crontab -l
#ntpdate
*/5 * * * * /usr/sbin/ntpdate 192.168.89.11
第2章 公网搭建NTP服务器
2.1 NTP服务安装
#系统自带ntp
[root@oldboyedu ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64
#如果没有就安装
yum -y install ntp
#启动ntp,并设置开机自启动
/etc/init.d/ntpd start
chkconfig ntpd on
192.168.89.11是服务端
192.168.89.10是客户端
2.2 配置NTP服务
[root@oldboyedu ~]# 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
2.3 启动NTP服务器
# 如果计划任务有时间同步,先注释,两种用法会冲突。
[root@oldboyedu ~]# crontab -e
# time sync by oldboy at 2010-2-1
#*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[root@oldboyedu ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]
[root@oldboyedu ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp1.aliyun.com 10.137.38.86 2 u 22 64 1 525.885 -42.367 0.000
[root@oldboyedu ~]# ntpstat
synchronised to NTP server (110.75.186.247) at stratum 3
time correct to within 4257 ms
polling server every 64 s
[root@oldboyedu ~]# ntpdate 10.0.0.9
7 Dec 18:43:07 ntpdate[26950]: the NTP socket is in use, exiting
2.4 客户端同步
#安装ntp
yum install ntp -y
#定时任务,同步时间服务器
[root@oldboyedu ~]# crontab -l
#ntpdate
* * * * * /usr/sbin/ntpdate 192.168.89.11