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

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

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

4.更改Windows服務(wù)打開服務(wù)管理器 WIN+R運(yùn)行 輸入services.msc

步驟1:配置NTP服務(wù)器
1.打開命令提示符(以管理員身份運(yùn)行)
2.配置NTP服務(wù)器
w32tm /config /manualpeerlist:{}, 0x8 /syncfromflags:MANUAL
注:{}內(nèi)是你要同步的外部服務(wù)器地址,例如復(fù)旦的時間同步服務(wù)器地址為:ntp.fudan.edu.cn,則完整命令如下:
w32tm /config /manualpeerlist:ntp.fudan.edu.cn,0x8 /syncfromflags:MANUAL
國家授時中心服務(wù)器的IP地址(210.72.145.44)
ntp.fudan.edu.cn (復(fù)旦)推薦使用
time-b.nist.gov1 s1a.time.edu.cn 北京郵電大學(xué)
s1b.time.edu.cn 清華大學(xué)
s1c.time.edu.cn 北京大學(xué)
s1d.time.edu.cn 東南大學(xué)
s1e.time.edu.cn 清華大學(xué)
s2a.time.edu.cn 清華大學(xué)
s2b.time.edu.cn 清華大學(xué)
s2c.time.edu.cn 北京郵電大學(xué)
s2d.time.edu.cn 西南地區(qū)網(wǎng)絡(luò)中心
s2e.time.edu.cn 西北地區(qū)網(wǎng)絡(luò)中心
s2f.time.edu.cn 東北地區(qū)網(wǎng)絡(luò)中心
s2g.time.edu.cn 華東南地區(qū)網(wǎng)絡(luò)中心
s2h.time.edu.cn 四川大學(xué)網(wǎng)絡(luò)管理中心
s2j.time.edu.cn 大連理工大學(xué)網(wǎng)絡(luò)中心
s2k.time.edu.cn CERNET桂林主節(jié)點(diǎn)
s2m.time.edu.cn 北京大學(xué)10.2.2.163:表示為局域網(wǎng)服務(wù)器IP
步驟2:啟動Windows時間服務(wù)
1.重新啟動Windows時間服務(wù)
net stop w32time net start w32time
步驟3:強(qiáng)制同步時間
強(qiáng)制重新同步時間
w32tm /resync
C#客戶端代碼實(shí)現(xiàn)同步時間
1. 同步NTP服務(wù)器時間的函數(shù)
using System;
using System.Diagnostics;
public class TimeSync
{
public static bool SyncTime(string ntpServer)
{
try
{
// 配置NTP服務(wù)器
if (!RunCommand($"w32tm /config /manualpeerlist:\"{ntpServer}\" /syncfromflags:manual /update"))
{
return false;
}
// 啟動Windows時間服務(wù)
if (!RunCommand("net start w32time"))
{
return false;
}
// 強(qiáng)制重新同步時間
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. 校驗(yàn)NTP服務(wù)器時間的函數(shù)
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($"校驗(yàn)時間時發(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;
}
}
}到此這篇關(guān)于Windows Server搭建局域網(wǎng)NTP時間服務(wù)器與客戶端通的文章就介紹到這了,更多相關(guān)Windows Server局域網(wǎng)NTP時間服務(wù)器與客戶端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
本地遠(yuǎn)程Windows服務(wù)器遠(yuǎn)程桌面不能相互復(fù)制粘貼的兩種解決方案
這篇文章主要給大家分享了本機(jī)遠(yuǎn)程Windows服務(wù)器遠(yuǎn)程桌面不能相互復(fù)制粘貼的兩種解決方案,對我們解決問題有一定的幫助,需要的朋友可以參考下2023-08-08
用Serv-U架設(shè)FTP服務(wù)器的方法與設(shè)置方法
用Serv-U架設(shè)FTP服務(wù)器的方法與設(shè)置方法...2007-10-10
win2003 服務(wù)器安全設(shè)置教程(權(quán)限+防火墻)
win2003 服務(wù)器安全設(shè)置教程(權(quán)限+防火墻)2009-07-07
windows環(huán)境下memcache配置方法 詳細(xì)篇
將memcache服務(wù)器安裝包解壓到C:\memcached文件夾后,使用cmd命令窗口安裝2011-05-05
Windows Server 2008 R2通過IP安全策略阻止某個IP訪問的設(shè)置方法
這篇文章主要介紹了Windows Server 2008 R2通過IP安全策略阻止某個IP訪問的設(shè)置方法,非常詳細(xì)的說明,最后有腳本之家給出的一些需要屏蔽與接受的端口與已經(jīng)配置好的安全策略文件大家可以根據(jù)需要添加2016-06-06
win2008 r2 服務(wù)器安全設(shè)置之安全狗設(shè)置圖文教程
服務(wù)器安全夠是一款不錯的服務(wù)器安全設(shè)置工具,這里為大家介紹一下windows版本的一些常見的設(shè)置技巧與優(yōu)點(diǎn),方便大家增加系統(tǒng)安全2017-06-06
Windows?Server?2022?組策略(gpedit.msc)設(shè)置匯總
這篇文章主要介紹了win2022中通過組策略對服務(wù)器進(jìn)行安全設(shè)置一些策略,我們配置服務(wù)器的時候都需要配置一下,尤其是ip安全策略2024-03-03
win2003 服務(wù)器安全設(shè)置教程(權(quán)限與本地策略)
腳本之家的這篇文章主要是補(bǔ)充下以前我們發(fā)的文章,對于服務(wù)器安全設(shè)置需要掌握很多的東西與技巧2012-07-07

