Windows Server搭建局域网NTP时间服务器与客户端通实现

1.服务器环境:

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服务器

bash 复制代码
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 西南地区网络中心

s2e.time.edu.cn 西北地区网络中心

s2f.time.edu.cn 东北地区网络中心

s2g.time.edu.cn 华东南地区网络中心

s2h.time.edu.cn 四川大学网络管理中心

s2j.time.edu.cn 大连理工大学网络中心

s2k.time.edu.cn CERNET桂林主节点

s2m.time.edu.cn 北京大学

10.2.2.163:表示为局域网服务器IP

步骤2:启动Windows时间服务

1.重新启动Windows时间服务

bash 复制代码
net stop w32time

net start w32time

步骤3:强制同步时间

强制重新同步时间
bash 复制代码
w32tm /resync

C#客户端代码实现同步时间与

  1. 同步NTP服务器时间的函数
cs 复制代码
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($"同步时间时发生错误: {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($"执行命令 '{command}' 时发生错误: {ex.Message}");
            return false;
        }
    }
}

2. 校验NTP服务器时间的函数

cs 复制代码
using System;
using System.Diagnostics;

public class TimeCheck
{
    public static bool CheckTime()
    {
        try
        {
            // 检查同步状态
            return RunCommand("w32tm /query /status");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"校验时间时发生错误: {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($"执行命令 '{command}' 时发生错误: {ex.Message}");
            return false;
        }
    }
}
相关推荐
likfishdn18 分钟前
Linux的文件与目录管理
linux·运维·服务器
渲染101专业云渲染19 分钟前
川翔云电脑优势总结
服务器·3d·电脑·blender·maya
chian-ocean37 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=39 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
橘子味的茶二1 小时前
高级系统架构师--第十章:计算机网络
服务器·计算机网络·系统架构
紫菜(Nori)1 小时前
短连接服务器压测-wrk
服务器·压力测试
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
北京耐用通信2 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net