> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/openbsd/networking-and-daemons/services/time-services/ntp.md).

# NTP

## 概述

精确的系统时间对日志记录、加密协议、文件时间戳以及分布式系统的一致运行至关重要。OpenBSD 基本系统内置了安全、精简的 NTP 守护进程 `ntpd(8)`。该服务可将系统时钟与远程时间服务器同步，并可选地向其他系统提供时间同步。

本章介绍如何为 `ntpd(8)` 配置典型的客户端用法以及向本地网络提供时间服务。

## 默认行为

OpenBSD 默认启用 NTP 守护进程。系统启动时，若时钟偏差较大，会通过 `ntpd -s` 执行一次性的时间步进，随后继续在后台逐步调整。

默认配置使用公共 NTP 池：

```
servers pool.ntp.org
```

此默认设置对多数系统已足够，除非有特定服务器偏好，否则无需修改。

## 配置文件

`ntpd` 的配置文件位于 **/etc/ntpd.conf**，由纯文本指令组成。常用指令如下：

* `servers <address>`——使用一组时间服务器池。
* `server <address>`——使用单个指定的时间服务器。
* `listen on <interface>`——在指定接口或地址上监听，启用 NTP 服务器功能。

### 示例：仅客户端配置

```
servers pool.ntp.org
```

此配置使用从 NTP 池中选取的多个地理分布服务器。

### 示例：客户端与服务器配置

```
servers pool.ntp.org
listen on egress
```

此示例从外部服务器同步时间，并通过系统的 `egress` 接口向本地客户端提供 NTP 服务。

## 启用并启动 ntpd

验证 NTP 服务是否已启用并运行：

```sh
# rcctl check ntpd
```

手动启用并启动服务：

```sh
# rcctl enable ntpd
# rcctl start ntpd
```

## 查看同步状态

使用 `ntpctl(8)` 查询守护进程及其对等端的状态：

```sh
$ ntpctl -s status
$ ntpctl -s peers
```

这些命令报告同步状态、层级（stratum）、偏移量与网络延迟等信息。

## 手动时钟初始化

系统时钟偏差较大时（例如断电后，或无实时时钟的系统上），`ntpd` 可在一次性初始化模式下运行：

```sh
# ntpd -s
```

此命令立即步进时钟。默认情况下在启动时自动执行。

前台运行以便调试：

```sh
# ntpd -s -d
```

## 日志与故障排查

`ntpd` 将活动记录到 **/var/log/messages**。实时跟踪输出：

```sh
# tail -f /var/log/messages
```

确保包过滤规则允许出站 UDP 端口 123。若时间同步失败：

* 检查 DNS 解析是否正常。
* 使用 `ntpctl -s peers` 检查对等端的可达性与响应。
* 确认没有其他时间服务（如 `rdate`）同时运行。

## 向其他系统提供时间

要让本地网络中的其他系统以该 OpenBSD 主机为时间源进行同步，配置 `ntpd` 使用 `listen on` 指令：

```
servers pool.ntp.org
listen on em0
```

此配置查询外部 NTP 服务器，同时在 `em0` 接口上监听请求。

此方式适用于多台系统须与同一内部时间源同步，或外部网络访问受限的环境。

## 限制与行为

OpenBSD 的 `ntpd` 注重简洁与安全，有意省略了某些其他 NTP 实现中的加密认证功能。闰秒支持为自动处理，无需额外配置。

`ntpd` 适用于多数客户端与内部服务器场景，但不适合高度专业化或需要认证的 NTP 部署。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/openbsd/networking-and-daemons/services/time-services/ntp.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
