NTP时间服务

NTP时间服务搭建

  • NTP时间服务:主要功能用于同步服务器之间的时间,因为有很多服务,如hadoop集群,结点之间的时间必须一致,否则服务会出错。

NTP时间服务安装

  • NTP安装比较简单,一般系统都可以直接安装,这里以CentOS为例
  • 安装命令:yum install ntp

NTP时间服务配置

  • 一般NTP服务分为服务端和客户端,程序上是没有区别的,只是配置上的区别。为什么要有服务端,主要是为了方便内网服务器进行时间同步,一个内网只需要配置一台时间服务器,用于同步网络时间,然后内网所有机器就可以同步这台服务器的时间,从而实现快速时间同步
    1. 服务端配置:这里直接给出示例,假设这台服务器的IP:192.168.11.11
# 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

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# ntp服务器配置
# 中国国家受时中心,并没有什么用
#server 210.72.145.44
# 第三方ntp时间服务器,比较稳定,如果大家有钱也可以给这个组织捐款,支持他们
server cn.ntp.org.cn

# allow update time by the upper server 
# 允许上层时间服务器主动修改本机时间
restrict cn.ntp.org.cn nomodify notrap noquery

# 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

#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
  1. 客户端配置:这里直接给出示例
# 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

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# ntp客户端配置
server 192.168.11.11

# 允许这台机器修改本机时间
restrict 192.168.11.11 nomodify notrap noquery

# 同步服务不存在时,使用本地时间
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

#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

NTP快速时间同步命令

  • 因为NTP服务启动后,需要很长的时间才会去同步,所以需要先使用命令来快速同步
  • 注意:有发现,在两台服务器之间,如果时间相差太大,时间同步服务会出现异常,所以最好先使用命令做一次时间同步。另外,如果同步地址不可用,也会出现时间无法同步的情况,所以使用同步命令还可以测试地址是否可用
  • 时间同步命令: ntpdate -u 192.168.11.11

NTP时间同步状态查询

  • 有时间需要知道当前同步的时间来自那个时间服务器,或配置了多个时间服务器时,那个服务器是最稳定的等,这时就需要使用命令来查询同步状态
  • 状态查询命令: ntpq -p
  • 注意: 状态的属性有点多,需要详细了解的,见http://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

NTP服务

  • 当服务配置完成,时间同步地址正常,就可以启动时间同步服务来定时同步服务器时间
  • 服务启动: systemctl start ntpd
    开机自启动:systemctl enable ntpd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值