# 14.2.配置文件

FreeBSD 明确区分了基本系统和第三方应用程序，因此这也影响了这些应用程序配置文件的位置。

FreeBSD 基本系统的配置文件位于 `/etc` 目录，而通过 Ports 和软件包安装的应用程序的所有配置文件则位于 `/usr/local/etc` 目录。

内核状态配置位于 `/etc/sysctl.conf`。在 [The sysctl utility](https://docs.freebsd.org/en/books/handbook/config/#configtuning-sysctl) 章节中，将更详细地解释 [sysctl(8)](https://man.freebsd.org/cgi/man.cgi?query=sysctl\&sektion=8\&format=html) 的操作。

有关 FreeBSD 文件系统结构的更多信息，请参阅 [hier(7)](https://man.freebsd.org/cgi/man.cgi?query=hier\&sektion=7\&format=html)。

作为一般规则，配置文件在语法上并没有统一标准。尽管 `#` 字符通常用于注释行，并且每行通常包含一个配置变量。

> **注意**
>
> 一些应用程序，如 [pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html)，已经开始使用 [Universal Configuration Language (UCL)](https://github.com/vstakhov/libucl)。

## 14.2.1. `/etc` 目录

`/etc` 目录包含所有 FreeBSD 基本系统的配置文件，负责配置 FreeBSD 系统。

> **小心**
>
> **在修改 `/etc` 目录中的文件时必须非常小心**；配置错误可能导致 FreeBSD 无法启动或发生故障。

| `/etc`             | 系统配置文件和脚本。                                                                                                                                                                                               |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `/etc/defaults`    | 默认的系统配置文件，更多信息请参见 [rc(8)](https://man.freebsd.org/cgi/man.cgi?query=rc\&sektion=8\&format=html)。                                                                                                         |
| `/etc/fstab`       | [fstab(5)](https://man.freebsd.org/cgi/man.cgi?query=fstab\&sektion=5\&format=html) 包含有关各种文件系统的描述信息。                                                                                                     |
| `/etc/mail`        | 额外的 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html) 配置和其他 MTA 配置文件。                                                                                            |
| `/etc/mtree`       | mtree 配置文件，更多信息请参见 man: mtree\[8]。                                                                                                                                                                       |
| `/etc/pam.d`       | 可插拔认证模块 (PAM) 库的配置文件。                                                                                                                                                                                    |
| `/etc/periodic`    | 每日、每周和每月运行的脚本，通过 [cron(8)](https://man.freebsd.org/cgi/man.cgi?query=cron\&sektion=8\&format=html) 执行，更多信息请参见 [periodic(8)](https://man.freebsd.org/cgi/man.cgi?query=periodic\&sektion=8\&format=html)。 |
| `/etc/rc.d`        | 系统和守护进程启动/控制脚本，更多信息请参见 [rc(8)](https://man.freebsd.org/cgi/man.cgi?query=rc\&sektion=8\&format=html)。                                                                                                    |
| `/etc/rc.conf`     | 包含有关本地主机名、任何潜在网络接口的配置详细信息，以及应在系统初次启动时启动的服务的描述信息。更多信息请参见 [Managing System-Specific Configuration](https://docs.freebsd.org/en/books/handbook/bsdinstall/#configtuning-core-configuration)                 |
| `/etc/security`    | OpenBSM 审计配置文件，更多信息请参见 [audit(8)](https://man.freebsd.org/cgi/man.cgi?query=audit\&sektion=8\&format=html)。                                                                                              |
| `/etc/ppp`         | ppp 配置文件，更多信息请参见 [ppp(8)](https://man.freebsd.org/cgi/man.cgi?query=ppp\&sektion=8\&format=html)。                                                                                                        |
| `/etc/ssh`         | OpenSSH 配置文件，更多信息请参见 [ssh(1)](https://man.freebsd.org/cgi/man.cgi?query=ssh\&sektion=1\&format=html)。                                                                                                    |
| `/etc/ssl`         | OpenSSL 配置文件。                                                                                                                                                                                            |
| `/etc/sysctl.conf` | 包含内核设置。更多信息请参见 [The sysctl utility](https://docs.freebsd.org/en/books/handbook/bsdinstall/#configtuning-sysctl)。                                                                                         |

## 14.2.2. `sysctl` 实用程序

[`sysctl(8)`](https://man.freebsd.org/cgi/man.cgi?query=sysctl\&sektion=8\&format=html) 实用程序用于修改正在运行的 FreeBSD 系统。

[`sysctl(8)`](https://man.freebsd.org/cgi/man.cgi?query=sysctl\&sektion=8\&format=html) 实用程序检索内核状态，并允许具有适当权限的进程设置内核状态。要检索或设置的状态使用“管理信息库”（“MIB”）样式的名称，描述为一个点分的组件集。

**表 1. 管理信息库**

| sysctl    | 幻数            |
| --------- | ------------- |
| kern      | 内核功能和特性       |
| vm        | 虚拟内存          |
| vfs       | 文件系统          |
| net       | 网络            |
| debug     | 调试参数          |
| hw        | 硬件            |
| machdep   | 机器相关          |
| user      | 用户空间          |
| p1003\_1b | POSIX 1003.1B |

`sysctl(8)` 的核心功能是读取和修改系统设置。

要查看所有可读取的变量：

```sh
% sysctl -a
```

输出类似于以下内容：

```sh
kern.ostype: FreeBSD
...
vm.swap_enabled: 1
vm.overcommit: 0
vm.domain.0.pidctrl.kdd: 8
vm.domain.0.pidctrl.kid: 4
vm.domain.0.pidctrl.kpd: 3
...
vfs.zfs.sync_pass_rewrite: 2
vfs.zfs.sync_pass_dont_compress: 8
vfs.zfs.sync_pass_deferred_free: 2
```

要读取特定变量，指定其名称：

```sh
% sysctl kern.maxproc
```

输出类似于以下内容：

```sh
kern.maxproc: 1044
```

管理信息库（MIB）是分层的，因此指定一个前缀会列出它下面的所有节点：

```sh
% sysctl net
```

输出类似于以下内容：

```sh
net.local.stream.recvspace: 8192
net.local.stream.sendspace: 8192
net.local.dgram.recvspace: 16384
net.local.dgram.maxdgram: 2048
net.local.seqpacket.recvspace: 8192
net.local.seqpacket.maxseqpacket: 8192
net.local.sockcount: 60
net.local.taskcount: 25
net.local.recycled: 0
net.local.deferred: 0
net.local.inflight: 0
net.inet.ip.portrange.randomtime: 1
net.inet.ip.portrange.randomcps: 9999
[...]
```

要设置特定变量，使用 *变量*=*值* 语法：

```sh
# sysctl kern.maxfiles=5000
```

输出类似于以下内容：

```sh
kern.maxfiles: 2088 -> 5000
```

> **注意**
>
> 为了在重启后保持配置，必须将这些变量添加到 `/etc/sysctl.conf` 文件中，如下所述。

## 14.2.3. `/etc/sysctl.conf` 文件

[sysctl(8)](https://man.freebsd.org/cgi/man.cgi?query=sysctl\&sektion=8\&format=html) 的配置文件 `/etc/sysctl.conf` 看起来类似于 `/etc/rc.conf`。

使用 `variable=value` 语法设置值。

> **注意**
>
> 指定的值会在系统进入多用户模式后设置。并非所有变量都可以在此模式下设置。

例如，要关闭致命信号退出的日志记录，并防止用户查看其他用户启动的进程，可以在 `/etc/sysctl.conf` 中设置以下参数：

```ini
# 不记录致命信号退出（例如，sig 11）
kern.logsigexit=0

# 防止用户查看由其他 UID 启动的进程信息。
security.bsd.see_other_uids=0
```

要获取有关特定 sysctl 功能的更多信息，可以执行以下命令：

```sh
% sysctl -d kern.dfldsiz
```

输出应类似于以下内容：

```sh
kern.dfldsiz: 初始数据大小限制
```

## 14.2.4. 管理系统特定的配置

系统配置的主要位置是 `/etc/rc.conf`。

该文件包含广泛的配置信息，并在系统启动时读取以配置系统。它为 rc\* 文件提供配置信息。

`/etc/rc.conf` 中的条目将覆盖 `/etc/defaults/rc.conf` 中的默认设置。

> **技巧**
>
> 不应编辑 `/etc/defaults/rc.conf` 文件中包含的默认设置。所有特定于系统的更改应写入 `/etc/rc.conf`。

在集群应用程序中，可以应用多种策略，将全站配置与特定系统配置分开，以减少管理开销。

推荐的方法是将特定系统的配置放入 `/etc/rc.conf.local`。

例如，`/etc/rc.conf` 中的这些条目适用于所有系统：

```ini
sshd_enable="YES"
keyrate="fast"
defaultrouter="10.1.1.254"
```

而 `/etc/rc.conf.local` 中的这些条目仅适用于此系统：

```ini
hostname="node1.example.org"
ifconfig_fxp0="inet 10.1.1.1/8"
```

使用诸如 `rsync` 或 `puppet` 等应用程序将 `/etc/rc.conf` 分发到每个系统，而 `/etc/rc.conf.local` 保持唯一。

系统升级不会覆盖 `/etc/rc.conf`，因此系统配置信息不会丢失。

> **技巧**
>
> `/etc/rc.conf` 和 `/etc/rc.conf.local` 都由 [sh(1)](https://man.freebsd.org/cgi/man.cgi?query=sh\&sektion=1\&format=html) 解析。这使得系统操作员能够创建复杂的配置场景。有关此主题的更多信息，请参见 [rc.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=rc.conf\&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-14-zhang-pei-zhi-yu-you-hua/14.2.-pei-zhi-wen-jian.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.
