# linux文件目录权限

```bash
[root@localhost ~]# ls -l
总用量 8
-rw-r--r--  1 root root   12 7月  31 21:38 1.txt
-rw-r--r--  1 root root    0 7月  30 22:34 22.txt
-rw-r--r--  1 root root    0 7月  30 22:31 2.txt
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
```

使用`ls -l` 查看目录下的文件时我们会看到文件中`-rw-r--r--` 这样的文件属性。分为五段为13331

* 第一段只有一个字符 - ，表示文件的类型
* 第二段三个字符表示文件属主的权限：r:可读 w:可写 x:可执行，也可用数字表示为 r=4，w=2，x=1，所以像文件1.txt的权限数字表示为644，root用户对该文件有读写权限。
* 第三段三个字符表示文件属组的权限，所以root组对该文件有读权限。
* 第四段三个字符表示其他用户的权限，所以其他用户对该文件有读权限。
* 第五段的 . 字符表示文件创建时是否开启selinux(linux安全系统)，开启才有 . 。

而后面的`root`和`root`分别表示文件的所属主和所属组。

## chmod 命令

`chmod`命令：

介绍：change mode 的缩写，修改文件的权限

格式：chmod \[option] number file

用法：

* chmod 666 dir|file ：数字表示，修改文件权限&#x20;
* chmod u+x,g+w,o-r dir|file

  &#x20;：加减表示
* chmod u=rwx，g=rw，o=r

  &#x20;：赋值表示
* -R 令目录及其目录下的文件都继承

> u为用户 g为组 o为其他人 a为所有u+g+o
>
> r=4 w=2 x=1 rwx=7 rw-=6 r-x=5

```bash
[root@localhost ~]# ls -l 1.txt
-rwxrwxrwx 1 root root 12 7月  31 21:38 1.txt
[root@localhost ~]# chmod 600 1.txt
[root@localhost ~]# ls -l 1.txt
-rw------- 1 root root 12 7月  31 21:38 1.txt
[root@localhost ~]# chmod  a+x,u+w,g+r,o+w 1.txt 
[root@localhost ~]# ls -l 1.txt
-rwxr-x-wx 1 root root 12 7月  31 21:38 1.txt
[root@localhost ~]# chmod u=r,g=rwx,o=wx 1.txt
[root@localhost ~]# ls -l 1.txt
-r--rwx-wx 1 root root 12 7月  31 21:38 1.txt
```

## umask 命令

`umask` 命令:

介绍：查看和更改默认权限\
， 默认0022(一般用后3位)\
root 有最高权限不受此影响 ，所以目录为rwxrwxrwx - ----w--w- = rwxr-xr-x(有x权限才能进入)\
文件为rw-rw-rw- - ----w-w- = rw-r--r--

格式：umask number

用法：

* umask ：直接使用查看文件默认权限
* umask number ：修改文件权限

```bash
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r--  1 root root    0 7月  31 22:25 1.txt
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# umask 0111
[root@localhost ~]# touch 2.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r--  1 root root    0 7月  31 22:25 1.txt
-rw-rw-rw-  1 root root    0 7月  31 22:26 2.txt
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# umask 0222
[root@localhost ~]# touch 3.txt
[root@localhost ~]# ls -l
总用量 4
-rw-r--r--  1 root root    0 7月  31 22:25 1.txt
-rw-rw-rw-  1 root root    0 7月  31 22:26 2.txt
-r--r--r--  1 root root    0 7月  31 22:26 3.txt
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
```

## chown 命令

`chown` 命令:

介绍：change owner 更改属主属组

格式：chown \[user].\[user] \[option] file|dir

用法：

* chown root.root dir|file 修改目录或文件为root用户root组
* -R ：令目录及其下的文件继承权限&#x20;
* chown .root 只改属组 同 chgrp root
* chown root. 只改属主

> 命令 . 也可被换 : 替换。

```bash
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 ping ping 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# chown root: anaconda-ks.cfg 
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# chown ping:root anaconda-ks.cfg 
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 ping root 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# chown :ping anaconda-ks.cfg 
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 ping ping 1430 7月  12 10:41 anaconda-ks.cfg
[root@localhost ~]# chown -R root:root ./
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
```

## history 命令

`history` 命令：

介绍：查看命令历史

用法：直接使用

> ！命令标号 ： 可直接执行编号的命令\
> 。！ch ： 执行最近ch开头的命令

```bash
[root@localhost ~]# history | tail -2
  250  history 
  251  history | tail -2
[root@localhost ~]# history | tail -4
  249  ls -l
  250  history 
  251  history | tail -2
  252  history | tail -4
[root@localhost ~]# !249
ls -l
总用量 4
-rw-------. 1 root root 1430 7月  12 10:41 anaconda-ks.cfg
```


---

# 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://lack.gitbook.io/operation-and-maintenance/linuxri-chang-yun-wei/linuxwen-jian-mu-lu-quan-xian.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.
