> 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/man/man8/chown.8.md).

# chown.8

`chown` — 更改文件属主和属组

## 名称

`chown`

## 概要

`chown [-fhvx] [-R [-H | -L | -P]] owner[:group] file`

`chown [-fhvx] [-R [-H | -L | -P]] :group file`

## 描述

`chown` 工具用于更改指定文件的用户 ID 和/或组 ID。除非使用 `-h` 选项，否则由参数指定的符号链接将被忽略而不作更改。

可用选项如下：

**`-H`** 如果指定了 `-R` 选项，则跟随命令行上的符号链接，因此这些链接本身不受命令影响。（遍历过程中遇到的符号链接不会被跟随。）

**`-L`** 如果指定了 `-R` 选项，则跟随所有符号链接。

**`-P`** 如果指定了 `-R` 选项，则不跟随任何符号链接。这是默认行为。

**`-R`** 更改以所列文件为根的整个文件层次结构的用户 ID 和/或组 ID，而不仅仅是文件本身。当使用诸如 “`.*`” 之类的通配符时，要小心避免误匹配指向父目录的硬链接 “`..`”。

**`-f`** 不报告任何更改文件属主或属组失败的信息，也不修改退出状态以反映此类失败。

**`-h`** 如果文件是符号链接，则更改链接本身的用户 ID 和/或组 ID。

**`-v`** 使 `chown` 输出详细信息，在属主被修改时显示文件。如果 `-v` 标志被指定多次，`chown` 将打印文件名，随后显示旧的和新数值用户/组 ID。

**`-x`** 不遍历文件系统挂载点。

除非指定了 `-R` 选项，否则 `-H`、`-L` 和 `-P` 选项将被忽略。此外，这些选项会相互覆盖，命令的行为由最后指定的选项决定。

`owner` 和 `group` 操作数均为可选，但必须指定其中之一。如果指定了 `group` 操作数，其前必须加上一个冒号（“:”）字符。

`owner` 可以是数值用户 ID 或用户名。如果用户名同时也是一个数值用户 ID，则该操作数按用户名处理。`group` 可以是数值组 ID 或组名。如果组名同时也是一个数值组 ID，则该操作数按组名处理。

出于明显的安全原因，文件的所有权只能由超级用户更改。

如果 `chown` 收到 `SIGINFO` 信号（参见 stty(1) 的 `status` 参数），则会显示当前文件名以及旧的和新文件属主和属组信息。

## 退出状态

`chown` 工具在成功时退出码为 0，发生错误时大于 0。

## 兼容性

早期版本的 `chown` 工具使用点号（“.”）字符来区分组名。现已改为冒号（“:”）字符，以便用户名和组名可以包含点号。

在早期版本的系统中，符号链接没有属主。

`-v` 和 `-x` 选项是非标准的，不建议在脚本中使用。

## 参见

[chgrp(1)](/man/man1/chgrp.1.md), [chmod(1)](/man/man1/chmod.1.md), [find(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/find.1.md), chown(2), fts(3), [groups(7)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man7/groups.7.md), symlink(7)

## 标准

`chown` 工具应符合 IEEE Std 1003.2 ("POSIX.2") 标准。

## 历史

`chown` 工具首次出现于 Version 1 AT\&T UNIX。


---

# 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/man/man8/chown.8.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.
