linux时间同步,ntpd、ntpdate

在Windwos中,系统时间的设置很简单,界面操作,通俗易懂。而且设置后,重启,关机都没关系。系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系统会自动在Bios里面取硬件时间,以保证时间的不间断。

    但在Linux下,默认情况下,系统时间和硬件时间,并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行,是靠Bios电池来维持,而系统时间,是用CPU tick来维持的。

    在系统开机的时候,会自动从Bios中取得硬件时间,设置为系统时间。

 

.Linux系统时间的设置

    在Linux中设置系统时间,可以用date命令:

//查看时间

[root@localhost ~]# date
2008年 12月 12日星期五 14:44:12 CST

//修改时间
[root@localhost ~]# date -set  "2013-12-24 00:01" <== (年/月/日 时:分【:秒】)
2009年 01月 01日星期四 00:01:00 CST

//date 有几种时间格式可接受,这样也可以设置时间:

[root@localhost ~]# date 012501012009.30  <== 月日时分年.秒
2009年 01月 25日星期日 01:01:30 CST

 

 .Linux硬件时间的设置

    硬件时间的设置,可以用hwclock或者clock命令。其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

//查看硬件时间可以是用hwclock,hwclock --show 或者hwclock -r

[root@localhost ~]# hwclock --show
2008年12月12日星期五 06时52分07秒  -0.376932 seconds

//设置硬件时间

[root@localhost ~]# hwclock --set --date="1/25/09 00:00" <== 月/日/年时:分:秒
[root@localhost ~]# hwclock
2009年01月25日星期日 00时00分06秒  -0.870868 seconds

[root@localhost ~]# hwclock  -w   根据系统时间设置硬件时间

 

.系统时间和硬件时间的同步

    同步系统时间和硬件时间,可以使用hwclock命令。

//以系统时间为基准,修改硬件时间

[root@localhost ~]# hwclock --systohc<== sys(系统时间)to(写到)hc(Hard Clock)
[root@localhost ~]# hwclock -w

//以硬件时间为基准,修改系统时间

[root@localhost ~]# hwclock --hctosys
[root@localhost ~]# hwclock -s

 

.不同机器之间的时间同步(重点)

    为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。Linux系统下,一般使用ntp服务器来同步不同机器的时间。一台机器,可以同时是ntp服务器和ntp客户机。在网络中,推荐使用像DNS服务器一样分层的时间服务器来同步时间。

    同步时间,可以使用ntpdate命令,也可以使用ntpd服务(debian中ntp服务器为ntp服务,需要安装apt-get install ntp,同时对上手动同步时间需要用到ntpdate,故还需安装apt-get install  ntpdate)。

    使用ntpdate比较简单。格式如下:

[root@linux ~]# ntpdate [-nv] [NTP IP/hostname]
[root@linux ~]# ntpdate 192.168.0.2
[root@linux ~]# ntpdate time.ntp.org

    但这样的同步,只是强制性的将系统时间设置为ntp服务器时间。如果cpu tick有问题,只是治标不治本。所以,一般配合cron命令,来进行定期同步设置。比如,在crontab中添加:

0 12 * * * * /usr/sbin/ntpdate 192.168.0.1

     这样,会在每天的12点整,同步一次时间。ntp服务器为192.168.0.1。

    使用ntpd服务,要好于ntpdate加cron的组合。因为,ntpdate同步时间,会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响。比如sleep,timer等。而且,ntpd服务可以在修正时间的同时,修正cpu tick。理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

    要注意的是,ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行. 所以新设置的时间服务器一定要先ntpdate从上源取得时间初值, 然后启动ntpd服务。ntpd服务运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程.

 

.ntpd服务的设置

    ntpd服务的相关设置文件如下:

1./etc/ntp.conf:这个是NTP daemon的主要设文件,也是 NTP 唯一的设定文件。

2./usr /share/zoneinfo/:在这个目录下的文件其实是规定了各主要时区的时间设定文件,例如北京地区的时区设定文件在/usr/share/zoneinfo/Asia/Beijing 就是了。这个目录里面的文件与底下要谈的两个文件(clock 与localtime)是有关系的。

3./etc/sysconfig/clock:这个文件其实也不包含在NTP 的 daemon 当中,因为这个是linux的主要时区设定文件。每次开机后,Linux 会自动的读取这个文件来设定自己系统所默认要显示的时间。

4./etc /localtime:这个文件就是“本地端的时间配置文件”。刚刚那个clock 文件里面规定了使用的时间设置文件(ZONE) 为/usr/share/zoneinfo/Asia/Beijing ,所以说,这就是本地端的时间了,此时, Linux系统就会将Beijing那个文件另存为一份/etc/localtime文件,所以未来我们的时间显示就会以Beijing那个时间设定文件为准。

