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;
        }
    }
}
相关推荐
斯普信专业组7 分钟前
Kafka主题运维全指南:从基础配置到故障处理
运维·分布式·kafka
倔强的石头10632 分钟前
【Linux指南】用户与系统基础操作
linux·运维·服务器
云上艺旅38 分钟前
centos升级内核
linux·运维·centos
kaikaile199541 分钟前
centos开启samba服务
linux·运维·centos
好多知识都想学1 小时前
Centos 7 服务器部署多网站
linux·服务器·centos
好多知识都想学1 小时前
centos 7 部署awstats 网站访问检测
linux·运维·centos
藥瓿亭1 小时前
K8S认证|CKS题库+答案| 10. Trivy 扫描镜像安全漏洞
linux·运维·服务器·云原生·容器·kubernetes·cks
leo__5202 小时前
在Ubuntu中设置开机自动运行(sudo)指令的指南
服务器·ubuntu·postgresql
Leo.yuan2 小时前
数据湖是什么?数据湖和数据仓库的区别是什么?
大数据·运维·数据仓库·人工智能·信息可视化
程序员JerrySUN2 小时前
Linux 内核内存管理子系统全面解析与体系构建
java·linux·运维