时间偏差超过15小时(54000秒),无法自动校时的解决方案

2024-08-29 11:40:27 浏览数 (1)

借鉴https://blog.csdn.net/weixin_43673589/article/details/109144725

承接https://cloud.tencent.com/developer/article/1851075

参考了微软官网文档:

https://docs.microsoft.com/zh-CN/troubleshoot/windows-server/identity/configure-w32ime-against-huge-time-offset

https://docs.microsoft.com/en-US/troubleshoot/windows-server/identity/configure-w32ime-against-huge-time-offset

https://docs.microsoft.com/zh-cn/troubleshoot/windows-client/identity/w32time-not-start-on-workgroup

https://docs.microsoft.com/en-us/troubleshoot/windows-client/identity/w32time-not-start-on-workgroup

https://docs.microsoft.com/zh-cn/troubleshoot/windows-server/identity/specialpollinterval-polling-interval-time-service-not-correct

https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/specialpollinterval-polling-interval-time-service-not-correct

有2个注册表项很关键

MaxPosPhaseCorrection MaxNegPhaseCorrection

Windows 2000、Windows XP、Windows Server 2003 和 Windows Vista 中的 ,这2个 注册表项的默认值为0xFFFFFFF,全F意味着使计算机任何时候都能自动校时。

在 ≥2008 的Server系统中,已采用 MaxPosPhaseCorrection 和 MaxNegPhaseCorrection 注册表项的新默认值为 48 小时(172800秒)。

在≥Win7的PC系统,已采用 MaxPosPhaseCorrection 和 MaxNegPhaseCorrection 注册表项的新默认值为 15 小时(54000秒)。

具体情况执行命令查看

reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesW32TimeConfig"|findstr PhaseCorrection

如下问题只是偶现,在阿里云、腾讯云等多家云厂商都有遇到,应该是windows系统层面的机制,而不是云厂商方面的问题。

腾讯云:

阿里云:

我调整时区前的时区是utc 7曼谷时间,然后我调时区到utc-10夏威夷时间,重启机器后,发现系统时间还是曼谷时间,并且不能自动校时到utc-10夏威夷时间,有类似如上的Time-Service系统日志和提醒手动校时的文字。

Windows can't synchronize automatically with the time server because the time difference is too great. Please update your time manually.

解决方案如下:阿里那边应用这个方案也可以,只需要把涉及内网域名的地方换成阿里的即可

