CentOS 6.3 搭建 NTP 服务器

NTP简介:

NTP(Network Time Protocol)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正。

下面讲解如何在CentOS6.3上配置NTP服务器和NTP客户端,可使多台客户机的时间与指定的NTP服务器的时间保持一致。从而保证了多台服务器的时间同步。

配置环境简介:

本例共用3台服务器做测试,1台做为NTP服务器,两台作为NTP的客户端。两台客户端的时间与NTP服务的时间保持同步。
   NTP服务器的地址:172.16.11.14
   客户端1的地址:172.16.11.13
   客户端2的地址:172.16.11.15

NTP服务器配置(172.16.11.14):

ntp:NTP服务器的主要软件

CentOS默认 会自动安装NTP服务。
如果不确定是否安装了ntp软件包可以用命令

#rpm –qa|grep ntp

查询一下。
如果没有安装,可以用命令

#yum install -y ntp

安装一下,也可以从centos安装盘安装。
配置文件在 /etc 目录下,打开ntp.conf文件。

#vim /etc/ntp.conf

找到如下内容:

1.restrict 192.168.0.1/24 mask 255.255.255.0 nomodify 

配置是否为其他PC提供时间服务,限定了哪些主机可以从本NTP服务器同步时间,默认的配置文件里是没有这句话的。加入这句话后,表明,只有192.168.0 这个网段的主机可以从本NTP服务器同步时间。
nomodify  表明客户端不可以修改服务器的地址

2.server time-b.nist.gov

配置时间源,远程时间服务器的地址。默认的配置文件里是没有的,加上这句话后。表示NTP服务器从这个时间服务器上同步标准时间。
CentOS在此处的配置项是 server 0.centos.pool.ntp.org    server 1.centos.pool.ntp.org server 2.centos.pool.ntp.org 测试中发现这几个时间服务器都不能正确的同步,所以我把它们给注释掉了。

3.server  127.127.1.0     # local clock 
  fudge   127.127.1.0 stratum 10

默认的配置文件里这两个是被注释掉的。如果第二部配置的server time-b.nist.gov 无效时,则NTP服务器会根据这里的配置,把自己的时间做为NTP服务器的时间,即和自己同步。考虑到有的局域网里不可以访问外网,所有这里需要把这个配置项用上,即把前面的注释符#号去掉就可以了。

NTF服务器的配置到这里已经结束了。保存并退出后,启动NTP服务,没有错误的话,NTP服务器就可以正常工作了。

配置开机时自动运行时间服务的命令:

#chkconfig ntpd on

启动或停止时间服务的命令:

#service ntpd start|stop|status|restart|try-restart|force-reload

验证ntp服务已经运行:

#pgrep ntpd


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分钟的时间才会进行一次时间同步。可以通过命令ntpstat查看同步情况,只用NTP服务器同步成功后,NTP客户端才可以同来同步时间。如果需要立刻从指定的时间服务器同步时间,可以使用ntpdate 命令。使用ntpdate 命令时,需要先关闭ntp服务

#service ntpd stop

然后执行:

#ntpdate  pool.ntp.org && hwclock -w

即可立即完成时间的同步。

Linux操作系统当中其实有两个时间,分别是:

软件时钟:Linux自己的系统时间,由1970/01/01开始记录的时间参数
硬件时钟:计算机系统在BIOS记录的实际时间,这也是硬件所记录的

在软件时钟方面,我们可以透过date这个指令来进行手动修改,但如果要修改BIOS记录的时间,就得要使用hwclock这个指令来写入才行:hwclock -r|-w

当我们进行完Linux时间的校时后,还需要以hwclock来更新BIOS的时间,因为每次重新启动的时候,系统会重新由BIOS将时间读出来,所以BIOS才是重要的时间依据啊

确认同步成功

# ntpq -p

同时修改一下 /etc/sysconfig/ntpd 文件

# vi /etc/sysconfig/ntpd

SYNC_HWCLOCK=yes

# 改成 yes ,这样主机 BIOS 的时间也会跟着一起改变的。


NTP客户端配置(172.16.11.13,172.16.11.15)

ntpdate:NTP客户端的主要软件

如果不确定是否安装了ntpdate软件包可以用命令

#rpm –qa|grep ntpdate

查询一下。
如果没有安装,可以用命令

#yum install -y ntpdate

安装一下,也可以从centos安装盘安装。
配置文件在 /etc 目录下,打开ntp.conf文件。

1.打开NTP配置文件

#vim /etc/ntp.conf

客户端的配置比服务器的配置要简单一下,只需加入 server 172.16.11.14 即可,表明本机的时间服务器是172.16.11.14,即从14上同步时间。

同时修改一下 /etc/sysconfig/ntpdate 文件

# vi /etc/sysconfig/ntpdate

SYNC_HWCLOCK=yes

# 把 no 改成 yes ,这样主机 BIOS 的时间也会跟着一起改变的。

2. 保存并退出NTP配置文件,启动NTP服务。

  配置开机时自动运行时间服务的命令:

  #chkconfig ntpdate on

  启动或停止时间服务的命令:

  #service ntpdate start|stop|status|restart|force-reload

  验证ntp服务已经运行:

  #pgrep ntpdate

3. 防火墙的配置参见上面14的配置。

相关命令:

1.查看系统时间:  date
2.设置系统时间:  date  mmddhhmmyyyy.ss   //月日时分年.秒
3.查看与上层NTP服务器的关系:ntpq –p
4.查看是否更新了自己的时间: ntpstat





配置文件:

1.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

# 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).
# server 0.centos.pool.ntp.org
# server 1.centos.pool.ntp.org
# server 2.centos.pool.ntp.org
server time-b.nist.gov

#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

# 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

# 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.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

# 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).
# server 0.centos.pool.ntp.org
# server 1.centos.pool.ntp.org
# server 2.centos.pool.ntp.org
server 172.16.11.14

#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

# 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

# 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




猜你喜欢

转载自maosheng.iteye.com/blog/2228200