使用虚拟机Linux系统搭建NTP服务器,时间同步失败,请求指导!

时间:2022-09-13 21:44:09
目前使用宿主机(WIN7系统)与虚拟机的NTP服务器同步时间,提示windows在与10.2.2.199进行同步时出错,无法连接到对等机。
请熟悉的达人帮忙找找是什么位置出的问题,非常感谢!
下面开始描述环境和配置。
宿主机ip:10.2.1.20/21,虚拟机ip:10.2.2.199/21(单网卡,桥接),网关10.2.1.1
相互可以ping通。

NTP版本:
[root@localhost ~]# rpm -qa | grep ntp
ntpdate-4.2.4p8-2.el6.centos.i686
fontpackages-filesystem-1.41-1.1.el6.noarch
ntp-4.2.4p8-2.el6.centos.i686

ntp.conf的配置 (虚拟机不能直接连接公网,修改的地方已标红)
[root@localhost ~]# cat /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 default nomodify
#restrict -6 default kod nomodify notrap nopeer noquery
#restrict 10.2.0.0 mask 255.255.0.0 nomodify
restrict default nomodify notrap noquery
restrict 10.2.0.0 mask 255.255.0.0 notrust nomodify notrap
 

# 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 pool.ntp.org
#server 127.0.0.1

#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

logfile /var/log/ntp/ntp.log

执行ntpstat结果
[root@localhost ~]# ntpstat
synchronised to local net at stratum 11 
   time correct to within 11 ms
   polling server every 1024 s

执行ntpq -p结果
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   25   64  377    0.000    0.000   0.000

暂时先贴这些,如果有需要我会追加详细配置情况,请各位达人不吝赐教。


7 个解决方案

#1


首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

#2


引用 1 楼 cxgsuda 的回复:
首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!

#3


引用 2 楼 Amerkor 的回复:
Quote: 引用 1 楼 cxgsuda 的回复:

首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!


哦,明白了! 不过楼主可以等到有linux client了再试,windows怎么配置ntp 我也不了解

#4


引用 2 楼 Amerkor 的回复:
Quote: 引用 1 楼 cxgsuda 的回复:

首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!


或者可以尝试下 把restrict 的条件放松点。

#5


经过上网查找相关资料和反复试验,终于把NTP server搭建成功了。
虽然cxgsuda同学没能解答我的问题,但仍然感谢您对我的问题的关注!

我把我的解决办法发出来,以方便后续其他朋友交流和学习。

首先说一下我配置文件/etc/ntp.conf中的问题
1、不应该设置notrust
restrict 10.2.0.0 mask 255.255.0.0 notrust nomodify notrap 
经过查阅之后,4.2版本之后的NTP rpm,在配置文件的这一句中应该把notrust去掉,否则会拒绝请求。

2、如果是不连接上层服务器的情况,必须配置以下信息
restrict 127.0.0.1
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10     #(stratum取值为3~10)

3、排除掉iptables的影响
执行service iptables stop,用以排除数据包被拒绝的问题。
由于是测试环境,所以直接将iptable关闭了,其他运行环境,请参考iptables的具体设置。

#6


引用 5 楼 Amerkor 的回复:
经过上网查找相关资料和反复试验,终于把NTP server搭建成功了。
虽然cxgsuda同学没能解答我的问题,但仍然感谢您对我的问题的关注!

我把我的解决办法发出来,以方便后续其他朋友交流和学习。

首先说一下我配置文件/etc/ntp.conf中的问题
1、不应该设置notrust
restrict 10.2.0.0 mask 255.255.0.0 notrust nomodify notrap 
经过查阅之后,4.2版本之后的NTP rpm,在配置文件的这一句中应该把notrust去掉,否则会拒绝请求。

2、如果是不连接上层服务器的情况,必须配置以下信息
restrict 127.0.0.1
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10     #(stratum取值为3~10)

3、排除掉iptables的影响
执行service iptables stop,用以排除数据包被拒绝的问题。
由于是测试环境,所以直接将iptable关闭了,其他运行环境,请参考iptables的具体设置。


也学习下!!!

#7



