Windows?Server搭建局域網NTP時間服務器與客戶端通
1.服務器環(huán)境:

win11更改注冊表win+R輸入regedit
win11更改注冊表 win+R輸入regedit
2.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config,找到Config目錄,雙擊Config目錄下的AnnounceFlags,設為5。

3.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer,將enabled設為1。

4.更改Windows服務打開服務管理器 WIN+R運行 輸入services.msc

步驟1:配置NTP服務器
1.打開命令提示符(以管理員身份運行)
2.配置NTP服務器
w32tm /config /manualpeerlist:{}, 0x8 /syncfromflags:MANUAL
注:{}內是你要同步的外部服務器地址,例如復旦的時間同步服務器地址為:ntp.fudan.edu.cn,則完整命令如下:
w32tm /config /manualpeerlist:ntp.fudan.edu.cn,0x8 /syncfromflags:MANUAL
國家授時中心服務器的IP地址(210.72.145.44)
ntp.fudan.edu.cn (復旦)推薦使用
time-b.nist.gov1 s1a.time.edu.cn 北京郵電大學
s1b.time.edu.cn 清華大學
s1c.time.edu.cn 北京大學
s1d.time.edu.cn 東南大學
s1e.time.edu.cn 清華大學
s2a.time.edu.cn 清華大學
s2b.time.edu.cn 清華大學
s2c.time.edu.cn 北京郵電大學
s2d.time.edu.cn 西南地區(qū)網絡中心
s2e.time.edu.cn 西北地區(qū)網絡中心
s2f.time.edu.cn 東北地區(qū)網絡中心
s2g.time.edu.cn 華東南地區(qū)網絡中心
s2h.time.edu.cn 四川大學網絡管理中心
s2j.time.edu.cn 大連理工大學網絡中心
s2k.time.edu.cn CERNET桂林主節(jié)點
s2m.time.edu.cn 北京大學10.2.2.163:表示為局域網服務器IP
步驟2:啟動Windows時間服務
1.重新啟動Windows時間服務
net stop w32time net start w32time
步驟3:強制同步時間
強制重新同步時間
w32tm /resync
C#客戶端代碼實現(xiàn)同步時間
1. 同步NTP服務器時間的函數
using System;
using System.Diagnostics;
public class TimeSync
{
public static bool SyncTime(string ntpServer)
{
try
{
// 配置NTP服務器
if (!RunCommand($"w32tm /config /manualpeerlist:\"{ntpServer}\" /syncfromflags:manual /update"))
{
return false;
}
// 啟動Windows時間服務
if (!RunCommand("net start w32time"))
{
return false;
}
// 強制重新同步時間
if (!RunCommand("w32tm /resync"))
{
return false;
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"同步時間時發(fā)生錯誤: {ex.Message}");
return false;
}
}
private static bool RunCommand(string command)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", $"/c {command}")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = Process.Start(psi))
{
using (System.IO.StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"執(zhí)行命令 '{command}' 時發(fā)生錯誤: {ex.Message}");
return false;
}
}
}2. 校驗NTP服務器時間的函數
using System;
using System.Diagnostics;
public class TimeCheck
{
public static bool CheckTime()
{
try
{
// 檢查同步狀態(tài)
return RunCommand("w32tm /query /status");
}
catch (Exception ex)
{
Console.WriteLine($"校驗時間時發(fā)生錯誤: {ex.Message}");
return false;
}
}
private static bool RunCommand(string command)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", $"/c {command}")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = Process.Start(psi))
{
using (System.IO.StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"執(zhí)行命令 '{command}' 時發(fā)生錯誤: {ex.Message}");
return false;
}
}
}到此這篇關于Windows Server搭建局域網NTP時間服務器與客戶端通的文章就介紹到這了,更多相關Windows Server局域網NTP時間服務器與客戶端內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
本地遠程Windows服務器遠程桌面不能相互復制粘貼的兩種解決方案
這篇文章主要給大家分享了本機遠程Windows服務器遠程桌面不能相互復制粘貼的兩種解決方案,對我們解決問題有一定的幫助,需要的朋友可以參考下2023-08-08
windows環(huán)境下memcache配置方法 詳細篇
將memcache服務器安裝包解壓到C:\memcached文件夾后,使用cmd命令窗口安裝2011-05-05
Windows Server 2008 R2通過IP安全策略阻止某個IP訪問的設置方法
這篇文章主要介紹了Windows Server 2008 R2通過IP安全策略阻止某個IP訪問的設置方法,非常詳細的說明,最后有腳本之家給出的一些需要屏蔽與接受的端口與已經配置好的安全策略文件大家可以根據需要添加2016-06-06
Windows?Server?2022?組策略(gpedit.msc)設置匯總
這篇文章主要介紹了win2022中通過組策略對服務器進行安全設置一些策略,我們配置服務器的時候都需要配置一下,尤其是ip安全策略2024-03-03

