> 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/di-35-zhang-fu-wu-qi/di-35.4-jie-ling-pei-zhi-wang-luo-mdns-dns-sd.md).

# 35.4 零配置网络（mDNS/DNS-SD）

零配置网络（[Zero-configuration networking](https://datatracker.ietf.org/doc/html/rfc6762)，也称 *Zeroconf*）是一组简化网络配置的技术，使网络设备无需管理员干预即可自动完成地址分配、域名解析与服务发现。

该功能在嵌入式设备上较为实用。在某些场景下，用户无法通过屏幕或串口获取树莓派的 IP 地址，但需通过 SSH 连接。树莓派 OS 默认启用 Avahi 服务且支持多播 DNS，在此情况下用户可直接使用 `ping raspberrypi.local` 获取 IP 地址，或直接通过 SSH 连接 `raspberrypi.local`。

零配置网络的主要部分包括：

| 组成部分         | 英文名称                  | 功能说明           |
| ------------ | --------------------- | -------------- |
| 链路本地寻址       | Link-Local Addressing | 提供数值型网络地址的自动分配 |
| 多播 DNS       | mDNS                  | 提供主机名的自动分发与解析  |
| 基于 DNS 的服务发现 | DNS-SD                | 提供服务实例的自动发现    |

Zeroconf 的常用实现是 [Avahi](https://avahi.org/)。**nss-mdns** 可在所有使用 **nsswitch**（名称服务切换，The Name Service Switch，NSS）的工具中，通过 mDNS 协议查找 `*.local` 后缀的主机名。

```sh
应用程序（ping、ssh 等）
   │
   │ 查询 ykla.local
   ▼
nsswitch（名称服务切换）
   │
   │ 按顺序查找：files → dns → mdns
   ▼
nss-mdns（NSS 模块）
   │
   │ 发送 mDNS 多播查询（UDP 5353）
   ▼
Avahi 守护进程（avahi-daemon）
   │
   │ 响应主机名对应的 IP 地址
   ▼
应用程序获得 IP 地址
```

## 安装 Avahi 和 nss-mdns

使用 pkg 安装：

```sh
# pkg install avahi-app nss_mdns
```

使用 Ports 安装：

```sh
# cd /usr/ports/net/avahi-app/ && make install clean
# cd /usr/ports/dns/nss_mdns/ && make install clean
```

## 配置名称服务切换

dns/nss\_mdns 安装以下 NSS 模块：

```sh
lib/nss_mdns.so.1
lib/nss_mdns_minimal.so.1
lib/nss_mdns4.so.1
lib/nss_mdns4_minimal.so.1
lib/nss_mdns6.so.1
lib/nss_mdns6_minimal.so.1
lib/nss_mdns.so
lib/nss_mdns_minimal.so
lib/nss_mdns4.so
lib/nss_mdns4_minimal.so
lib/nss_mdns6.so
lib/nss_mdns6_minimal.so
```

关于上述模块的说明，参见 [avahi/nss-mdns](https://github.com/avahi/nss-mdns)。

**/etc/nsswitch.conf** 文件是 nsswitch 的配置文件，源代码位于 **lib/libc/net/nsswitch.conf**，默认文件内容如下：

```ini
#
# nsswitch.conf(5) - name service switch configuration file
#
group: compat
group_compat: nis
hosts: files dns
netgroup: compat
networks: files
passwd: compat
passwd_compat: nis
shells: files
services: compat
services_compat: nis
protocols: files
rpc: files
```

为了启用 mDNS，需在 `hosts: files dns` 行后追加 `mdns` 和 `mdns6`。

```ini
hosts: files dns mdns mdns6
```

## 启用服务

D-Bus 作为 Avahi 的依赖自动安装，将其设为开机自启：

```sh
# service dbus enable
```

启动 D-Bus 服务：

```sh
# service dbus start
```

将 Avahi 守护进程设置为开机自启：

```sh
# service avahi-daemon enable
```

启动 Avahi 服务：

```sh
# service avahi-daemon start
```

## 测试服务

执行 ping 命令测试 FreeBSD 的主机名，即可显示 IP 地址。

```powershell
PS C:\Users\ykla> ping ykla

正在 Ping ykla.local [192.168.179.129] 具有 32 字节的数据:
来自 192.168.179.129 的回复: 字节=32 时间=6ms TTL=64
来自 192.168.179.129 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.179.129 的回复: 字节=32 时间=3ms TTL=64
来自 192.168.179.129 的回复: 字节=32 时间=3ms TTL=64

192.168.179.129 的 Ping 统计信息:
    数据包: 已发送 = 4，已接收 = 4，丢失 = 0 (0% 丢失)，
往返行程的估计时间(以毫秒为单位):
    最短 = 1ms，最长 = 6ms，平均 = 3ms
```

> **技巧**
>
> 上述示例中的 `ykla`、`ykla.local` 为占位符，需要替换为实际的值。

也可查看 IPv6 地址：

```powershell
PS C:\Users\ykla> ping -6 ykla

正在 Ping ykla.local [fe80::20c:29ff:fe8a:22dc%18] 具有 32 字节的数据:
来自 fe80::20c:29ff:fe8a:22dc%18 的回复: 时间=1ms
来自 fe80::20c:29ff:fe8a:22dc%18 的回复: 时间=3ms
来自 fe80::20c:29ff:fe8a:22dc%18 的回复: 时间=4ms
来自 fe80::20c:29ff:fe8a:22dc%18 的回复: 时间=13ms

fe80::20c:29ff:fe8a:22dc%18 的 Ping 统计信息:
    数据包: 已发送 = 4，已接收 = 4，丢失 = 0 (0% 丢失)，
往返行程的估计时间(以毫秒为单位):
    最短 = 1ms，最长 = 13ms，平均 = 5ms
```

与 FreeBSD 主机的实际 IP 地址一致，验证通过。

使用 SSH 连接到该设备：

```powershell
PS C:\WINDOWS\system32> ssh ykla@ykla

FreeBSD 16.0-CURRENT (GENERIC) #0 main-n285005-e9fc0c538264: Mon Apr 13 12:44:54 UTC 2026

Welcome to FreeBSD!

……省略部分输出……
```

> **技巧**
>
> 本节示例中出现的用户名 `ykla` 及主机名 `ykla` 均为示例，请根据自身环境替换为实际用户名和主机名。


---

# 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/di-35-zhang-fu-wu-qi/di-35.4-jie-ling-pei-zhi-wang-luo-mdns-dns-sd.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.
