Linux同步时间服务器

来源:互联网 发布:三级分销数据统计表 编辑:程序博客网 时间:2024/05/19 20:43

在集群中同步时间,可以使用ntpd服务器


安装服务器:

1. 安装ntpd服务,可通过一下命令安装

  1. yum install ntp -y

2. 修改配置如下:

  1. # For more information about this file, see the man pages
  2. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  3. driftfile /var/lib/ntp/drift
  4. # Permit time synchronization with our time source, but do not
  5. # permit the source to query or modify the service on this system.
  6. restrict default nomodify
  7. # Permit all access over the loopback interface. This could
  8. # be tightened as well, but to do so would effect some of
  9. # the administrative functions.
  10. restrict 127.0.0.1
  11. restrict ::1
  12. # Hosts on local network are less restricted.
  13. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  14. restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap
  15. # Use public servers from the pool.ntp.org project.
  16. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  17. # server 0.centos.pool.ntp.org iburst
  18. # server 1.centos.pool.ntp.org iburst
  19. # server 2.centos.pool.ntp.org iburst
  20. # server 3.centos.pool.ntp.org iburst
  21. server 127.127.1.0
  22. fudge 127.127.1.0 stratum 8
  23. #broadcast 192.168.1.255 autokey# broadcast server
  24. #broadcastclient# broadcast client
  25. #broadcast 224.0.1.1 autokey# multicast server
  26. #multicastclient 224.0.1.1# multicast client
  27. #manycastserver 239.255.254.254# manycast server
  28. #manycastclient 239.255.254.254 autokey # manycast client
  29. # Enable public key cryptography.
  30. #crypto
  31. includefile /etc/ntp/crypto/pw
  32. # Key file containing the keys and key identifiers used when operating
  33. # with symmetric key cryptography.
  34. keys /etc/ntp/keys
  35. # Specify the key identifiers which are trusted.
  36. #trustedkey 4 8 42
  37. # Specify the key identifier to use with the ntpdc utility.
  38. #requestkey 8
  39. # Specify the key identifier to use with the ntpq utility.
  40. #controlkey 8
  41. # Enable writing of statistics records.
  42. #statistics clockstats cryptostats loopstats peerstats
  43. # Disable the monitoring facility to prevent amplification attacks using ntpdc
  44. # monlist command when default restrict does not include the noquery flag. See
  45. # CVE-2013-5211 for more details.
  46. # Note: Monitoring will not be disabled with the limited restriction flag.
  47. disable monitor

3. 启动ntpd服务
  1. systemctl start ntpd
可通过`watch ntpq -p`查看是否启动正确

安装客户端:
1. 安装ntpdate客户端
  1. yum install ntpdate -y

2. 运行ntpdate server同步时间,server即为ntpd服务器地址或主机名

疑难问题:
错误1.Server dropped: Strata too high
解决办法:
ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
这是因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

  1. server 127.127.1.0
  2. fudge 127.127.1.0 stratum 8


在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

  1. # watch ntpq -p
显示内容中:

注意LOCAL的这个就是与自身同步的ntp server

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0175次的变更,每一次是poll的值的秒数,是64*5=320秒的时间。

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断


错误2.Server dropped: no data

出现这个问题的原因可能有2:

1. 检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:

  1. # ntpq -c version


下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd

解决:

把notrust去掉。


2. 检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。

可以用命令

  1. #service iptables stop


来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。


微信公众号:

0 0
原创粉丝点击