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;
        }
    }
}
相关推荐
_平凡之路_4 分钟前
解决ubuntu22.04 gnome-terminal 无法启动的问题
linux·运维·python
凯子坚持 c7 分钟前
0基础带你入门Linux之使用
linux·运维·服务器
hgdlip20 分钟前
电脑ip会因为换了网络改变吗
服务器·网络·tcp/ip·电脑
EterNity_TiMe_24 分钟前
【Linux基础IO】深入Linux文件描述符与重定向:解锁高效IO操作的秘密
linux·运维·服务器·学习·性能优化·学习方法
python-码博士24 分钟前
Rosetta 一:手把手教你用Linux安装Rosetta(全网最简洁)
linux·运维·服务器
你可以自己看29 分钟前
python中函数式编程与高阶函数,装饰器与生成器,异常处理与日志记录以及项目实战
服务器·开发语言·python
神秘的土鸡1 小时前
Linux中Docker容器构建MariaDB数据库教程
linux·运维·服务器·数据库·docker·mariadb
coisini.cn1 小时前
Windows10、CentOS Stream9 环境下安装kafka_2.12-3.6.2记录
运维·zookeeper·kafka·windows10·centos stream 9
Amd7941 小时前
Nuxt Kit中的 Nitro 处理程序
服务器·插件·处理程序·模块化·nuxt 3·预渲染·nitro
路溪非溪1 小时前
Linux内核启动流程
linux·运维·服务器