5. /etc/timezone:系统时区文件

 

    下面重点说说 /etc/ntp.conf文件的设置。在 NTP Server 的设定上面,其实最好不要对 Internet 无限制的开放,尽量仅提供您自己内部的 Client 端联机进行网络校时就好。此外, NTP Server 总也是需要网络上面较为准确的主机来自行更新自己的时间啊,所以在我们的 NTP Server 上面也要找一部最靠近自己的 Time Server 来进行自我校正。事实上, NTP 这个服务也是Server/Client 的一种模式。

[root@linux ~]# vi /etc/ntp.conf
# 1. 关于权限设定部分
#  权限的设定主要以 restrict 这个参数来设定,主要的语法为:
#   restrict IP mask netmask_IP parameter
#   其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0
#  至于paramter则有:
#   ignore :关闭所有的 NTP 联机服务
#   nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,

#   Client 端仍然可以透过 Server 端来进行网络校时。
#   notrust:该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
#   noquery:不提供 Client 端的时间查询

#   notrap:不提供trap这个远程事件登入

#  如果paramter完全没有设定,那就表示该 IP (或网域)“没有任何限制”

restrict default nomodifynotrapnoquery # 关闭所有的 NTP 要求封包
restrict 127.0.0.1    #这是允许本级查询
restrict 192.168.0.1 mask 255.255.255.0 nomodify
#在192.168.0.1/24网段内的服务器就可以通过这台NTP Server进行时间同步了
# 2. 上层主机的设定
#  要设定上层主机主要以 server 这个参数来设定,语法为:
#  server [IP|HOST Name] [prefer]
#  Server 后面接的就是我们上层 Time Server 啰!而如果 Server 参数
#  后面加上perfer的话,那表示我们的 NTP 主机主要以该部主机来作为
#  时间校正的对应。另外,为了解决更新时间封包的传送延迟动作,
#  所以可以使用driftfile来规定我们的主机
#  在与 Time Server 沟通时所花费的时间,可以记录在driftfile 
#  后面接的文件内,例如下面的范例中,我们的 NTP server 与 
#  cn.pool.ntp.org联机时所花费的时间会记录在 /etc/ntp/drift文件内
server 0.pool.ntp.org

server 1.pool.ntp.org

server 2.pool.ntp.org

server cn.pool.ntp.org prefer

#其他设置值,以系统默认值即可

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys /etc/ntp/keys

总结一下,restrict用来设置访问权限,server用来设置上层时间服务器,driftfile用来设置保存漂移时间的文件。

 

.ntp服务的启动与观察

在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间咯。(因为启动服务器,端口会被服务端占用,就不能手动同步时间了)

[root@linux ~] # ntpdate cn.pool.ntp.org

25 Apr 14:33:51 ntpdate[8310]: step time server 80.85.129.2 offset 6.655976 sec

然后,启动ntpd服务:

[root@linux ~] # service ntpd start

或 [root@linux ~] # /etc/init.d/ntpd start

查看端口:

[root@linux ~] # netstat -ln|grep 123

udp        0      0 192.168.228.153:123        0.0.0.0:*

udp        0      0 127.0.0.1:123               0.0.0.0:*

udp        0      0 0.0.0.0:123                  0.0.0.0:*

udp        0      0 :::123                       :::*

如何确认我们的NTP服务器已经更新了自己的时间呢?

[root@linux ~] # ntpstat

synchronized to NTP server(127.127.1.0) at stratum 11

time correct to within 950ms

polling server every 64 s

#改指令可列出NTP服务器是否与上层联机。由上述输出结果可知,时间校正约

#为950*10(-6)秒。且每隔64秒会主动更新时间。

常见的错误:

25 Apr 15:30:17 ntpdate[11520]: no server suitable for synchronization found

其实,这不是一个错误。而是由于每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接。当此时用客户端连接服务端就会报这样的信息。一般等待几分钟就可以了。

[root@linux ~] # ntptrace –n 127.0.0.1

127.0.0.1:stratum 11, offset 0.000000,synch distance 0.950951

222.73.214.125:stratum 2,offset –0.000787,synch distance 0.108575

209.81.9.7:stratum 1,offset 0.000028,synch distance 0.00436,refid ‘GPS’

#这个指令可以列出目前NTP服务器(第一层)与上层NTP服务器(第二层)彼此之间的

#关系

[root@linux ~] # ntpq –p

 

指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:

remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。

refid:参考的上一层NTP主机的地址

st:即stratum阶层

when:几秒前曾做过时间同步更新的操作

poll:下次更新在几秒之后

reach:已经向上层NTP服务器要求更新的次数

delay:网络传输过程钟延迟的时间

offset:时间补偿的结果

jitter:Linux系统时间与BIOS硬件时间的差异时间

 

    最后提及一点,ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件。

在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

转自:http://blog.sina.com.cn/s/blog_636a55070101u1mg.html

 

其它:

NTP的配置

 

 

 

A: 配置/etc/ntp.conf

 

NTP Server的主要配置文件为/etc/ntp.conf ,没有修改过的ntp.conf文件内容如下所示,配置选项都有相关注释信息(Linux 版本为Red Hat Enterprise Linux Server release 6.6 )