在执行ntpq -p以后会出现下面两行:
10.0.2.15       .INIT.          16 u    -   64    0    0.000    0.000   0.000
*LOCAL(0)        .LOCL.           8 l    3   64    1    0.000    0.000   0.000
在我的配置文件里有 server 10.0.2.15(即虚拟机的IP地址)
如果没有这行配置就不会出现第一行的显示。
这样能说明我服务器配置成功了么?
我用mac模拟客户端,进行ntpdate -u 10.0.2.15,总是提示no data错误

#1


首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

#2


引用 1 楼 cxgsuda 的回复:
首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!

#3


引用 2 楼 Amerkor 的回复:
Quote: 引用 1 楼 cxgsuda 的回复:

首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!


哦,明白了! 不过楼主可以等到有linux client了再试,windows怎么配置ntp 我也不了解

#4


引用 2 楼 Amerkor 的回复:
Quote: 引用 1 楼 cxgsuda 的回复:

首先 你的虚拟机要与宿主机同步时间的话,最重要的一条配置 没有配
server  10.2.1.20

配置后重启ntp服务,10分钟后查看ntpq -p 结果中 10.2.1.20有无*号

其次,有一点可能我也不太清楚,接触过的环境一般ntp server 和ntp client 都是linux
没有设置过 ntp server 是windows的情况,不知道windows通过什么提供ntp服务

非常抱歉,可能是我描述的不够清晰
我目前是希望在虚拟机上搭建NTP服务器,然后宿主机(win7)向虚拟机同步时间,也就是说,虚拟机是时间服务器(server),宿主机是client。

由于我所在的环境中不能连接公网,所以虚拟机作为NTP server时不能连接预设的上层时间服务器。
目前看似乎是虚拟机的NTP服务器没能搭建成功,不能正常提供服务。

我使用win7的宿主机也是为了测试一下虚拟机的NTP服务器是否搭建成功,后续需要推广到其他局域网内linux系统的client。

非常感谢您的回复,期盼您能够再次解答!


或者可以尝试下 把restrict 的条件放松点。

#5


经过上网查找相关资料和反复试验,终于把NTP server搭建成功了。
虽然cxgsuda同学没能解答我的问题,但仍然感谢您对我的问题的关注!

我把我的解决办法发出来,以方便后续其他朋友交流和学习。

首先说一下我配置文件/etc/ntp.conf中的问题
1、不应该设置notrust
restrict 10.2.0.0 mask 255.255.0.0 notrust nomodify notrap 
经过查阅之后,4.2版本之后的NTP rpm,在配置文件的这一句中应该把notrust去掉,否则会拒绝请求。

2、如果是不连接上层服务器的情况,必须配置以下信息
restrict 127.0.0.1
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10     #(stratum取值为3~10)

3、排除掉iptables的影响
执行service iptables stop,用以排除数据包被拒绝的问题。
由于是测试环境,所以直接将iptable关闭了,其他运行环境,请参考iptables的具体设置。

#6


引用 5 楼 Amerkor 的回复:
经过上网查找相关资料和反复试验,终于把NTP server搭建成功了。
虽然cxgsuda同学没能解答我的问题,但仍然感谢您对我的问题的关注!

我把我的解决办法发出来,以方便后续其他朋友交流和学习。

首先说一下我配置文件/etc/ntp.conf中的问题
1、不应该设置notrust
restrict 10.2.0.0 mask 255.255.0.0 notrust nomodify notrap 
经过查阅之后,4.2版本之后的NTP rpm,在配置文件的这一句中应该把notrust去掉,否则会拒绝请求。

2、如果是不连接上层服务器的情况,必须配置以下信息
restrict 127.0.0.1
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10     #(stratum取值为3~10)

3、排除掉iptables的影响
执行service iptables stop,用以排除数据包被拒绝的问题。
由于是测试环境,所以直接将iptable关闭了,其他运行环境,请参考iptables的具体设置。


也学习下!!!

#7



在执行ntpq -p以后会出现下面两行:
10.0.2.15       .INIT.          16 u    -   64    0    0.000    0.000   0.000
*LOCAL(0)        .LOCL.           8 l    3   64    1    0.000    0.000   0.000
在我的配置文件里有 server 10.0.2.15(即虚拟机的IP地址)
如果没有这行配置就不会出现第一行的显示。
这样能说明我服务器配置成功了么?
我用mac模拟客户端,进行ntpdate -u 10.0.2.15,总是提示no data错误