yum install ntp //安装ntp
/etc/NTP.conf 配置文件
# Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict asia.pool.ntp.org restrict time.asia.apple.com restrict 133.100.11.8 restrict ntp.sjtu.edu.cn restrict pool.ntp.org restrict time.nist.gov restrict clock.cuhk.edu.hk restrict 127.0.0.1 restrict -6 ::1 restrict default nomodify # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.rhel.pool.ntp.org #server 1.rhel.pool.ntp.org #server 2.rhel.pool.ntp.org server asia.pool.ntp.org prefer server time.asia.apple.com server 133.100.11.8 server ntp.sjtu.edu.cn server pool.ntp.org server time.nist.gov server clock.cuhk.edu.hk #broadcast 192.168.1.255 key 42 # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 key 42 # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 key 42 # manycast client # Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 refid NIST # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()‘ing # it to the file. driftfile /var/lib/ntp/drift broadcastdelay 0.008 statsdir /var/log/ntp/ logfile /var/log/ntp/ntp.log # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8
防火墙添加例外:iptables -A INPUT -p udp --dport 123 -j ACCEPT //开启UDP 123 端口
管理命令:
ntpq -p #查看本机和上层服务器的时间同步结果 (watch ntpq -p 实时查看)
ntptrace #可以用来追踪某台时间服务器的时间对应关系
ntpdate IP #客户端要和NTP server进行时钟同步。
客户机测试: ntpdate 8.8.8.8 (ntpdate -d 8.8.8.8 进入调试模式)
错误1:ntpdate -u ip -> 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服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
处理:等待几分钟后,重试一般解决。
也可以使用命令 ntpq -p查看情况
客户机配置:
# crontab –e
# 30 3 * * * /usr/sbin/ntpdate -u 202.120.2.101 && hwclock -w //凌晨3点30同步时间并写入硬件
帮助链接:鸟哥
时间: 2024-12-22 23:02:03