ubuntu配置ntp服务器

一个多台机器构成系统,有时候会有时间同步的需求,比如应用服务器和数据库服务器,如果两则不一致的话,有时就会产生问题,所以需要一个时间同步的机制来保证每台机器的时间一致。

Network Time Protocol(NTP,网络时间协议)用于同步它所有客户端时钟的服务。NTP服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使本地网络的所有客户端能同步时钟。

在ubuntu上只要sudo apt-get install ntp就可以安装好了,安装好之后有有一个ntpd的后台进程,因为ntp既是服务器又是客户端,如果只想要客户端功能可以把这个deamon停掉,使用crontab执行19 * * * * /usr/sbin/ntpdate ntp.api.bz 就可以了(ntp.api.bz是ntp服务器地址,如局域网中有ntp服务器,也可以用该服务器的ip)。

一般一个局域网中需要建一个ntp服务器,其他机器同步该机器就可以了,配置ntp服务器也比较简单,编辑/etc/ntp.conf文件,如下:


server ntp.api.bz
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
broadcast 172.16.120.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
disable auth
broadcastclient

上面加粗行就是文件中我所改动的,server是指定外部ntp server, broadcast是指对哪个局域网广播时间, 最后两行是指客户端不需要加密验证就可以同步改服务器。


需要注意的是ntp server重启之后需要比较长的一段时间客户端才能同步服务器,这段时间内客户端ntpdate会抛个no server suitable for synchronization found, 蛋疼

ps. 国内可用的NTP服务器地址列表 网络时间 服务器



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值