ntp-实现时间同步

Posted 猫二哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ntp-实现时间同步相关的知识,希望对你有一定的参考价值。

一背景介绍

已经安装好crontab和ntp,系统centos6.5,一般不是最小化安装都会有哈。集群一定要搭建时间同步机制,不然运行久了,job运行会非常慢。

二搭建内网NTP服务器,通过此服务器进行时间同步

1修改配置文件

vim /etc/ntp.conf

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# 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 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

#restrict 是允许单个IP地址访问本ntp服务器,或者允许网段中的服务器访问本ntp服务器进行时间同步
restrict 192.168.247.* mask 255.255.255.0
#允许192.168.247网段的ip可以访问本ntp服务器

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#增加server 210.72.145.44 表示指定本ntp服务器的上游ntp服务器为210.72.145.44,并且设置为首先服务器。同步时间为:从上到下,写的越靠上,优先级越高。(此服务器同步不了时间,寻找下一个ntp服务器)。此IP地址是中国国家授时中心ntp服务器。
#增加server 133.100.11.8     #当上面服务器同步不了,则寻找第二个。此IP地址是日本福冈大学ntp服务器。
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
server 192.168.247.132         
#local clock 如果上面的服务器都无法同步时间,就和本地系统时间同步。192.168.247.132 在这里是一个IP地址,不是网段。

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# 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

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

2开启ntp服务

/etc/init.d/ntpd start

使用如下命令,查看是否启动,123是ntp服务的端口哈
netstat -ln | grep 123

3客户端同步时间

这个命令,一般用户没有权限哦,只有root有,可以授权也可以用root,所以创建crontab的时候使用root用户。
ntpdate master

这个不是一个错误,一般ntp服务启动的时候,需要几分钟才能建立联系,所以耐心等待几分钟再试试。

4最后一步,使用crontab

定时同步时间,零点1分同步,且创建crontab的时候使用root用户,如果给其他用户赋予了ntpdate的运行权限也可以使用其他用户创建
crontab -e
01 00 * * * ntpdate mater

以上是关于ntp-实现时间同步的主要内容,如果未能解决你的问题,请参考以下文章

NTP服务时间同步服务NTP

Windows server 2012 NTP配置,实现成员服务器及客户端时间与域控制器时间同步

时间同步的网络授时

同步时间,为什么我选 Chrony 而不是 NTP ?

NTP时间同步服务器和北斗时间同步服务器以及GPS时间服务器有什么区别?

实现集群-NTP时间同步