CentOS NTP服务器安装与配置

NTP简介:
NTP(Network Time Protocol)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正。本例讲解如何在CentOS6.3上配置NTP服务器和NTP客户端,可使多台客户机的时间与指定的NTP服务器的时间保持一致。从而保证了多台服务器的时间同步。

服务器环境
操作系统:CentOS 6.5 x86_x64
服务器ip:192.168.17.253

一、安装NTP服务器

yum install ntp

二、配置NTP
NTP配置文件路径:/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
restrict 210.72.145.44

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions. //限定了哪些主机可以从本NTP服务器同步时间
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 perfer   # 中国国家授时中心
server ntp.sjtu.edu.cn        #上海交通大学NTP服务器
server 202.112.10.36             # 1.cn.pool.ntp.org
server 59.124.196.83             # 0.asia.pool.ntp.org

#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服务默认走UPD协议,使用123端口,如果启动防火墙的话,需要配置一下防火墙。

vim /etc/sysconfig/iptables 

打开防火墙配置文件。加入如下配置项:

#open port for NTP server
-A  INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT

重启防火墙服务器:

service iptables restart

四、测试配置
NTP服务启动后大约需要3~5分钟的时间才会进行一次时间同步,在我配置的时候大约等了10分钟左右。我们可以通过命令ntpstat查看同步情况。

CentOS NTP服务器安装与配置

注:只有NTP服务器同步成功后,NTP客户端才可以同来同步时间。如果需要立刻从指定的时间服务器同步时间,可以使用"ntpdate"命令。使用"ntpdate"命令时,需要先关闭ntp服务:

 service ntpd stop

然后执行 "ntpdate NTPSERVERIP" 即可立即完成时间的同步。
五、相关命令
ntpdate //手动更新NTP服务器时间
ntpq -p //查询网络中的NTP服务器,同时显示客户端和每个服务器的关系
watch ntpq -p //监测ntpq -p命令

六、相关文献
NTP官方网站:http://ntp.org
鸟哥NTP配置:http://vbird.dic.ksu.edu.tw/linux_server/0440ntp_2.php

[box style="warning"]

感谢adamfei提醒,123端口容易被上传文件攻击。希望大家注意一下!

[/box]