# 32.10.文件传输协议（FTP）

文件传输协议（FTP）为用户提供了一种简单的方式来上传和下载文件。FreeBSD 包含了 FTP 服务器软件 ftpd，在系统基础部分中。

FreeBSD 提供了几个配置文件来控制对 FTP 服务器的访问。本节总结了这些文件的作用。更多关于内建 FTP 服务器的细节，请参考 [ftpd(8)](https://man.freebsd.org/cgi/man.cgi?query=ftpd\&sektion=8\&format=html)。

## 32.10.1. 配置

最重要的配置步骤是决定哪些账户将被允许访问 FTP 服务器。FreeBSD 系统有许多系统账户，这些账户不应允许访问 FTP。禁止访问 FTP 的用户列表可以在 **/etc/ftpusers** 中找到。默认情况下，它包括系统账户。还可以添加其他不应允许访问 FTP 的用户。

在某些情况下，可能希望限制某些用户的访问权限，而不是完全禁止他们使用 FTP。可以通过创建 **/etc/ftpchroot** 来实现这一点，正如 [ftpchroot(5)](https://man.freebsd.org/cgi/man.cgi?query=ftpchroot\&sektion=5\&format=html) 中所描述的那样。此文件列出了受到 FTP 访问限制的用户和组。

要启用匿名 FTP 访问服务器，可以在 FreeBSD 系统上创建一个名为 `ftp` 的用户。用户将能够使用用户名 `ftp` 或 `anonymous` 登录到 FTP 服务器。当提示输入密码时，任何输入都会被接受，但按惯例，密码应该是一个电子邮件地址。当匿名用户登录时，FTP 服务器将调用 [chroot(2)](https://man.freebsd.org/cgi/man.cgi?query=chroot\&sektion=2\&format=html) 来限制用户的访问范围，仅限于 `ftp` 用户的主目录。

可以创建两个文本文件，指定显示给 FTP 客户端的欢迎信息。**/etc/ftpwelcome** 的内容将在用户到达登录提示符之前显示。登录成功后，**/etc/ftpmotd** 的内容将被显示。请注意，文件的路径相对于登录环境，因此匿名用户的 **\~ftp/etc/ftpmotd** 内容将被显示。

配置好 FTP 服务器后，在 **/etc/rc.conf** 中设置适当的变量，以便在启动时启动服务：

```sh
ftpd_enable="YES"
```

要立即启动服务，请执行：

```sh
# service ftpd start
```

通过输入以下命令测试连接到 FTP 服务器：

```sh
% ftp localhost
```

ftpd 守护进程使用 [syslog(3)](https://man.freebsd.org/cgi/man.cgi?query=syslog\&sektion=3\&format=html) 来记录消息。默认情况下，系统日志守护进程将与 FTP 相关的消息写入 **/var/log/xferlog**。可以通过修改 **/etc/syslog.conf** 中的以下行来更改 FTP 日志的位置：

```sh
ftp.info      /var/log/xferlog
```

> **注意**
>
> 请注意运行匿名 FTP 服务器可能会带来的一些潜在问题。特别是，考虑清楚是否允许匿名用户上传文件。FTP 站点可能会变成未经授权的商业软件交易的场所，甚至更糟。如果需要匿名 FTP 上传，请确保权限设置正确，确保这些文件在管理员审查之前，其他匿名用户无法读取。


---

# 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-32-zhang-wang-luo-fu-wu-qi/32.10.-wen-jian-chuan-shu-xie-yi-ftp.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.