[root@localhost ~]# more /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 -6 default kod nomodify notrap nopeer noquery
 
# 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.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
 
 
#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
 
# 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

各个选项信息:

#系统时间与BIOS事件的偏差记录

driftfile /etc/ntp/drift

 

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

1:设定NTP主机来源(其中prefer表示优先主机),192.168.7.49是本地的NTP服务器,所以优先指定从该主机同步时间。

server 192.168.7.49 prefer 
 
server 0.rhel.pool.ntp.org iburst
 
server 1.rhel.pool.ntp.org iburst
 
server 2.rhel.pool.ntp.org iburst
 
server 3.rhel.pool.ntp.org iburst

clip_image001

 

2:限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器

 

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间

 

#此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

 

#设置默认策略为允许任何主机进行时间同步

restrict default ignore

 

3:确保localhost(这个常用的IP地址用来指Linux服务器本身)有足够权限.使用没有任何限制关键词的语法:

restrict 127.0.0.1

restrict -6 ::1

 

B:配置/etc/ntp/stpe-tickers文件

修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对)

[root@localhost ntp]# more /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
[root@localhost ntp]# vi /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
server 192.168.7.49 prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org 
server 2.rhel.pool.ntp.org 
server 3.rhel.pool.ntp.org 

关于ntp.conf and step-tickers区别:

step-tickers is used by ntpdate where as ntp.conf is the configuration file for the ntpd daemon. ntpdate is initially run to set the clock before ntpd to make sure time is within 1000 sec. ntp will not run if the time difference between the server and client by more then 1000 sec ( or there about). The start up script will read step-tickers for servers to be polled by ntpdate.

C:配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

#允许BIOS与系统时间同步,也可以通过hwclock -w 命令

SYNC_HWCLOCK=yes

 

IPTABLES 配置

由于NTP服务需要使用到UDP端口号123,所以当系统的防火墙(Iptables)启动的情况下,必须开放UDP端口号123。

[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# 

clip_image002

如果防火墙没有开放UDP端口号123,有可能出现下面情况。

[root@localhost ~]# /usr/sbin/ntpq -c rv | grep stratum

stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,

[root@localhost~]#

A stratum level of 16 indicates that NTP is not synchronizing correctly.If a stratum level of 16 is detected, wait 15 minutes and issue the command again. It may take this long for the NTP server to stabilize.If NTP continues to detect a stratum level of 16, verify that the NTP port (UDP Port 123) is open on all firewalls between the cluster and the remote machine you are attempting to synchronize to.

 

 

启动NTP服务

 

 

[root@localhost ~]# service ntpd status
ntpd is stopped
[root@localhost ~]# service ntpd start
Starting ntpd: [  OK  ]
[root@localhost ~]# 
 
service ntpd status      #查看ntpd服务状态
service ntpd start           #启动ntpd服务
service ntpd stop            #停止ntpd服务
service ntpd restart         #重启ntpd服务

检查ntp服务是否开机启动,将其设置为开机启动。

[root@localhost ~]# chkconfig --list ntpd

ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@localhost ~]# runlevel

N 3

[root@localhost ~]# chkconfig ntpd on #在运行级别2、3、4、5上设置为自动运行

[root@localhost ~]# chkconfig --list ntpd

ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@localhost ~]#

如果要设置在运行级别上自动运行,可以使用下面命令

chkconfig --level 345 ntpd on

可以用下面命令检测NTP服务是否运行

[root@localhost ~]# pgrep ntpd 
2639
2641
[root@localhost ~]# netstat -tlunp | grep ntp   #如果看到123端口,说明ntp服务成功启动。
udp        0      0 192.168.7.224:123           0.0.0.0:*                               2639/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2639/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2639/ntpd           
udp        0      0 fe80::250:56ff:feb3:b5:123  :::*                                    2639/ntpd           
udp        0      0 ::1:123                     :::*                                    2639/ntpd           
udp        0      0 :::123                      :::*                                    2639/ntpd           
[root@localhost ~]# 

clip_image003

查看ntp服务器有无和上层ntp连通

 

[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.7.49) at stratum 6 
   time correct to within 440 ms
   polling server every 128 s
[root@localhost ~]# 

查看ntp服务器与上层ntp的状态

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   13   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   17   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u    1   64    1    0.937   -9.570   0.000

clip_image004

remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid    - 参考上一层ntp主机地址

st       - stratum阶层

when     - 多少秒前曾经同步过时间

poll     - 下次更新在多少秒后

reach    - 已经向上层ntp服务器要求更新的次数

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。

clip_image005

第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。

remote 列出源的 IP 地址或主机名。

when   指出从轮询源开始已过去的时间(秒)。

poll   指出轮询间隔时间。该值会根据本地时钟的精度相应增加。

reach  是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。

offset 是源时钟与本地时钟的时间差(毫秒)。

 

ntpd、ntpdate的区别

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

转自:https://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

posted on 2018-06-20 14:40  苦涩的茶  阅读(279139)  评论(1编辑  收藏  举报

// 1.生成目录索引列表