---
url: https://eoht.net/fe/linux/file-permission.md
description: >-
  系统介绍 Linux 文件与目录权限模型，解释文件类型、所有者、用户组、其他用户以及 r、w、x 在文件和目录上的不同含义，说明权限判定顺序和 root
  账户的限制，并演示使用 chmod 的符号模式与数字模式修改权限、用 chown 更改所有者或用户组，结合 744、765 等示例拆解权限数值和实际效果。
---

# Linux 文件权限 管理 Linux 文件访问控制 {#linux-file-permissions-guide}

## Linux 的文件权限 {#understand-linux-file-permissions}

Linux 操作系统中对权限的管理很严格。Linux 系统中不仅是对用户与组根据 UID，GID 进行了管理，还对 Linux 系统中的文件，按照用户与组进行分类，针对不同的群体进行了权限管理，用他来确定谁能通过何种方式和目录进行访问和操作。

权限共有 10 个字符，我们将它分为 4 大部分来理解：

### 表示文件的类型 {#linux-file-types}

|     |                                  |
| --- | -------------------------------- |
| `-` | 表示是一个文件                   |
| `d` | 表示是一个目录                   |
| `l` | 表示是一个连接（理解为快捷方式） |

### 用户组类型 {#linux-user-group-types}

| 缩写 | 用户组  | 类型                               |
| ---- | ------- | ---------------------------------- |
| `u`  | `owner` | 属主 :当前用户具有的对该文件的权限 |
| `g`  | `group` | 当前组内其他用户具有对该文件的权限 |
| `o`  | `other` | 其他组的用户具有的对该文件的权限   |

### 权限 {#linux-permission-bits}

* `r`：`Read` 读
* `w`：`Write` 写
* `x`：`execute` 执行

针对目录加执行权限，文件不加执行权限（因文件具备执行权限有安全隐患）

针对文件和目录来说，`r，w，x` 有着不同的作用和含义
|命令 |针对文件|针对目录|
| --- | --- | --- |
|`r`|读取文件内容|查看目录下的文件列表|
|`w`|修改文件内容|删除和创建目录下的文件|
|`x`|执行权限对除二进制程序以外的文件没什么意义|可以 cd 进入目录，能查看目录中文件的详细属性，能访问目录下文件内容(基础权限)|

:::tip
root 账户不受文件权限的读写限制，执行权限受限制
:::

<table>
  <caption>权限分配</caption>
  <colgroup span="2"></colgroup>
  <colgroup span="3"></colgroup>
  <colgroup span="3"></colgroup>
  <colgroup span="3"></colgroup>
  <thead>
    <tr>
      <th scope="col" rowspan="2">权限项</th>
      <th scope="col" rowspan="2">文件类型</th>
      <th scope="colgroup" colspan="3">文件所有者</th>
      <th scope="colgroup" colspan="3">文件所属组用户</th>
      <th scope="colgroup" colspan="3">其他用户</th>
    </tr>
    <tr>
      <th scope="col">读</th>
      <th scope="col">写</th>
      <th scope="col">执行</th>
      <th scope="col">读</th>
      <th scope="col">写</th>
      <th scope="col">执行</th>
      <th scope="col">读</th>
      <th scope="col">写</th>
      <th scope="col">执行</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">字符表示</th>
      <td>(d|l|c|s|p)</td>
      <td>（r）</td>
      <td>（w）</td>
      <td>(x)</td>
      <td>(r)</td>
      <td>(w)</td>
      <td>(x)</td>
      <td>(r)</td>
      <td>(w)</td>
      <td>(x)</td>
    </tr>
    <tr>
      <th scope="row">数字表示</th>
      <td></td>
      <td>4</td>
      <td>2</td>
      <td>1</td>
      <td>4</td>
      <td>2</td>
      <td>1</td>
      <td>4</td>
      <td>2</td>
      <td>1</td>
    </tr>
  </tbody>
</table>

用户获取文件权限的顺序：先看是否为所有者，如果是，则后面权限不看；再看是否为所属组，如果是，则后面权限不看。

## 修改文件权限 {#change-linux-file-permissions}

`chown` 是 `change owner` 的意思，主要作用就是改变文件或者目录所有者。
|||
|---|---|
|`chmod`|修改文件和文件夹读写执行属性。使用权限：`所有使用者`|
|`chown`|修改文件和文件夹的用户和用户组属性。使用权限：`root`|

## mode 方式 {#symbolic-permission-mode}

```bash
chmod who opt per file
```

|       |                                                                                   |
| ----- | --------------------------------------------------------------------------------- |
| `who` | ` u g o a(all)` (`u 用户 user`，`g 用户组 group`，`o 其他用户`，`a 所有用户默认`) |
| `opt` | `+` 添加某个权限 `-` 取消某个权限 `=` 赋予权限                                    |
| `per` | `r w x X`                                                                         |

示例：

```bash
chmod u=rwx,g=r a.txt
chmod u+x,g+w,o+w test.log #r 读，w 写， x 执行
```

## 数字方式设置权限 {#numeric-permission-mode}

### 命令语法 {#chmod-command-syntax}

使用数字方式修改文件权限的基本语法如下：

```bash
chmod xxx file
```

其中 `xxx` 是一个三位数，分别表示用户、用户组、其他用户的权限。

### 权限对应关系表 {#linux-permission-value-table}

| 权限  | 数字值 | 权限总和 |
| ----- | ------ | -------- |
| `rwx` | `421`  | `7`      |
| `rw-` | `420`  | `6`      |
| `r--` | `400`  | `4`      |

> * `0` 表示无权限
> * `1` 表示执行权限（`x`）
> * `2` 表示写权限（`w`）
> * `4` 表示读权限（`r`）

### 权限示例 {#linux-permission-examples}

```bash
-rwxr--r-- 1 root root 10 oct 16 02:55 yhp.log
```

| 权限类别     | 权限字符串 | 数值拆解 | 权限总和 |
| ------------ | ---------- | -------- | -------- |
| 用户权限     | `rwx`      | `4+2+1`  | `7`      |
| 用户组权限   | `r--`      | `4+0+0`  | `4`      |
| 其他用户权限 | `r--`      | `4+0+0`  | `4`      |

### 权限组合示例：`744` {#chmod-744-example}

代表用户拥有读、写、执行权限，组和其他用户只有读权限。

## 修改权限示例 {#chmod-usage-example}

以下操作展示如何修改文件权限：

* 所有人都加入写入权限（+2）
* 给用户组加入写入权限（+2）
* 给其他用户加入执行权限（+1）

### 权限组合结果：`765` {#chmod-765-result}

```bash
chmod 765 a.txt
```

## Linux 学习路径 {#linux-learning-path}

权限操作会直接影响文件命令的结果，可先对照 [Linux 文件操作](/fe/linux/file)；继续排查程序与资源占用时，可阅读 [Linux 进程管理](/fe/linux/process)。
