linux下ntp时间服务器安装配置

ntp服务器设置
浏览:144 |更新:2014-07-22 16:00
192.168.56.11 ntp服务器

192.168.56.200 ntp服务器上层同步ip

192.168.56.12 客户端

1:查看客户端和服务器端ntp rpm包是否安装

[root@11g ~]# rpm -qa | grep ntp

fontpackages-filesystem-1.41-1.1.el6.noarch

ntp-4.2.4p8-3.el6.x86_64

ntpdate-4.2.4p8-3.el6.x86_64

如果没有后面两个,需要安装,注意是客户端和服务器端都要查看

2:在需要做ntp服务器的主机 编辑 /etc/ntp.conf

[root@11g ~]# cp /etc/ntp.conf /etc/ntp.conf.bak 先拷贝一份做备份

[root@11g ~]# rm -rf /etc/ntp.conf 显出原始ntp配置文件

[root@11g ~]# vi /etc/ntp.conf 重新编辑一份ntp配置文件

driftfile /var/lib/ntp/drift

restrict 192.168.56.12

restrict 127.0.0.1

restrict -6 ::1

server 192.168.56.200

fudge 192.168.56.200 stratum 8

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

注释:

restrict 192.168.56.12 <---------客户端需要同步的ip,有多少个客户端,添加多

restrict 127.0.0.1 <---------允许本地同步,便于本地监控、配置

restrict -6 ::1

server 192.168.56.200 <---------需要同步的上层服务器

fudge 192.168.56.200 stratum 8 <-------同步上层服务器的stratum 大小不能超过或等于16

还有一种方案是:

[root@11g ~]# vi /etc/ntp.conf

driftfile /var/lib/ntp/drift

restrict default nomodify notrap <----- 允许所有ip,省去设置麻烦

restrict 127.0.0.1

restrict -6 ::1

server 192.168.56.200

fudge 192.168.56.200 stratum 8

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

3:启动ntp服务器

[root@11g ~]# /etc/init.d/ntpd start 启动

Starting ntpd: [ OK ]

[root@11g ~]# chkconfig ntpd on 设置开机自启动ntpd服务

[root@11g ~]# lsof -i:123 查看123端口

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

ntpd 2862 ntp 16u IPv4 22529 0t0 UDP *:ntp

ntpd 2862 ntp 17u IPv6 22530 0t0 UDP *:ntp

ntpd 2862 ntp 18u IPv6 22534 0t0 UDP localhost6.localdomain6:ntp

ntpd 2862 ntp 19u IPv6 22535 0t0 UDP [fe80::a00:27ff:fe8a:b4f0]:ntp

ntpd 2862 ntp 20u IPv6 22536 0t0 UDP [fe80::a00:27ff:feb9:c941]:ntp

ntpd 2862 ntp 21u IPv4 22537 0t0 UDP localhost.localdomain:ntp

ntpd 2862 ntp 22u IPv4 22538 0t0 UDP 11g:ntp

ntpd 2862 ntp 23u IPv4 22539 0t0 UDP 192.168.110.12:ntp <-----可以看出,设置成功

[root@11g ~]# ntpq -p 如果出现这样的情况,ntp服务器最后设置才算成功

remote refid st t when poll reach delay offset jitter

===================================================

===========================

*192.168.56.200 LOCAL(0) 11 u 9 64 377 1.362 2.822 0.449

以下是失败的设置:

