天天看点

NTP时间服务器搭建及配置

NTP服务的安装

[root@m01 ~]# yum install ntp

[root@m01 ~]# rpm -qa ntp

ntp-4.2.6p5-5.el6.centos.x86_64

NTP 服务端配置

[root@m01 ~]# egrep -v "#|^$" /etc/ntp.conf

driftfile /var/lib/ntp/drift

restrict default nomodify

restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict -6 ::1

server ntp1.aliyun.com

server time.nist.gov

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

ntp.conf配置详解

driftfile /var/lib/ntp/drift #driftfile 参数解决NTP服务器校准时间时的传送延迟,格式:driftfile 文件名;在与上级时间服务器联系时所花费的时间记录在driftfile参数后面的文件内。注意:driftfile 后面接的文件必须是完整的全路径,不能是链接文件,并且文件权限要写成ntpd守护进程可写的状态

restrict 是控制权限参数,主要的语法格式为:restrict IP地址 mask 子网掩码 参数。其中IP 可以是ip 也可以是default(即:所有IP);

参数包含:

ignore:关闭所有的NTP联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校对

notrust:客户端除非通过认证,否则客户端来源将被认为不信任子网

noquery:不提供客户端的时间查询

注:如果参数么有设定,默认是没有任何限制

restrict -6 default kod nomodify notrap nopeer noquery#拒绝IPV6

restrict default kod nomodify notrap nopeer noquery #拒绝IPV4

restrict 127.0.0.1 #放行本机来源

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 放行网络来源

server ntp1.aliyun.com prefer 以这个为最优先级(上级)

server time.nist.gov 以这个为最上级备机(参考)

restrict 133.100.9.2       #开放server 访问我们ntp服务的权限

NTP服务的启动

     在启动前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步。

# ntpdate -u 202.112.10.36

22 Dec 16:52:38 ntpdate[6400]: adjust time server 202.112.10.36 offset 0.012135 sec

[root@m01 ~]# /etc/init.d/ntpd start

正在启动 ntpd:                                            [确定]

如果定时任务里面有时间同步任务时候要注释掉

[root@m01 ~]# chkconfig ntpd on

[root@m01 ~]# netstat -lntup|grep ntp

udp        0      0 172.16.1.61:123             0.0.0.0:*                               2312/ntpd          

udp        0      0 10.0.0.61:123               0.0.0.0:*                               2312/ntpd          

udp        0      0 127.0.0.1:123               0.0.0.0:*                               2312/ntpd          

udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2312/ntpd          

udp        0      0 fe80::20c:29ff:fed3:8d56:123 :::*                                    2312/ntpd          

udp        0      0 fe80::20c:29ff:fed3:8d4c:123 :::*                                    2312/ntpd          

udp        0      0 ::1:123                     :::*                                    2312/ntpd          

udp        0      0 :::123                      :::*                                    2312/ntpd  

[root@m01 ~]# tail -10 /var/log/messages

Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 5 lo ::1 UDP 123

Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 6 eth0 fe80::20c:29ff:fed3:8d4c UDP 123

Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 7 eth1 fe80::20c:29ff:fed3:8d56 UDP 123

Jun 26 19:49:35 m01 ntpd[2312]: Listening on routing socket on fd #24 for interface updates

Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c016 06 restart

Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM

Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c011 01 freq_not_set

Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c61c 0c clock_step +222384.777553 s

Jun 29 09:36:01 m01 ntpd[2312]: 0.0.0.0 c614 04 freq_mode

Jun 29 09:36:02 m01 ntpd[2312]: 0.0.0.0 c618 08 no_sys_peer

如何确定服务器顺利的更新了时间?使用下面的命令查看

[root@m01 ~]# ntpstat

unsynchronised

   polling server every 64 s

上面是么有成功的,可以重启ntp服务,如下:

[root@m01 ~]# /etc/init.d/ntpd restart

关闭 ntpd:                                                [确定]

synchronised to NTP server (182.92.12.11) at stratum 3

   time correct to within 7952 ms

这里代表顺利的更新了时间

[root@m01 ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*time5.aliyun.co 10.137.38.86     2 u   43   64    7    9.230    8.139   2.038

 host-24-56-178- .ACTS.           1 u  105   64    2  333.570  -34.886   0.000

上面的ntpq -p列出了与NTP服务相关的上联ntp服务的状态,其中每行的意思:

remote:NTP的的主机ip或是主机名,其中*代表正在使用当中的上联NTP;+代表也能上联,作为下个提供时间更新的候选

refid:参考的上一级NTP服务器地址

st: remote远程服务器的级别,由于NTP是层级结构,有顶端的服务器,也有多层的Relay Server再到客户端,所以服务器从高到低分为1-16个级别,为了减缓负荷和网络堵塞,原则上应该直接连到级别为1的服务器上

when :上次成功请求后,到现在的秒数

poll: 本地和远程服务器多少时间进行一次时间同步。刚开始的运行ntp的时候找个poll值会比较小,那样和服务器的同步频率就增加了,可以尽快调整的正确的时间范围,之后poll的值会逐渐增大,同步的频率也会相对减小

reach : 这是一个八进制值,用来测试能和服务器连接,每成功一次它的值就会增加

delay : 从本地发送同步要求到ntp服务器的时间,也就是网络传输的延迟时间,单位是10^(-3)秒

offset : 主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近

jitter : 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小,主机的时间就越精确

NTP服务的安全配置:

虽然已经在ntp.conf中使用restrict参数配置了限制,但是最好在IPtables中再次限制

iptables -A INPUT -i $EXTIF -p udp -s 192.168.100.0/24 --dport 123 -j ACCEPT

NTP客户的时间校准

1、手动的方式进行校准

使用date hwclock-r;hwclock -w这三个命令来查看时间并且写入bioos中

2、网络校对的方式

ntpdate 10.0.0.61

使用hwclock -w 写入到bios中

添加crontab定时任务

通过在客户端启动ntp服务器进程,在ntp.conf配置文件中配置

restrict 10.0.0.61 授权来自61服务器

server 10.0.0.61 这个就是ntp服务器地址

本文转自 kesungang 51CTO博客,原文链接:http://blog.51cto.com/sgk2011/1793889,如需转载请自行联系原作者

继续阅读