Network Time Protocol, 如何在 server 与 client 同步他们的时间呢?
1) 首先, 主机当然要启动这个 daemon
2) 之后 client 会向 NTP server 校对时间的 message
3) 然后 NTP Server 会送出目前的标准时间给 Client
4)Client 接收了来自 Server 的时间后, 调整自己的时间, 达成了网络校时
ntp 这个 daemon 是以 port 123 为连接的端口 (使用 UDP 封包), 所以我们要利用 Time server 来进行时间的同步更新时, 就得要使用 NTP 软件提供的 ntpdate 来进行 port 123 的连接.
2
NTP 服务器的阶层概念
由于 NTP 时间服务器采用类似阶层架构 (stratum) 来处理时间的同步化, 所以他使用的是类似一般 server/client 的主从架构. 网络社会上面有提供一些主要与次要的时间服务器, 这些均属于第一阶及第二阶的时间服务器 (stratum-1, stratum-2)
Tips: 其实 NTP 的阶层概念与 DNS 很类似啦, 当你架设一部 NTP 主机, 这部 NTP 所向上要求同步化的那部主要主机为 stratum-1 时, 那么你的 NTP 就是 stratum-2 , 举例来说, 如果我们的 NTP 是向台湾的 tock.stdtime.gov.tw 这部 stratum-2 的主机要求时间同步化, 那我们的主机即为 stratum-3 , 如果还有其他的 NTP 主机向我们要求时间同步, 那么该部主机则会是 stratum-4 啦! 就这样啊~ 那最多可以有几个阶层? 最多可达 15 个阶层.
3
NTP 服务器的安装与设定
3.1 查看系统是否已经安装 NTP 服务组件
- rpm -qa | grep "ntp" #查看是否已经安装 ntp 组件, 有如下两个组件说明已经安装
- ntpdate-4.2.6p5-1.el6.CentOS.x86_64
- ntp-4.2.6p5-1.el6.CentOS.x86_64
3.2 yum 安装
yum -y install ntp1.3 ntp
3.3 配置文件
配置文件
/etc/ntp.conf
配置文件默认内容
- egrep -v "^$|#" ntp.conf #排除默认注释及空行
- driftfile /var/lib/ntp/drift
- restrict default kod nomodify notrap nopeer noquery #对默认的 client 拒绝所有操作
- restrict -6 default kod nomodify notrap nopeer noquery
- restrict 127.0.0.1 #允许本地地址的一切操作
- restrict -6 ::1
- 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
- includefile /etc/ntp/crypto/pw
- keys /etc/ntp/keys
修改配置
- cat /etc/ntp.conf
- driftfile /var/lib/ntp/drift
- restrict default kod nomodify notrap nopeer noquery #对默认的 client 拒绝所有操作
- restrict -6 default kod nomodify notrap nopeer noquery
- restrict 127.0.0.1 #允许本机的一切操作
- restrict 192.168.1.0 mask 255.255.255.0 nomodify #允许局域网内所有 client 连接到这台服务器
restrict -6 ::1 同步时间. 但是拒绝让他们修改服务器上的时间
- #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 127.127.1.0 #如果无法连接公网, 则使用自己本身的时间作为标准时间.
- fudge 127.127.1.0 stratum 10 #设置 stratum 级别
- includefile /etc/ntp/crypto/pw
- keys /etc/ntp/keys
3.4 restrict 安全相关参数
ignore: 关闭所有的 NTP 联机服务
nomodify: 客户端不能更改服务端的时间参数, 但是客户端可以通过服务端进行网络校时
notrust: 客户端除非通过认证, 否则该客户端来源将被视为不信任子网
noquery
: 不提供客户端的时间查询
4
Client 的安装和配置
客户端的安装同服务器, 如上.
4.1 配置文件
- driftfile /var/lib/ntp/drift
- restrict default kod nomodify notrap nopeer noquery
- restrict -6 default kod nomodify notrap nopeer noquery
- restrict 127.0.0.1
- restrict -6 ::1
- #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 ntpserver #<== 添加自己的时间服务器
- includefile /etc/ntp/crypto/pw
- keys /etc/ntp/keys
- 5
服务器端启动
在服务器端启动 NTP 服务, 命令如下:
/etc/init.d/ntpd start
添加开启启动:
- chkconfig ntpd on
- 6
NTP 的启动与观察
6.1 查看启动的端口
这样就表示我们的 NTP 服务器已经启动了, 不过要与上层 NTP 服务器联机则还需要一些时间, 通常启动 NTP 后约在 15 分钟内才会和上层 NTP 服务器顺利连接上. 那要如何确认我们的 NTP 服务器有顺利的更新自己的时间呢? 你可以使用底下几个指令来查阅喔 (请自行等待数分钟后再以下列指令查阅):
6.2 查看 NTP 服务器上层联机
这个指令可以列出我们的 NTP 服务器有跟上层联机否. 由上述的输出结果可以知道, 时间有校正约 258 * 10^(-3) 秒, 且每隔 512 秒会主动去更新时间喔!
6.3 列出目前我们的 NTP 与相关的上层 NTP 的状态
各个参数的含义:
事实上这个输出的结果告诉我们, 时间真的很准了啦! 因为差异都在 0.001 秒以内, 可以符合我们的一般使用了. 另外, 你也可以检查一下你的 BIOS 时间与 Linux 系统时间的差异, 就是 /var/lib/ntp/drift 这个档案的内容, 就能了解到咱们的 Linux 系统时间与 BIOS 硬件时钟到底差多久? 单位为 10^(-6) 秒啦!
7
注意
要让你的 NTP Server/Client 真的能运作, 在上述的动作中得注意:
上述的 ntpstat 以及 ntpq -p 的输出结果中, 你的 NTP 服务器真的要能够连结上层 NTP 才行! 否则你的客户端将无法对你的 NTP 服务器进行同步更新的! 重要重要!
你的 NTP 服务器时间不可与上层差异太多.
服务器防火墙在 UDP port 123 有没有开啊? 要特别注意的呢!
来源: http://www.tuicool.com/articles/eA7VBbJ