[root@11g ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

192.168.56.200 .INIT. 16 u 41 64 0 0.000 0.000 0.000

4:防火墙设置

iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT 对外开放123端口

最好的办法是将所有linux系统的防火墙都关闭:

chkconfig --level 2345 ip6tables off

chkconfig --level 2345 iptables off

service ip6tables stop

service iptables stop

查看是否关闭成功:

[root@mysql ~]# chkconfig --list | grep ip

ip6tables 0:off1:off2:off3:off4:off5:off6:off

iptables 0:off1:off2:off3:off4:off5:off6:off

3:客户端设置

[root@mysql ~]# ntpdate 192.168.56.11 设置同步

22 Jul 06:53:14 ntpdate[5611]: no server suitable for synchronization found

[root@mysql ~]# ntpdate -d 192.168.56.11 查看原因

22 Jul 02:55:01 ntpdate[2096]: ntpdate 4.2.4p8@1.1612-o Thu Jan 10 15:17:41 UTC 2013 (1)

22 Jul 02:55:02 ntpdate[2096]: no server suitable for synchronization found

如果stratum 设置没有问题的话,第一次执行

[root@mysql ~]# ntpdate 192.168.56.11

22 Jul 06:53:14 ntpdate[5611]: no server suitable for synchronization found

需要再等上3-5分钟再次执行

[root@mysql ~]# ntpdate 192.168.56.11

22 Jul 06:54:45 ntpdate[5627]: adjust time server 192.168.56.11 offset -0.119003 sec <-----同步成功

4:客户端设置定时同步时间

[root@mysql ~]# vi /root/ntpupdate.sh 编辑定时脚本

/usr/sbin/ntpdate 192.168.56.11

[root@mysql ~]# cat /root/ntpupdate.sh 编辑完之后查看

/usr/sbin/ntpdate 192.168.56.11

[root@mysql ~]# crontab -e 编辑定时任务,每分钟执行一次

*/1 * * * * /root/ntpupdate.sh

[root@mysql ~]# crontab -l

*/1 * * * * /root/ntpupdate.sh 编辑后查看











方法二
1. /etc/ntp.conf
NTP服务的主要配置文件,不同的Linux版本文件所在的目录可能会不同。
2. /usr/share/zoneinfo
规定了各主要时区的时间设定文件。/usr/share/zoneinfo/Asia/Shanghai 为中国大陆地区的时区设定文件。
3. /etc/sysconfig/clock
时区设置文件
4. /etc/localtime
本地系统的时间设定文件
[root@linuxidc1 ~]# grep -v "^#" /etc/ntp.conf
restrict default kod nomodify notrap nopeer noquery//对于默认的client拒绝所有的操作
restrict -6 default kod nomodify notrap nopeer noquery //针对ipv6的设置
restrict 127.0.0.1 //允许本机地址一切的操作
restrict -6 ::1
server https://www.360docs.net/doc/1418254255.html, //设置上层服务器做同步ip地址
server https://www.360docs.net/doc/1418254255.html,
server https://www.360docs.net/doc/1418254255.html,
server 127.127.1.0 //loc

al地址
fudge 127.127.1.0 stratum 10 //这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
driftfile /var/lib/ntp/drift //记录上次的NTP server与上层NTP server联接所花费的时间
keys /etc/ntp/keys
参数:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询

3.具体配置

[root@linuxidc1 ~]# vim /etc/ntp.conf
3 restrict default kod nomodify notrap nopeer noquery
4 #restrict -6 default kod nomodify notrap nopeer noquery
5 restrict default nomodify
6 restrict 192.168.8.0 mask 255.255.255.0 nomodify
11 restrict 127.0.0.1
18 #server https://www.360docs.net/doc/1418254255.html,
19 #server https://www.360docs.net/doc/1418254255.html,
20 #server https://www.360docs.net/doc/1418254255.html,
21 server 192.168.8.70
31 server 127.127.1.0 # local clock
32 fudge 127.127.1.0 stratum 10


[root@linuxidc1 ~]# /etc/init.d/ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]

客户端测试
注:客户端的/etc/hosts里需要加上ntp服务器的ip地址和主机名

[root@linuxidc2 ~]# ntpdate 192.168.8.70
2 Mar 00:35:44 ntpdate[11181]: no server suitable for synchronization found
注:在ntp server上启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令进行同步时会产生 no server suitable for synchronization found的错误。
查看同步的过程
[root@linuxidc1 ~]# watch ntpq -p

Linux下的ntp服务器

注:LOCAL的这个就是与自身同步的ntp server。reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

客户端测试
[root@linuxidc2 ~]# ntpdate 192.168.8.70
2 Mar 00:40:51 ntpdate[11184]: adjust time server 192.168.8.70 offset -0.000031 sec
[root@linuxidc2 ~]# date
Sat Mar 2 01:00:13 CST 2013
服务器端
[root@linuxidc1 ~]# date
Sat Mar 2 01:00:15 CST 2013
[root@linuxidc1 ~]#


NTP客户端配置:

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


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

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

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

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


在成功配置好NTP服务端后,在NTP客户端执行ntpdate命令更新NTP客户端的时间时,如果报以下错误:
no server suitable for synchronization found
则可采

用以下步骤检测:
(1) 在NTP服务端执行以下命令检测NTP服务是否运行
#service ntpd status
(2) 运行ping命令检测NTP客户端与NTP服务端是否连通
#ping NTP服务端IP
(3) 在NTP客户端执行 ntpdate -d NTP服务端IP:
如果输出结果如下:
6 Nov 10:23:16 ntpdate[3521]: ntpdate 4.2.2p1@1.1570-o Tue Nov 18 07:40:49 UTC 2008 (1)
Looking for host 10.75.80.47 and service ntp
host found : 10.75.80.47
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
10.75.80.47: Server dropped: no data
……
……

6 Nov 10:23:20 ntpdate[3521]: no server suitable for synchronization found
如果出现以上情况,请按以下步骤处理:
a) 检查NTP服务端使用的ntp版本:
#ntpq -c version
如果输出版本是ntp4.2(包括4.2)之后的版本,则请检测是否在restrict的定义中使用了notrust。如果有则删除notrust,再进行NTP时间同步。
b) 检查NTP服务端的防火墙是否开放NTP服务端口:udp 123
#service iptables stop
执行以上命令关闭NTP服务端的防火墙,然后再进行NTP时间同步,如果成功,则需要修改iptables的设置,以开放NTP服务端口:
#setup #进入配置界面

在配置界面选择Firewall configuration进入防火墙配置界面:

在防火墙配置界面中选择Customize进入详细的配置界面:

按图中方式加入ntp:udp以运行NTP服务通过防火墙。
9) 如果输出结果如下:
5 Nov 19:02:27 ntpdate[28566]: ntpdate 4.2.2p1@1.1570-o Fri Sep 4 18:54:46 UTC 2009 (1)
Looking for host 10.75.80.55 and service ntp
host found : 10.75.80.55
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
10.75.80.55: Server dropped: strata too high
……
……

5 Nov 19:02:27 ntpdate[28566]: no server suitable for synchronization found
出现以上情况的原因是由于NTP Server还没有和自身或其他NTP Server保持同步,因此需在ntp的配置文件ntp.conf中加入以下语句以保证NTP Server与自身同步:
server 127.127.1.0
fudge 127.127.1.0 stratum 10

相关主题
相关文档
最新文档