# 7.6.DNS

DNS 可以理解为一本[电话簿](https://en.wikipedia.org/wiki/Telephone_directory)，其中将 IP 地址与主机名相互关联。

有三个文件负责管理 FreeBSD 系统如何与 DNS 进行交互。这三个文件是 [hosts(5)](https://man.freebsd.org/cgi/man.cgi?query=hosts\&sektion=5\&format=html)、[resolv.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=resolv.conf\&sektion=5\&format=html) 和 [nsswitch.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=nsswitch.conf\&sektion=5\&format=html)。

除非在 `/etc/nsswitch.conf` 文件中另有说明，FreeBSD 将首先查看 `/etc/hosts` 文件中的地址，然后查看 `/etc/resolv.conf` 文件中的 DNS 信息。

> **注意**
>
> [nsswitch.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=nsswitch.conf\&sektion=5\&format=html) 文件指定了 nsdispatch（名称服务切换调度器）应如何运行。
>
> 在默认情况下，`/etc/nsswitch.conf` 文件中的 hosts 部分如下所示：
>
> ```ini
> hosts: files dns
> ```
>
> 例如，在使用 [nscd(8)](https://man.freebsd.org/cgi/man.cgi?query=nscd\&sektion=8\&format=html) 服务时，可以通过将该行更改为如下所示，来更改优先级顺序：
>
> ```ini
> hosts: files cache dns
> ```

## 7.6.1. 本地地址

`/etc/hosts` 文件是一个简单的文本数据库，用于提供主机名到 IP 地址的映射。可以将通过局域网连接的本地计算机的条目添加到此文件中，以便简化命名，而无需设置 DNS 服务器。此外，`/etc/hosts` 还可以用于提供互联网名称的本地记录，从而减少查询外部 DNS 服务器的需求，特别是对于常访问的名称。

例如，如果在本地环境中有一个 [www/gitlab-ce](https://cgit.freebsd.org/ports/tree/www/gitlab-ce/) 的本地实例，可以将其添加到 `/etc/hosts` 文件中，如下所示：

```ini
192.168.1.150 git.example.com git
```

## 7.6.2. 配置 Nameserver

FreeBSD 系统如何访问互联网域名系统（DNS）由 [resolv.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=resolv.conf\&sektion=5\&format=html) 控制。

`/etc/resolv.conf` 中最常见的条目有：

| `nameserver` | 解析器应该查询的名称服务器的 IP 地址。服务器按顺序查询，最多可以列出三个。 |
| ------------ | --------------------------------------- |
| `search`     | 主机名查找的搜索列表。通常由本地主机名的域决定。                |
| `domain`     | 本地域名。                                   |

典型的 `/etc/resolv.conf` 文件如下所示：

```ini
search example.com
nameserver 147.11.1.11
nameserver 147.11.100.30
```

> **注意**
>
> 选项 `search` 和 `domain` 只能使用其中一个。

使用 DHCP 时，[dhclient(8)](https://man.freebsd.org/cgi/man.cgi?query=dhclient\&sektion=8\&format=html) 通常会根据从 DHCP 服务器接收到的信息重写 `/etc/resolv.conf`。

> **注意**
>
> 如果当前正在配置的这台机器 **不是** DNS 服务器，可以使用 [local-unbound(8)](https://man.freebsd.org/cgi/man.cgi?query=local-unbound\&sektion=8\&format=html) 来提升 DNS 查询性能。
>
> 要在系统启动时启用它，请执行以下命令：
>
> ```sh
> # sysrc local_unbound_enable="YES"
> ```
>
> 要启动 [local-unbound(8)](https://man.freebsd.org/cgi/man.cgi?query=local-unbound\&sektion=8\&format=html) 服务，请执行以下命令：
>
> ```sh
> # service local_unbound start
> ```


---

# Agent Instructions: 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:

```
GET https://book.bsdcn.org/hanbook/di-7-zhang-wang-luo/7.6.-dns.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
