当前位置首页 > CentOS知识

企业内部在centos7.2系统中必杀技NTP时间服务器及内网服务器时间同步(windows和linux客户端同步)

阅读次数:252 次  来源:admin  发布时间:

网络时间协议NTP(Network Time Protocol)是用于互联网中时间同步的标准互联网协议。NTP的用途是把计算机的时间同步到某些时间标准。目前采用的时间标准是世界协调时UTC(Universal Time Coordinated)。NTP的主要开发者是美国特拉华大学的David L. Mills教授。

NTP对于我们个人来说有什么用呢,简单的讲,当你的计算机时间不准确了,你可以接入到互联网,从网上同步一下时间。

对于企业来说,当你有成百上千的计算机,都不能直接连接互联网,时间不统一影响业务,如果一台一台的修改,工作量太大。这时搭建一个自己的NTP服务器就显出它的优势了。

企业内部架设NTP服务器的大概拓扑如下:

配置好相关的yum源、确保你的ntp服务器可以连网

# vim /etc/resolv.conf

#添加网关,这个可以ipconfig查看默认网关

ameserver 192.168.8.251

1、yum安装ntp服务

# yum install -y nt

配置开机启动

# chkconfig ntpd o

# chkconfig --list ntpd

tpd 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭

2、编辑ntp的配置文件

# vim /etc/ntp.conf 添加如下内容就可以了

# 允许内网网段 192.168.0.0 其他机器同步时间
restrict 192.168.0.0 mask 255.255.0.0 nomodify
 
# 中国这边最活跃的时间服务器 : http://www.pool.ntp.org/zone/cn
server ntp.api.bz perfer   
server 210.72.145.44 # 中国国家受时中心
server 202.112.10.36             # 1.cn.pool.ntp.org
server 59.124.196.83             # 0.asia.pool.ntp.org
# 允许上层时间服务器主动修改本机时间
restrict ntp.api.bz nomodify notrap noquery
restrict 210.72.145.44 nomodify notrap noquery
restrict 202.112.10.36 nomodify notrap noquery
restrict 59.124.196.83 nomodify notrap noquery


# 外部时间服务器不可用时,以本地时间作为时间服务
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

最终的参数:

# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'

或者

# cat /etc/ntp.conf | grep "^[^#]"

driftfile /var/lib/ntp/drift
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.0.0 mask 255.255.0.0 nomodify
server ntp.api.bz perfer
server 210.72.145.44             # 中国国家受时中心
server 202.112.10.36             # 1.cn.pool.ntp.org
server 59.124.196.83             # 0.asia.pool.ntp.org
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
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
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

国内常用的时间服务器列表:

210.72.145.44  (国家授时中心服务器IP地址)
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)
s1a.time.edu.cn北京邮电大学
s1b.time.edu.cn清华大学
s1c.time.edu.cn北京大学
s1d.time.edu.cn东南大学
s1e.time.edu.cn清华大学
s2a.time.edu.cn清华大学
s2b.time.edu.cn清华大学
s2c.time.edu.cn北京邮电大学
s2d.time.edu.cn西南地区网络中心
s2e.time.edu.cn西北地区网络中心
s2f.time.edu.cn东北地区网络中心
s2g.time.edu.cn华东南地区网络中心
s2h.time.edu.cn四川大学网络管理中心
s2j.time.edu.cn大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学

在配置前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步

# ntpdate -u ntp.api.bz

13 Apr 14:45:05 ntpdate[11464]: step time server 61.153.197.226 offset 28824.742403 sec

tp服务器配置好之后启动服务# service ntpd start,它监听的端口是UDP的123端口、可以使用

# netstat -tnulp | grep nt

udp 0 0 192.168.8.102:123 0.0.0.0:* 11484/ntpd

udp 0 0 127.0.0.1:123 0.0.0.0:* 11484/ntpd

udp 0 0 0.0.0.0:123 0.0.0.0:* 11484/ntpd

udp6 0 0 fe80::4637:e6ff:fe5:123 :::* 11484/ntpd

udp6 0 0 ::1:123 :::* 11484/ntpd

udp6 0 0 :::123 :::* 11484/ntpd

重启完了之后要等几分钟或者十几分钟后就可以实现时间同步了,

# ntpdate 192.168.8.100

11 Aug 11:38:30 ntpdate[7619]: adjust time server 10.17.1.60 offset 0.000178 sec

如果重启后马上测试就会出现下面的情况:

# ntpdate 192.168.8.100

11 Aug 11:05:28 ntpdate[7326]: no server suitable for synchronization found

可以用这个命令参数来查看:

# ntpdate -d 192.168.8.100

tpq -p 查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

# ntpq -

remote refid st t when poll reach delay offset jitter

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

*61.153.197.226 216.218.192.202 2 u 37 64 1 47.274 10.405 0.000

210.72.145.44 .INIT. 16 u - 64 0 0.000 0.000 0.000

gus.buptnet.edu 10.3.9.9 4 u 35 64 1 81.880 2.559 0.000

59-124-196-83.H .INIT. 16 u - 64 0 0.000 0.000 0.000

LOCAL(0) .LOCL. 10 l - 64 0 0.000 0.000 0.000

tp2.aliyun.com 10.137.38.86 2 u 22 64 1 35.176 4.490 1.640

tpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

刚启动的时候,一般是:

# ntpstat

unsynchronised

time server re-starting

olling server every 64

连接并同步后:

# ntpstat

ynchronised to NTP server (110.75.186.248) at stratum 3

time correct to within 225 m

olling server every 64

NTP的配置文件:

/etc/ntp.conf NTP服务的配置文件。

