ntp服务器设置

来源:互联网 发布:python 对象转字符串 编辑:程序博客网 时间:2024/06/05 20:39

在openstack cloudstack hadoop中,都会有时钟同步的概念,绝大部分都是使用ntp来同步的.

下面记录一下ntp服务的安装和使用

1.安装

yum  install ntp

2.修改配置文件

  NTP服务器配置如下:

    编辑配置文件/etc/ntp.conf   

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1
restrict -6 ::1


restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

server 192.168.146.225
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org


server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

   配置文件说明如下:

    第一行restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器配置,notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(这里的查询是服务器本身状态查询)。

    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    这句是手动增加的,意思是从192.168.1.1-192.168.1.254的服务器都可以使用我们的NTP服务器来同步时间。

    server 192.168.146.225
    这句也是手动增加的,指明局域网中作为NTP服务器的IP;

  配置文件的最后两行作用是当服务器与公用的时间服务器失去联系时以本地时间为客户端提供时间服务。


   端口
  ntp使用udp协议,记得开放其123端口。


  启动NTPD

    为了使NTP服务可以在系统引导的时候自动启动,执行:
    #chkconfig ntpd on
    启动ntpd:
  service ntpd start


   NTP客户端配置:

    在客户端手动执行“ntpdate 服务器IP”来同步时间;


    另可以使用crond来定时同步时间:

    以root身份运行周期性任务:
  [root@supersun root]# crontab -e

  添加以下内容,每15分钟更新一下时间:
  15 * * * * ntpdate 服务器IP

    此处的ntpdate命令包含在ntp软件包中,记得确认系统中是否已安装。

这个是摘抄别人的,这个讲的不错,另外附上我自己修改好的配置文件

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.63.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 192.168.63.33
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


#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


server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10


3.重启服务

/etc/init.d/ntpd start

这样服务端就配置好了

4.客户端只要安装好ntp就行了,也就是第一步

然后使用ntpdate 192.168.63.33这个命令就会从33机器上获取时间,当然绝大部分人是将这个自动同步时间写入 crontab 

具体的操作是 crontab -e

内容是:15 * * * * ntpdate 192.168.63.33

0 0