Linux局域网中配置NTP服务器

Linux局域网中配置NTP服务器

一个局域网内有两台Linux,安装了CentOS5.6,想配置NTP,使两台电脑的时间保持同步。局域网网段配置为192.168.100.0 netmask 255.255.255.0

1. 配置NTP服务器,要求运行NTP服务的计算机能够访问Internet。如果不能联网,则不能保证时间的正确性,因此无法提供NTP时间同步服务。

2. 首先检查是否安装了组件: [root@testmechine ~]# rpm -qa ntp ntp-4.2.2p1-9.el5.centos.1

假如没有安装则yum install ntp来进行安装。

以下操作需要root权限。

3. 配置NTP Server

(1)NTP Server 必须已经联网。 (2)修改 /etc/ntp.conf

这个配置文件中有注释,具体配置参数请参考“鸟哥的私房菜-服务器架设篇”。我只修改了如下内容:

# Hosts on local network are less restricted.

restrict 192.168.100.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 133.100.11.8 prefer server 129.7.1.66 server pool.ntp.org server 202.120.2.101

修改说明:

restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap 因为我的局域网的IP地址段为“192.168.100.0 mask 255.255.255.0”,这个根据自己的网络配置进行修改。

server 133.100.11.8 prefer 这是从网上参考的,该NTP服务器来自日本 server 129.7.1.66,该NTP服务器来自美国 server pool.ntp.org 这是官方推荐

server 202.120.2.101 这是上海交大的NTP

(3)启动NTP服务 /etc/init.d/ntpd start

(4)如果需要,可以设定系统自动启动ntpd服务 chkconfig --add ntpd

chkconfig --level 345 ntpd on

(5)通常启动NTP服务5分钟后,计算机才能成功连接到外网的NTP服务器进行时间同步(具体原因我还不清楚)。在ntp server上使用命令:“ntpstat”查看结果,若结果类似于“synchronised to NTP server (129.7.1.66) at stratum 2 ”,则表示与NTP时间服务器同步成功,若提示“unsynchronised”,则表示不成功。

4. 配置NTP客户端

客户端的配置比较简单,假设局域网中的NTP服务器的IP地址为192.168.100.128,则运行命令: ntpdate 192.168.100.128

如果需要同时更新BIOS中的系统时间,则再运行命令: hwclock -w

如果需要定时运行时间同步命令,则需要使用crontab vi /etc/crontab #加入这一行

*/5 * * * * root ntpdate 192.168.100.128 && hwclock -w

这样,系统每隔5分钟,都会自动执行一遍时间同步,

另外,使用service crond status 查看cron deamon是否正常运行。

5. Trouble Shooting

(1) 解决ntp的错误 no server suitable for synchronization found

两台Linux,master上配置了ntp服务,slave上运行“ntpdate master”,提示错误“no server suitable for synchronization found”。用“ntpdate -d master”来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:

错误1. Server dropped: Strata too high

stratum显示为“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。这是因为NTP server还没有和其自身或者它的server同步上。在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?在ntp server上使用命令:“ntpstat”查看结果,若结果为“synchronised to NTP server (129.7.1.66) at stratum 2 ”,则表示与NTP时间服务器同步成功,若提示

“unsynchronised”,则表示不成功。

错误2. Server dropped: no data

从客户端执行“ntpdate -d master\时有错误信息如下:“transmit(192.168.30.22) 192.168.30.22: Server dropped: no data” 出现这个问题的原因可能有2:

(1) 检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。我自己配置NTP时就是遇到了这个问题。

可以在iptables中[/etc/sysconfig/iptables]添加如下规则: -A RH-Firewall-1-INPUT -p udp --dport 123 -j ACCEPT

(2) 检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。 使用以下命令检查ntp的版本:

# ntpq -c version

下面是来自ntp官方网站的说明:

The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant \

In 4.2 (and later) notrust means \authenticated.\This forces remote time servers to authenticate themselves to your (client) ntpd

解决方法:把notrust去掉。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4