1)、权限的设定主要以 restrict 这个参数来设定,主要的语法为:
restrict IP地址 mask 子网掩码 参数
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
参数有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询 
2)、用server这个参数设定上级时间服务器,语法为:
server  IP地址或域名 [prefer]
IP地址或域名就是我们指定的上级时间服务器,如果 Server 参数最后加上 prefer,表示我们的 NTP 服务器主要以该部主机时间进行校准。
3)、解决NTP服务器校准时间时的传送延迟
使用driftfile参数设置:
driftfile 文件名 
在与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
注意:  driftfile 后面接的文件需要使用完整的路径文件名,不能是链接文件,并且文件的权限需要设定成 ntpd守护进程可以写入。

/usr/share/zoneinfo/ 这个目录下存放的是各个时区对应的设定文件。

/etc/localtime 本地系统时间设定文件。

/etc/sysconfig/clock 本机时区设定文件。

配置内网NTP-Clients linux-client

内网其他设备作为NTP的客户端配置,相对就比较简单,而且所有设备的配置都相同。

首先需要安装NTPD服务,然后配置为自启动(与NTP-Server完全一样)。然后找其中一台配置/etc/ntp.conf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。

# yum install -y nt

# chkconfig ntpd o

# vim /etc/ntp.conf

加入如下内容:

# 配置时间服务器为本地的时间服务器

erver 192.168.8.100

restrict 192.168.8.100 nomodify notrap noquery

erver 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

请求服务器前,请先使用ntpdate手动同步下时间

# ntpdate -u 192.168.8.100

13 Apr 14:49:51 ntpdate[4114]: no server suitable for synchronization found

这里有可能出现同步失败,一般情况下原因都是本地的NTPD服务器还没有正常启动起来,一般需要几分钟时间后才能开始同步。

错误判断请参考后面的错误处理。

# service ntpd restart

启动后,查看同步情况

# ntpq -

# ntpstat

.....

因为是内网,一般ntpstat很快就可以同步上,几分钟需要等下.

本机客户端配置完成后,使用SCP拷贝/etc/ntp.conf到其他需要同步的客户端机器,启动NTPD服务即可。

其他客户端机器上操作配置如下:

# ntpdate -u 192.168.8.100

# scp 192.168.8.xxx:/etc/ntp.conf /etc/ntp.conf

# service ntpd start

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

当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:

错误1.Server dropped: Strata too high

在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。

在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。

这是因为NTP server还没有和其自身或者它的server同步上。

以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

erver 127.127.1.0 fudge

127.127.1.0 stratum 8

在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

# watch ntpq -

出现画面:

Every 2.0s: ntpq -p Thu Jul 10 02:28:32 2008

remote refid st t when poll reach delay offset jitter

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

192.168.30.22 LOCAL(0) 8 u 22 64 1 2.113 179133. 0.001

LOCAL(0) LOCAL(0) 10 l 21 64 1 0.000 0.000 0.001

注意LOCAL的这个就是与自身同步的ntp server。

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

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。

错误2.Server dropped: no data

从客户端执行netdate –d时有错误信息如下:

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

192.168.30.22: Server dropped: no data

erver 192.168.30.22, port 123

.....

28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found

出现这个问题的原因可能有2:

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

使用以下命令检查ntp的版本:

# ntpq -c versio

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

The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd

解决:

把notrust去掉。

2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。

可以用命令

#service iptables sto

来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

centos7关闭防火墙方法;

# systemctl stop firewalld.service

禁止随机启动

# systemctl disable firewalld.service

windows-client配置

关于windows客户端与centos ntp服务器同步的配置:

1、 打开“开始”,点击运行输入gpedit.msc,然后“确定”

2、 进入组策略配置界面,单击“计算机配置”下的管理模板/系统/windows时间服务/双击“时间提供程序”

3、 双击“启动windows NTP 客户端”,然后选择”已启用”选项,点击“下一设置”

4、 将“设置”选项配置如下图:192.168.8.102,公司内部的centos7.2搭建的ntp时间服务器地址

5、 然后确定即可,双击桌面右下角的时间,然后选择“internet时间”选项

更新组策略:开始/运行/gpupdate /force

报错:windows在与192.168.8.102同步时出错

如果没有启用windows time,启动即可

如果已经启动,将服务重新启动

实际案例:

美国的服务器时间和国内不一致,交互的时候发生问题,现在将国内和国外的服务器时间都调整为一致

1.先查看时区,可以看到是东1区

# date -R

Thu, 25 May 2017 18:35:59 -0700

看一下时钟

# cat /etc/sysconfig/clock

ZONE="Asia/Shanghai"

查看系统默认的时区:

[root@u05mix06 Asia]# ll /etc/localtime

lrwxrwxrwx 1 root root 39 May 24 04:02 /etc/localtime -> /usr/share/zoneinfo/America/Los_Angele

2.修改时区

将Asia/shanghai-上海时区写入当前时区

# cp -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp: overwrite `/etc/localtime'? y

提示是否覆盖,输入Y回车,

然后#date

查看时区和时间(CST,中国时区)

# date -R

Fri, 26 May 2017 10:08:28 +0800

crontab -e

加入如下任务:

00 6 * * * /usr/sbin/ntpdate -u us.pool.ntp.org | logger -t NTP

表示每天6点进行同步一次也可以选择us.ntp.org.cn 这个美国的时间同步服务器

将时间写入硬件

hwclock -w

上一篇:在没有IIS的条件下利用HttpListener创建自己的Web服务器运行ASMX和asp.net
下一篇:利用IO多路复用,使用linux下的EpollSelector实现并发服务器