解决linux上的时间同步问题:NTP
2018-01-29 15:10
567 查看
本篇文章主要讲的是自己在工作中遇到的问题及如何解决,对于什么是NTP和如何使用NTP这些概念都没有涉及,因为这些都是网上有的内容,我也是参考的这些内容。
在windows下自己下载了ntp-4.2.4p4.tar.gz压缩包,按照http://www.51testing.com/html/28/116228-202637.html这篇文章,在Ubuntu虚拟机中解压,编译和安装之后,但是并没有在/etc目录下生成ntp.conf配置文件,因此也就无从谈起来修改配置文件了。
于是按照这篇文章的方法:http://www.latelee.org/using-gnu-linux/ubuntu-ntp-note.html
服务器端的NTP:
在公司的生产环境下操作:
在想要作为服务器的机器上执行sudo
apt-get install ntp命令来安装ntp,安装完成后就发现在/etc/目录下生成了
ntp.conf配置文件,接着对于NTP服务器端的设置就按照这篇文章。
我的/etc/ntp.conf文件的内容为:
#
/etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile
/var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
statistics
loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org
server 127.127.1.0
fudge 127.127.1.0
c963
stratum 8
# Use Ubuntu's ntp server as a fallback.
server ntp.ubuntu.com
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.
# By default, exchange time with everybody, but don't allow configuration.
#modified by zhangjianjun
#restrict -4 default kod notrap nomodify nopeer noquery
restrict -4 default noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
NTP客户端:
将自己的Ubuntu虚拟机作为NTP的客户端,然后在虚拟机上解压,编译,安装ntp-4.2.4p4.tar.gz之后就可以使用
ntpdate
-d (10.0.xx.xx)(服务器端的ip地址),想要执行成功的话,客户端必须能够ping通服务器端的ip地址,
到此发现可以执行成功了。
linux@ubuntu:~$sudo ntpdate -d 10.0.24.25
29 Jan 14:33:10 ntpdate[6515]: ntpdate 4.2.6p3@1.2290-o Tue Jun 5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02652, dispersion 0.00002
transmitted 4, in filter 4
reference time: de193b90.11525693 Mon, Jan 29 2018 14:20:00.067
originate timestamp: de193e0f.43fffb69 Mon, Jan 29 2018 14:30:39.265
transmit timestamp: de193ebb.8f7b7817 Mon, Jan 29 2018 14:33:31.560
filter delay: 0.02652 0.02654 0.02654 0.02658
0.00000 0.00000 0.00000 0.00000
filter offset: -172.295 -172.295 -172.295 -172.295
0.000000 0.000000 0.000000 0.000000
delay 0.02652, dispersion 0.00002
offset -172.295461
29 Jan 14:33:33 ntpdate[6515]: step time server 10.0.24.25 offset -172.295461 sec
但是发现,客户端的时间根本没有发生变化,把客户端的时间故意调错,虽然执行成功的话,客户端的时间也没有矫正过来,
经过多次尝试终于发现:按照如下的方法执行的话,就没有问题了
先执行:sudontpdate -u 10.0.24.25
再执行:sudontpdate -d 10.0.24.25
执行完第二行命令后输出的打印信息有一些变化,见红色的部分
29 Jan 15:13:04 ntpdate[6581]: ntpdate 4.2.6p3@1.2290-o Tue Jun 5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02647, dispersion 0.00005
transmitted 4, in filter 4
reference time: de1947b9.084ab653 Mon, Jan 29 2018 15:11:53.032
originate timestamp: de194815.5e2b1c2a Mon, Jan 29 2018 15:13:25.367
transmit timestamp: de194815.5ddecfc4 Mon, Jan 29 2018 15:13:25.366
filter delay: 0.02657 0.02655 0.02649 0.02647
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000530 0.000550 0.000625 0.000664
0.000000 0.000000 0.000000 0.000000
delay 0.02647, dispersion 0.00005
offset 0.000664
29 Jan 15:13:27 ntpdate[6581]: adjust time server10.0.24.25 offset 0.000664 sec
通过对比发现只有输出打印信息为adjust
time server的时候才会生效。
具体为什么这么操作,我也不知道,这仅仅是无意间这么操作的时候发现确实客户端的时间跟客户端进行了同步
在项目中又遇到一个问题:每一台机器上跑的linux系统的显示的时区是不一样的,但是因此即使我们时间通过成功了,但是
在显示的时候时间也是不一样的,因此就需要在linux下进行时区的转换,我们先把每个linux系统上的时区都修改为相同的时区,然后
在进行时间同步即可。
参考链接:http://blog.51cto.com/13178102/2055701
到此本以为就改结束了,但是有一个问题,那就是我们的程序是运行在目标版上(mips架构)的,不是运行在x86架构上的。因此就不能使用
上述的方法来设置ntp server,因为不能再mips架构上的linux使用apt-get命令来安装ntp server程序,必须找到源码然后进行交叉编译才行。
因此必须使用源码来交叉编译,通过实验可知,使用之前下载的ntp-4.2.4p4.tar.gz(最好使用这个版本,因为我们实验过好几个版本,不是配置时出错,就是编译时出错,只有这个版本没有任何问题)就可以。
正如前面说的,使用解压,编译,安装的方法是得不到ntp.conf配置文件的,因此我就把之前使用sudo apt-get install ntp生成的配置文件保留了一份(如果使用apt-get remove来卸载已安装的软件时,会把所有的配置文件都给删除了,之前我把/etc目录下的ntp.conf拷贝了一份ntp.conf.bak,结果在卸载软件时还是被删除了),在这时拷贝的/etc目录下,
安装成功之后就会在指定的目录下的bin目录下生成一些可执行文件,比如ntpd, ntpdate.....,此时并不代表已经执行了ntp server服务,需要我们自己手动来启动ntp server
执行如下命令:
/usr/local/ntp/bin/ntpd -c /etc/ntp.conf
/usr/local/ntp/bin/ntpd表示ntpd的绝对路径,-c参数是指定配置文件,具体信息的参数信息可以使用-h参数来查看。
执行完执行使用ps -e | grep "ntpd" 就可以看到ntpd进行已经在后台运行了,注意,服务器启动后大约10分钟才可以连接,否则客户端同步时就会出现no server suitable for synchronization
found。此时客户端就可以进行同步时间了
仅参考这篇文章的服务器部分,链接:http://www.51testing.com/html/28/116228-202637.html
其实我们也可以不使用ntp客户端,也可以自己写一个客户端程序,然后进行编译,直接运行也可以与ntp server进行时间同步。
下载地址:http://download.csdn.net/download/zhangjianjun521/10233029
在windows下自己下载了ntp-4.2.4p4.tar.gz压缩包,按照http://www.51testing.com/html/28/116228-202637.html这篇文章,在Ubuntu虚拟机中解压,编译和安装之后,但是并没有在/etc目录下生成ntp.conf配置文件,因此也就无从谈起来修改配置文件了。
于是按照这篇文章的方法:http://www.latelee.org/using-gnu-linux/ubuntu-ntp-note.html
服务器端的NTP:
在公司的生产环境下操作:
在想要作为服务器的机器上执行sudo
apt-get install ntp命令来安装ntp,安装完成后就发现在/etc/目录下生成了
ntp.conf配置文件,接着对于NTP服务器端的设置就按照这篇文章。
我的/etc/ntp.conf文件的内容为:
#
/etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile
/var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
statistics
loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org
server 127.127.1.0
fudge 127.127.1.0
c963
stratum 8
# Use Ubuntu's ntp server as a fallback.
server ntp.ubuntu.com
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.
# By default, exchange time with everybody, but don't allow configuration.
#modified by zhangjianjun
#restrict -4 default kod notrap nomodify nopeer noquery
restrict -4 default noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
NTP客户端:
将自己的Ubuntu虚拟机作为NTP的客户端,然后在虚拟机上解压,编译,安装ntp-4.2.4p4.tar.gz之后就可以使用
ntpdate
-d (10.0.xx.xx)(服务器端的ip地址),想要执行成功的话,客户端必须能够ping通服务器端的ip地址,
到此发现可以执行成功了。
linux@ubuntu:~$sudo ntpdate -d 10.0.24.25
29 Jan 14:33:10 ntpdate[6515]: ntpdate 4.2.6p3@1.2290-o Tue Jun 5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02652, dispersion 0.00002
transmitted 4, in filter 4
reference time: de193b90.11525693 Mon, Jan 29 2018 14:20:00.067
originate timestamp: de193e0f.43fffb69 Mon, Jan 29 2018 14:30:39.265
transmit timestamp: de193ebb.8f7b7817 Mon, Jan 29 2018 14:33:31.560
filter delay: 0.02652 0.02654 0.02654 0.02658
0.00000 0.00000 0.00000 0.00000
filter offset: -172.295 -172.295 -172.295 -172.295
0.000000 0.000000 0.000000 0.000000
delay 0.02652, dispersion 0.00002
offset -172.295461
29 Jan 14:33:33 ntpdate[6515]: step time server 10.0.24.25 offset -172.295461 sec
但是发现,客户端的时间根本没有发生变化,把客户端的时间故意调错,虽然执行成功的话,客户端的时间也没有矫正过来,
经过多次尝试终于发现:按照如下的方法执行的话,就没有问题了
先执行:sudontpdate -u 10.0.24.25
再执行:sudontpdate -d 10.0.24.25
执行完第二行命令后输出的打印信息有一些变化,见红色的部分
29 Jan 15:13:04 ntpdate[6581]: ntpdate 4.2.6p3@1.2290-o Tue Jun 5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02647, dispersion 0.00005
transmitted 4, in filter 4
reference time: de1947b9.084ab653 Mon, Jan 29 2018 15:11:53.032
originate timestamp: de194815.5e2b1c2a Mon, Jan 29 2018 15:13:25.367
transmit timestamp: de194815.5ddecfc4 Mon, Jan 29 2018 15:13:25.366
filter delay: 0.02657 0.02655 0.02649 0.02647
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000530 0.000550 0.000625 0.000664
0.000000 0.000000 0.000000 0.000000
delay 0.02647, dispersion 0.00005
offset 0.000664
29 Jan 15:13:27 ntpdate[6581]: adjust time server10.0.24.25 offset 0.000664 sec
通过对比发现只有输出打印信息为adjust
time server的时候才会生效。
具体为什么这么操作,我也不知道,这仅仅是无意间这么操作的时候发现确实客户端的时间跟客户端进行了同步
在项目中又遇到一个问题:每一台机器上跑的linux系统的显示的时区是不一样的,但是因此即使我们时间通过成功了,但是
在显示的时候时间也是不一样的,因此就需要在linux下进行时区的转换,我们先把每个linux系统上的时区都修改为相同的时区,然后
在进行时间同步即可。
参考链接:http://blog.51cto.com/13178102/2055701
到此本以为就改结束了,但是有一个问题,那就是我们的程序是运行在目标版上(mips架构)的,不是运行在x86架构上的。因此就不能使用
上述的方法来设置ntp server,因为不能再mips架构上的linux使用apt-get命令来安装ntp server程序,必须找到源码然后进行交叉编译才行。
因此必须使用源码来交叉编译,通过实验可知,使用之前下载的ntp-4.2.4p4.tar.gz(最好使用这个版本,因为我们实验过好几个版本,不是配置时出错,就是编译时出错,只有这个版本没有任何问题)就可以。
正如前面说的,使用解压,编译,安装的方法是得不到ntp.conf配置文件的,因此我就把之前使用sudo apt-get install ntp生成的配置文件保留了一份(如果使用apt-get remove来卸载已安装的软件时,会把所有的配置文件都给删除了,之前我把/etc目录下的ntp.conf拷贝了一份ntp.conf.bak,结果在卸载软件时还是被删除了),在这时拷贝的/etc目录下,
安装成功之后就会在指定的目录下的bin目录下生成一些可执行文件,比如ntpd, ntpdate.....,此时并不代表已经执行了ntp server服务,需要我们自己手动来启动ntp server
执行如下命令:
/usr/local/ntp/bin/ntpd -c /etc/ntp.conf
/usr/local/ntp/bin/ntpd表示ntpd的绝对路径,-c参数是指定配置文件,具体信息的参数信息可以使用-h参数来查看。
执行完执行使用ps -e | grep "ntpd" 就可以看到ntpd进行已经在后台运行了,注意,服务器启动后大约10分钟才可以连接,否则客户端同步时就会出现no server suitable for synchronization
found。此时客户端就可以进行同步时间了
仅参考这篇文章的服务器部分,链接:http://www.51testing.com/html/28/116228-202637.html
其实我们也可以不使用ntp客户端,也可以自己写一个客户端程序,然后进行编译,直接运行也可以与ntp server进行时间同步。
下载地址:http://download.csdn.net/download/zhangjianjun521/10233029
相关文章推荐
- 快速有效的解决linux和windows时间同步的问题
- LInux下可以使用命令自动与网络的NTP服务器同步时间 CSDN验证码验证失败问题解决
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
- Linux和windows下时间不同步问题的解决办法
- 【工具用法】如何解决Windows10 和 deepin(Linux)双系统时间的同步问题
- 双系统下Windows与Linux时间无法同步问题解决
- LInux下可以使用命令自动与网络的NTP服务器同步时间 CSDN验证码验证失败问题解决
- 解决linux下用ntpdate同步时间
- 关于linux的ntpdate时间同步失败的解决办法
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
- ntp时间同步问题解决方法
- 解决 Linux 和 Windows 双启动带来的时间同步问题
- C# NTP 时钟同步,获取指定IP地址的系统时间及错误问题解决。
- 解决 Linux 和 Windows 双启动带来的时间同步问题 | Linux 中国
- 解决 Linux 和 Windows 双启动带来的时间同步问题
- NTP 时间同步流量使用过大 问题解决
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
- linux安装完毕时区时间问题ntp