基本原理就是既设置ntp 1分钟校时,也设置开机计划任务1分钟校时,原因是开机后第1次校时要等若干分钟,校时成功后才会每隔1分钟校时1次,设置开机计划任务就是为了让开机后快速校时,不用等若干分钟(等若干分钟的原因见微软官网文档https://docs.microsoft.com/en-us/troubleshoot/windows-client/identity/w32time-not-start-on-workgroup ,参考微软文档后就不需要设置开机计划任务校时了,但设置上也无妨,双保险)

代码里的10进制数 4294967295 是16进制0xFFFFFFFF

管理员身份powershell运行代码

invoke-webrequest http://windowsgg-1251783334.cos.na-siliconvalley.myzijiebao.com/timesync.ps1 -outfile c:timesync.ps1 2>$null 1>$null

c:timesync.ps1

代码详情如下

schtasks.exe /delete /tn "time sync" /F 2>&1 > $null

schtasks.exe /delete /tn "timesync" /F 2>&1 > $null

$configfile =@"

@echo off`n`r

net stop w32time 2>&1 > nul`n`r

#w32tm /unregister`n`r

#w32tm /register`n`r

sc.exe triggerinfo w32time delete`n`r

sc.exe config w32time start= auto`n`r

sc.exe triggerinfo w32time start/networkon stop/networkoff`n`r

net start w32time`n`r

w32tm /config /manualpeerlist:"time1.tencentyun.com,0x1 time2.tencentyun.com,0x1 time3.tencentyun.com,0x1 time4.tencentyun.com,0x1 time5.tencentyun.com,0x1" /syncfromflags:manual /reliable:yes /update`n`r

w32tm /resync`n`r

w32tm /resync`n`r

w32tm /resync`n`r

"@

$configfile|Out-File -FilePath C:Windowstimesync.bat -Encoding ascii -Force

notepad C:Windowstimesync.bat

#Invoke-WebRequest -uri http://windowsgg-1251783334.cos.na-siliconvalley.myzijiebao.com/timesync.xml -OutFile c:timesync.xml

#Register-ScheduledTask -xml (Get-Content 'c:timesync.xml' | Out-String) -TaskName timesync -TaskPath -force

#上面2行代码适用≥2012系统,不适用2008R2系统,为了提升代码兼容性,上面2行的功能用下面的黑体代码替代了

$str1=""

$str2=""

$str3=""

if(((Get-WmiObject Win32_OperatingSystem).version) -eq "6.1.7601"){

$str1="certutil -urlcache -split -f "

$str2=" "

$str3="Win7"

}

if(((Get-WmiObject Win32_OperatingSystem).version) -ne "6.1.7601"){

$str1="Invoke-WebRequest -uri "

$str2=" -OutFile "

if(((Get-WmiObject Win32_OperatingSystem).version) -eq "6.3.9600"){$str3="Win8"}

if(((Get-WmiObject Win32_OperatingSystem).version).split(".")[0] -eq "10"){$str3="Win10"}

}

"Set-ExecutionPolicy Unrestricted -force" > c:importtask.ps1

$str1 ("'http://windowsgg-1251783334.cos.na-siliconvalley.myzijiebao.com/timesync.xml'") $str2 ("'c:timesync.xml'") >> c:importtask.ps1

powershell -file c:importtask.ps1

start-sleep 5

del "c:importtask.ps1" 2>&1 > $null

schtasks /create /tn "timesync" /XML c:timesync.xml 2>$null

start-sleep 5

del "c:timesync.xml" 2>&1 > $null

reg add "HKEY_LOCAL_MACHINESOFTWAREPoliciesMicrosoftW32TimeConfig" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f

reg add "HKEY_LOCAL_MACHINESOFTWAREPoliciesMicrosoftW32TimeConfig" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f

reg add "HKEY_LOCAL_MACHINESOFTWAREWow6432NodePoliciesMicrosoftW32TimeConfig" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f

reg add "HKEY_LOCAL_MACHINESOFTWAREWow6432NodePoliciesMicrosoftW32TimeConfig" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f

reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesw32timeConfig" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f

reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesw32timeConfig" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f

net stop w32time 2>&1 > $null

#w32tm /unregister

#w32tm /register

sc.exe triggerinfo w32time delete ; sc.exe config w32time start= auto

#sc.exe config w32time start= delayed-auto

sc.exe triggerinfo w32time start/networkon stop/networkoff

net start w32time

reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MinPollInterval" /t reg_DWORD /d 5 /f

reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MaxPollInterval" /t reg_DWORD /d 10 /f

reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f

reg add "HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f

reg add "HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f

w32tm /config /update

w32tm /config /manualpeerlist:"time1.tencentyun.com,0x1 time2.tencentyun.com,0x1 time3.tencentyun.com,0x1 time4.tencentyun.com,0x1 time5.tencentyun.com,0x1" /syncfromflags:manual /reliable:yes /update

net stop w32time 2>&1 > $null

net start w32time

reg query HKLMSYSTEMCurrentControlSetServicesW32TimeParameters | findstr "NtpServer Type"

reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeParameters | findstr "NtpServer Type"

reg query HKLMSOFTWAREWOW6432NodePoliciesMicrosoftW32timeParameters | findstr "NtpServer Type"

reg query HKLMSYSTEMCurrentControlSetServicesW32TimeConfig | findstr "PollInterval"

reg query HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient | findstr "PollInterval"

reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"

reg query HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"

w32tm /resync 2>&1 > $null

start-sleep 3

w32tm /resync 2>&1 > $null

start-sleep 3

w32tm /resync 2>&1 > $null

要验证调整不同的时区时是否会自动校时,命令行设置时区的命令tzutil /s ,举例

举例:

设置utc-10 阿拉斯加时区

tzutil /s "Hawaiian Standard Time"

设置utc-9阿拉斯加时区

tzutil /s "Alaskan Standard Time"

设置utc-8美国加拿大太平洋时区

tzutil /s "Pacific Standard Time"

设置utc 2埃及开罗时区

tzutil /s "Egypt Standard Time"

设置utc 8中国东八区

tzutil /s "China Standard Time"

0 人点赞