# 16.10.TCP 封装器（TCP Wrapper）

TCP Wrappers 是一种基于主机的网络访问控制系统。通过在实际的网络服务之前拦截传入的网络请求，TCP Wrappers 根据预定义的规则评估源 IP 地址是否被允许访问或被拒绝。

然而，尽管 TCP Wrappers 提供了基本的访问控制，但不应被视为更强大安全措施的替代品。为了全面保护，建议结合使用防火墙、适当的用户身份验证实践和入侵检测系统。

## 16.10.1. 初始配置

TCP Wrappers 默认在 [inetd(8)](https://man.freebsd.org/cgi/man.cgi?query=inetd\&sektion=8\&format=html) 中启用。因此，第一步是启用 [inetd(8)](https://man.freebsd.org/cgi/man.cgi?query=inetd\&sektion=8\&format=html)，执行以下命令：

```sh
# sysrc inetd_enable="YES"
# service inetd start
```

然后，正确配置 **/etc/hosts.allow**。

> **警告**
>
> 不同于其他实现，FreeBSD 中 **hosts.deny** 的使用已被弃用。所有配置选项应放在 **/etc/hosts.allow** 中。

在最简单的配置中，守护进程的连接策略设置为允许或阻止，具体取决于 **/etc/hosts.allow** 中的选项。FreeBSD 的默认配置是允许所有连接到由 inetd 启动的守护进程。

基本配置通常采用 `daemon : address : action` 形式，其中 `daemon` 是 inetd 启动的守护进程，`address` 是有效的主机名、IP 地址或用括号括起来的 IPv6 地址 (\[ ])，`action` 是 `allow` 或 `deny`。TCP Wrappers 使用“先匹配规则”的语义，意味着配置文件会从头开始扫描匹配的规则。待找到匹配项，规则会被应用，搜索过程停止。

例如，要允许通过 [mail/qpopper](https://cgit.freebsd.org/ports/tree/mail/qpopper/) 守护进程进行 POP3 连接，可以将以下行添加到 **/etc/hosts.allow**：

```ini
# 这是允许 POP3 连接所必需的：
qpopper : ALL : allow
```

每次编辑此文件时，请重新启动 inetd：

```sh
# service inetd restart
```

## 16.10.2. 高级配置

TCP Wrappers 提供了高级选项，可以更好地控制连接的处理方式。在某些情况下，可能需要对某些主机或守护进程连接返回评论。在其他情况下，可能需要记录日志条目或向管理员发送电子邮件。还有些情况可能需要仅对本地连接使用某个服务。所有这些都可以通过使用通配符、扩展字符和外部命令执行来实现。要了解更多关于通配符及其相关功能的信息，请参阅 [hosts\_access(5)](https://man.freebsd.org/cgi/man.cgi?query=hosts_access\&sektion=5\&format=html)。


---

# 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://handbook.bsdcn.org/di-16-zhang-an-quan/16.10.-tcp-feng-zhuang-qi.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.
