A-A+

搭建ntp时间服务器的方法教程

2015年12月29日 站长资讯 暂无评论

ntp服务的重要性本文不再赘述,有兴趣的可以百度或者鸟哥的书翻一下。这里跟大家分享一下如何配置ntp服务器

原理:让一部主要的Primary Server校对时间---->开放网络服务让Clients主机来连接 ----->Clients自己调整时间。

有关ntp服务的配置参数

ntp服务进程监听在UDP的123端口。

/usr/share/zoneinfo里面存放着世界各个地区的时间区域数据(timezone data),不要企图用文本编辑器直接打开!

/etc/sysconfig/clock Linux开机后会自动读取该文件中的数据来设定系统时间,如在中国,通常该项为

ZONE="Asia/Shanghai"

/usr/sbin/ntpd 服务端启动ntp的服务名称

/usr/sbin/ntpdate 客户端启动校对时间的命令,注意这里的路径/usr/sbin,通常ntpdate命令是需要写入crontab列表中,千万别写错路径,曾经有血淋淋的教训。

ntp服务器端主配置文件 /etc/ntp.conf (注意配置ntp服务器端的前提是你需要有一个上游时间服务器真正运行,这里笔者用的是阿里的时间服务器=110.75.186.249,IP地址: 110.75.186.249浙
江省杭州市 阿里巴巴)

/etc/ntp.conf配置文件主要定义了三段

1、server 定义了你上一层ntp服务器的地址

格式: server [IP OR HOSTNAME] [prefer]

2、restrict 管理和控制该ip地址的权限

格式: restrict [IP] mask [netmask_IP掩码][parameter]

参数:说常用的

nomodify:允许客户端同步服务器端的时间,不允许客户端修改服务器端时间。

3、driftfile 用来记录与server中的上一层服务器的差异时间

格式:driftfile /PATH/TO/FILE (使用绝对路径)

FILE档案记录的数值单位为百万分之一秒 ppm

这是我的ntp服务器的主配置文件参数

  1. driftfile /var/lib/ntp/drift  
  2. # Permit time synchronization with our time source, but do not   
  3. # permit the source to query or modify the service on this system.   
  4. restrict default kod nomodify notrap nopeer noquery   
  5. restrict -6 default kod nomodify notrap nopeer noquery  
  6. # Permit all access over the loopback interface.  This could   
  7. # be tightened as well, but to do so would effect some of   
  8. # the administrative functions.   
  9. restrict 127.0.0.1    
  10. restrict -6 ::1  
  11. # Hosts on local network are less restricted.   
  12. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap   
  13. restrict 192.168.1.0 mask 255.255.255.0 nomodify     # 我允许192.168.1.0网段内的客户端主机从我这里同步时间   
  14. # Use public servers from the pool.ntp.org project.    
  15. # Please consider joining the pool (http://www.pool.ntp.org/join.html).   
  16. #server 0.centos.pool.ntp.org iburst   
  17. #server 1.centos.pool.ntp.org iburst   
  18. #server 2.centos.pool.ntp.org iburst   
  19. #server 3.centos.pool.ntp.org iburst     
  20. server 110.75.186.249 prefer    #上层服务器我用的是110.75.186.249   

实验环境,一台CentOS6.5操作系统的主机当做ntp服务器提供服务,另外找两台主机当做客户端同步时间。

主ntp服务器 192.168.1.141

当前时间 Sat Dec 20 23:34:34 CST 2014

1、ntp服务器yum install ntp即可

2、修改配置文件定义了如上的参数

3、service ntpd start && chkconfig ntpd on 最好加入开机启动项中

4、netstat -tunlp | grep 123 是否有ntpd服务进程启动

客户端ntp服务器1 192.168.1.111

当前时间 Sat Dec 20 22:27:22 CST 2014 肯定与主服务器不同步

客户端ntp服务器2 192.168.1.115

当前时间 Sat Dec 20 22:34:32 CST 2014 肯定也与主服务器不同步

两个客户端使用ntpdate 命令同步主服务器看看是否有效果

第1台返回结果:20 Dec 23:38:58 ntpdate[2269]: step time server 192.168.1.141 offset 4102.953608 sec

第2台返回结果:20 Dec 23:39:00 ntpdate[2149]: step time server 192.168.1.141 offset 3798.445042 sec

经试验,两台时间均与服务器保持一致。至此ntp服务器建立完成!

P.S还可以在两台客户端节点上使用crontab命令来实施日常同步时间

*/3 * * * * /usr/sbin/ntpdate 192.168.1.141 &> /dev/null 定义每3分钟同步一次时间

标签:

给我留言