w32time时间同步

Windows环境下,使用w32time服务实现时间同步:

1、经测试xp,win2003都可以作时间服务器。(生产环境中一般不使用win平台作服务器。)

2、ntp服务使用UDP协议123端口。

3、服务器和客户端都要注意时区设置,若差距超过54000秒,客户端win32time是不会同步的,系统事件提示错误。可设置HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\MaxPosPhaseCorrection和HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\MaxNegPhaseCorrection的值为 0xFFFFFFFF忽略时间差距(对服务器可不能这么做)。

4、同步精度为2秒。(这个说法来自说微软早期的文档)

一、Server端:

1、启用NTP服务器功能

将HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer内的Enabled设定为1

2、强制主机将它自身宣布为可靠的时间源

将HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\AnnounceFlags设定为5

3、重启w32time

二、client端

1、设定时间服务器,这里的是192.168.56.4

将HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters内的NtpServer设定为192.168.56.4,0x1

2、设定同步频率,单位为秒,这里设定为24小时,但系统会根据需要自动调整同步频率

将HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient内的SpecialPollInterval设定为86400

3、立即同步时间

w32tm /resync

无论成功与否都可以系统事件里找到记录。

注意:执行立即同步命令会导致系统时间跳变,对于数据库应用存在风险。

http://www.linuxidc.com/Linux/2011-09/42704.htm

4、也可不修改注册表直接使用命令(推荐的做法)

注册服务(复原NTP注册表项):
net stop w32time 
w32tm /unregister 
w32tm /register 
net start w32time

测试NTP服务器是否可用(将显示服务端与本地端时间差,会根据服务端的stratum值来判定是否可用):
w32tm /monitor /computers:192.168.56.4

配置NTP服务器地址:
w32tm /config /update /manualpeerlist:192.168.56.4 /syncfromflags:manual

设定同步频率为一小时:
reg add "HKLM\SYSTEM\CurrentControlSet\Services\w32time\TimeProviders\NtpClient" /v SpecialPollInterval /t REG_DWORD /d 3600 /f

立即执行时间同步:
w32tm /resync

查询服务器时间与本地时间差(可用于日常检查,offset值应在-1至1,不考虑服务端的stratum值):
w32tm /stripchart /computer:192.168.56.4 /samples:5 /dataonly

另外,如果想在客户端控制面板中增加时间服务器,可以做如下设定:

将HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers下的1设定为192.168.56.4

或者也可以新增一个字符值,再修改default。

关于w32time的同步策略,只找到win2000的:

When the local clock offset has been determined, the following algorithm is used to adjust the time:
If the local clock time of the client is behind the current time received from the server, W32Time will change the local clock time immediately.
If the local clock time of the client is more than three minutes ahead of the time on the server, W32Time will change the local clock time immediately.
If the local clock time of the client is less than three minutes ahead of the time on the server, W32Time will quarter or halve the clock frequency for long enough to bring the clocks into sync. If the client is less that 15 seconds ahead, it will halve the frequency; otherwise, it will quarter the frequency. The amount of time the clock spends running at an unusual frequency depends on the size of the offset that is being corrected.

关于w32time的毛病,还真不少,用这句可以概括:

We do not guarantee and we do not support the accuracy of the W32Time service between nodes on a network. The W32Time service is not a full-featured NTP solution that meets time-sensitive application needs.

 详细看这里:https://www.greyware.com/software/domaintime/v5/overview/w32time.asp

 还有在wikipedia上找到的这段:

Microsoft says that the W32Time service cannot reliably maintain sync time to the range of 1 to 2 seconds. If higher accuracy is desired, Microsoft recommends using a different NTP implementation.

 https://en.wikipedia.org/wiki/Network_Time_Protocol

w32time的替代方案:http://www.nist.gov/pml/div688/grp40/softwarelist.cfm

相关参考:

http://www.wangchao.net.cn/bbsdetail_1929070.html

http://support.microsoft.com/kb/314054/zh-cn

http://blog.sina.com.cn/s/blog_6bbe9f580100od92.html

https://www.meinbergglobal.com/english/sw/ntp.htm#ntp_nt

http://baike.baidu.com/link?url=xS-3Z_fpE3lmG9NEOdPAkcXlXYwuIWToxsxPXz59G7GX3O0yLlGkyGcpaZSpkF4C9JK97AvhqBJ_eiJ6qAJf6q

http://gnaw0725.blog.51cto.com/156601/660275

https://technet.microsoft.com/en-us/library/cc773263(v=ws.10).aspx

相关推荐