# ntp-server **Repository Path**: hj-smart/ntp-server ## Basic Information - **Project Name**: ntp-server - **Description**: ntp服务端以及作为二级代理角色 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-14 - **Last Updated**: 2026-03-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ntp-server #### 一、介绍 ntp服务端以及作为二级代理角色,支持切换服务器角色 1. 当作为NTP服务器时,仅需提供时间同步服务 2. 当作为NTP二级代理时,提供时间同步服务,并支持向上游服务器发送时间同步请求,更新本地时间 #### 二、测试说明 1. 切换到二级代理模式 ```powershell curl.exe -X POST http://localhost:9123/api/ntp/v1/config -H "Content-Type: application/json" -d '{\"mode\": \"proxy\", \"upstream_server\": \"ntp.ntsc.ac.cn:123\"}' ``` 2. 切换到主NTP服务器模式 ```powershell curl.exe -X POST http://localhost:9123/api/ntp/v1/config -H "Content-Type: application/json" -d '{\"mode\": \"server\"}' ``` 3. 查询NTP服务信息 ```powershell curl.exe http://localhost:9123/api/ntp/v1/status ``` #### 三、部署说明 1. 下载ntp-server.service文件,并放置到/etc/systemd/system/目录下 2. 修改ntp-server.service文件中的ExecStart参数,设置为ntp-server的执行路径 3. 启动ntp-server服务 ```powershell # 重新加载 systemd 配置 sudo systemctl daemon-reload # 启动服务 sudo systemctl start ntp-server # 启用开机自启 sudo systemctl enable ntp-server # 查看服务状态 sudo systemctl status ntp-server # 查看服务日志 sudo journalctl -u ntp-server -f ``` #### 四、注意事项 1. 因为windows存在时间服务`w32time`,会占用123端口,所以ntp服务端设定了监听端口为50123,否则需要先停掉windows自身的时间服务,命令如下: ```powershell # 停掉w32time服务 net stop w32time # 启动w32time服务 net start w32time ``` 2. 对于Linux系统,建议在启动服务前停止系统自带的 NTP 服务: ```powershell sudo systemctl stop systemd-timesyncd sudo systemctl disable systemd-timesyncd ``` #### 五、针对openEuler操作系统 1. openEuler 操作系统默认支持 NTP 服务,它使用 `Chrony` 作为默认的时间同步服务。`Chrony` 是一个更现代化的 NTP 实现,比传统的 ntpd 具有更好的性能和准确性。 2. 查看`Chrony` 服务状态 ```shell systemctl status chronyd ``` ##### 自定义部署NTP服务 1. 先停用`Chrony`服务 ```shell systemctl stop chronyd systemctl disable chronyd ``` 2. 修改service文件,添加对`Chrony`的依赖关系: ```shell [Unit] Description=Custom NTP Server Documentation=https://github.com/yourusername/ntp-server After=network-online.target Wants=network-online.target # 添加与 chronyd 的冲突声明 Conflicts=chronyd.service Before=chronyd.service [Service] Type=simple User=root # 假设可执行文件安装在 /usr/local/bin 目录下 ExecStart=/usr/local/bin/ntp-server \ --ntp-listen=:50123 \ --http-listen=:9123 # 添加服务启动前的预处理命令 ExecStartPre=-/usr/bin/systemctl stop chronyd.service ExecStartPre=-/usr/bin/systemctl disable chronyd.service Restart=always RestartSec=10 # 给予服务足够的权限来修改系统时间 CapabilityBoundingSet=CAP_SYS_TIME AmbientCapabilities=CAP_SYS_TIME # 增加安全性配置 NoNewPrivileges=true ProtectSystem=full ProtectHome=true ProtectKernelTunables=true ProtectKernelModules=true ProtectControlGroups=true [Install] WantedBy=multi-user.target ``` 3. 使用方法如下: ```shell # 安装服务 sudo cp ntp-server.service /etc/systemd/system/ sudo systemctl daemon-reload # 启动服务(会自动停止和禁用 chronyd) sudo systemctl start ntp-server # 启用开机自启 sudo systemctl enable ntp-server ```