Linux下NTP服务搭建

检查是否安装NTP

[root@linux ~] # rpm -q ntp

ntp-4.2.2p1-7.el5

安装NTP

[root@linux ~] #yum –y install ntp

[root@linux ~] #rpm -ivh ntp.xxx.xxx.rpm

安装完成以后编辑配置文件/etc/ntp.conf

[root@linux ~] #vi /etc/ntp.conf

restrict default kod nomodify  # 用restrict控管权限
notrap nopeer noquery 
restrict -6 default kod nomodify notrap nopeer noquery # nomodify - 用户端不能更改ntp服务器的时间参数

# notrap - 不提供trap远端登陆
restrict 127.0.0.1
restrict -6 ::1

restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap #允许192.168.x.x网段的IP连接NTP服务器
restrict cn.pool.ntp.org  #增加NTP上层服务器
restrict 164.67.62.194  #增加NTP上层服务器
server cn.pool.ntp.org prefer # 用server设定上层ntp服务器,“prefer”表示优先
server 164.67.62.194
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift  #系统时间与bios事件的偏差记录
keys /etc/ntp/keys
logfile /var/log/ntp.log  #日志
broadcastdelay 0.008

保存退出

:qw
在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间咯。(因为启动服务器,端口会被服务端占用,就不能手动同步时间了)

[root@linux ~] # ntpdate cn.pool.ntp.org
25 Apr 14:33:51 ntpdate[8310]: step time server 80.85.129.2 offset 6.655976 sec

运行NTP

[root@linux ~] #service ntpd start

[root@linux ~] #chkconfig ntpd on

查看运行端口

[root@linux ~]# netstat -ln|grep 123
udp        0      0 192.168.228.153:123         0.0.0.0:*   
udp        0      0 127.0.0.1:123               0.0.0.0:*            
udp        0      0 0.0.0.0:123                 0.0.0.0:*            
udp        0      0 :::123                      :::* 

查看ntp服务器有无和上层连通
[root@linux ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
注意:此命令出现上述synchronised结果比较慢。

查看ntp服务器与上层间的联系
[root@linux ~]# ntptrace -n 127.0.0.1
127.0.0.1: stratum 2, offset 0.064524, synch distance 0.121167
210.72.145.44: timed out, nothing received***Request timed out

客户端设置

命令同步如下
[root@linux ~]# ntpdate 192.168.228.153
hwclock -w把系统时间写入硬件BIOS时间中,也可以用计划任务做定时更新:
[root@linux ~]# crontab -e
0 0 * * * /usr/sbin/ntpdate 192.168.228.153;/sbin/hwclock -w;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d7eee16509371c5ce1a743f0a0fed994.html