Linux配置NTP服务器,时间同步

当服务器多了,时间准确与否,一致与否是个大问题。虽然这个问题总是被忽略,但是统一一致的时间是很有必要的。下面说一下在局域网内配置Linux时间服务器的方法。

配置的环境及要求:

假设在192.168.0.0网段内,要以IP为192.168.0.101的Linux机器时间服务器。

192.168.0.101服务器能上外网,能与比较权威的公网时间服务器同步

同网段内的其他机器每小时自动向192.168.0.101同步时间

 

1. 安装NTP 服务,并备份配置文件

 

[plain] view plain copy
 
  1. yum install ntp  
  2. chkconfig ntpd on  
  3. cd /etc/  
  4. mv ntp.conf ntp.conf.bak  



 

 

2. vi /etc/ntp.conf ,输入配置以下内容

 

 

[plain] view plain copy
 
  1. #设置此服务器同上层服务器做时间同步的IP地址,prefer意味着首选IP地址  
  2. #经试验,下面的几个时间服务器速度还不错  
  3. server 210.72.145.44 prefer  
  4. server 218.21.130.42  
  5. server 0.asia.pool.ntp.org  
  6. server 1.asia.pool.ntp.org  
  7. server 2.asia.pool.ntp.org  
  8.   
  9. #记录上次的NTP server与上层NTP server联接所花费的时间  
  10. driftfile /etc/ntp/drift  
  11.   
  12. #设置默认策略为允许任何主机进行时间同步  
  13. restrict default ignore  
  14.   
  15. #设置允许访问此时间服务器的时间服务的IP地址  
  16. #根据自己实际情况配置  
  17. restrict 127.0.0.1     
  18. restrict 192.168.10.2  # 指定某台机器时间同步  
  19. restrict 192.168.0.0 mask 255.255.255.0 #允许192.168.0.0/254子网内主机时间同步  
  20. restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap #允许任何主机跟进行时间同步  
  21.   
  22. #指定阶层编号为10,降低其优先度。  
  23. fudge 127.127.1.1 stratum 10  
  24.   
  25. #设置ntp日志的path  
  26. statsdir /var/log/ntp/  
  27.   
  28. #设置ntp日志文件  
  29. logfile /var/log/ntp/ntp.log  


3. NTP server的维护

 

 

[plain] view plain copy
 
  1. ----添加为服务  
  2. chkconfig --level 345 ntpd on  
  3. ----启动  
  4. service ntpd start  
  5. 或  
  6. /etc/rc.d/init.d/ntpd start  
  7. ----停止  
  8. service ntpd stop  
  9. 或  
  10. /etc/rc.d/init.d/ntpd stop  
  11. ----管理命令  
  12. ntpq –p     #查看本机和上层服务器的时间同步结果  
  13. ntptrace     #可以用來追踪某台时间服务器的时间对应关系  
  14. ntpdate IP   #客户端要和NTP server进行时钟同步。  
  15. /var/log/ntp/ntp.log   #查看ntp日志  


4. 配置客户端LINUX客户端: (局域网内对时间有要求的机器)

 

每小时同NTP server进行一次时钟同步,并写入本机BIOS

 

[plain] view plain copy
 
    1. echo "00 */1 * * * root /usr/sbin/ntpdate 192.168.0.240;/sbin/hwclock -w">>/etc/crontab  

-------------------------------------------------------------------------------------

1.4 配置集群间时间同步

1.4.1 配置集群中主时间服务节点

1)检查ntp服务器有没有安装

rpm –qa|grep ntp;

2)修改配置文件/etc/ntp.conf

修改三处

1.打开restrict 192.168.60.0 mask 255.255.255.0 nomodify notrap;

2.注释掉

#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

3.添加

server 127.127.1.0

fudge  127.127.1.0 stratum 10

3)修改配置文件/etc/sysconfig/ntpd

在第一行添加:SYNC_HWCLOCK=yes

4)启动ntp服务

1.检查有没有启动

service ntpd status;

2.启动

service ntpd start;

3.设置下次系统启动,ntpd自动启动

chkconfig ntpd on;

5)设置cdh1节点的时间

执行date -s 17:55:00,修改完后,记得执行clock -w,把系统时间写入CMOS。

1.4.2 配置集群中从时间服务节点

在cdh2、cdh3、cdh4集群,设置每十秒钟同步时间一次;

要在root用户下设置才有效。

crontab –e;

0-59/10 * * * * /usr/sbin/ntpdate cdh1;

手工同步时间

/usr/sbin/ntpdate cdh1;

 

 

 

posted on 2017-10-20 14:21  cxhfuujust  阅读(546)  评论(0编辑  收藏  举报

导航