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;
        }
    }
}
相关推荐
sakoba14 分钟前
Docker学习其二(容器卷,Docker网络,Compose)
运维·网络·学习·docker·容器·基础
A了LONE36 分钟前
cv弹窗,退款确认弹窗
java·服务器·前端
来自于狂人41 分钟前
CentOS 镜像源配置与 EOL 后的应对策略
linux·运维·centos
吉凶以情迁2 小时前
window服务相关问题探索 go语言服务开发探索调试
linux·服务器·开发语言·网络·golang
卍郝凝卍2 小时前
云上服务器常见的存储方式和类型
大数据·服务器·数据库
柏木乃一3 小时前
Linux初步认识与指令与权限
linux·运维·服务器·shell·权限
189228048613 小时前
NX947NX955美光固态闪存NX962NX966
大数据·服务器·网络·人工智能·科技
Joemt3 小时前
ubuntu源码编译安装cmake高版本、pybind11安装、crow使用
linux·运维·ubuntu
真智AI4 小时前
打破数据质量瓶颈:用n8n实现30秒专业数据质量报告自动化
大数据·运维·人工智能·python·自动化