> For the complete documentation index, see [llms.txt](https://lack.gitbook.io/operation-and-maintenance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lack.gitbook.io/operation-and-maintenance/linuxri-chang-yun-wei/linuxwen-jian-te-shu-quan-xian.md).

# linux文件特殊权限

## 文件的特殊权限

```bash
[root@localhost ~]# lsattr 1.txt 
---------------- 1.txt
[root@localhost ~]#
```

linux的文件目录中还有一些特殊权限，或者说隐藏文件

### lsattr 命令

`lsattr` 命令：

介绍：查看目录文件的隐藏属性

格式：lsattr \[option] file|fir

用法：

* -R 逐级查看目录下的子目录和文件
* -d 只看目录本身

```bash
[root@localhost ~]# lsattr 1.txt 
---------------- 1.txt
[root@localhost ~]# lsattr -R ./
---------------- ./anaconda-ks.cfg
---------------- ./1.txt
[root@localhost ~]# lsattr -d ./
---------------- ./
```

### chattr 命令

`chattr` 命令：

介绍：给目录文件增加隐藏属性

格式：chattr \[option] file|dir

用法：

* +a 追加不可修改，删除，写入(可追加)的权限
* -a
* +i 追加不可修改，删除，写入(不可追加)的权限
* -i

> \> 追加但会覆盖原有的内容
>
> \>> 追加至文件的尾部

```bash
[root@localhost ~]# chattr +a 1.txt 
[root@localhost ~]# lsattr 
---------------- ./anaconda-ks.cfg
-----a---------- ./1.txt
[root@localhost ~]# rm -fr 1.txt
rm: 无法删除"1.txt": 不允许的操作
[root@localhost ~]# echo "123" > 1.txt 
-bash: 1.txt: 不允许的操作
[root@localhost ~]# echo "123" >> 1.txt 
[root@localhost ~]# cat 1.txt
123
123
[root@localhost ~]# chattr -a 1.txt
[root@localhost ~]# lsattr 1.txt
---------------- 1.txt
[root@localhost ~]# rm -fr 1.txt 
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# echo "123" > 1.txt
[root@localhost ~]# chattr +i 1.txt
[root@localhost ~]# echo "123" >> 1.txt
-bash: 1.txt: 权限不够
[root@localhost ~]# rm -fr 1.txt
rm: 无法删除"1.txt": 不允许的操作
[root@localhost ~]#
```

## 特殊权限

特殊权限：在一般权限前的 chmod n755 n为特殊权限

suid：set\_uid,数字为4,语法高亮为红色

> /etc/shadow 存放用户密码

rws：s：suid 临时给出属主权限，只在二进制的可执行文件中有效

如passwd 属性为-rwsr-xr-x. 普通用户使用时临时拥有属主权限

用法：

* chmod u+s file 给目录文件suid权限
* chmod 4755 file
* chmod u=rws file

  当给文件赋值suid时，文件无x权限是，则为rwS，无执行权限

```bash
[root@localhost ~]# chmod u+s 1.txt
[root@localhost ~]# ls -l 1.txt
-rwSr--r-- 1 root root 4 8月   1 21:10 1.txt
[root@localhost ~]# chmod u+x 1.txt
[root@localhost ~]# ls -l 1.txt
-rwsr--r-- 1 root root 4 8月   1 21:10 1.txt
```

sgid:set\_gid,数字为2,语法高亮为黄色

rws：s：sgid 临时给出属组权限

用法：

* chmod g+s dir|file 给目录文件sgid权限
* chmod 2755 dir|file

> 注：chmod g=rws file时 r

当给文件sgid时，文件无x权限是，则为rwS，无执行权限

当给目录sgid是，在文件下创建的子目录和文件都将继承目录的原来组权限

如：目录123为 drwxrwsrwx 2 root root 4096 12月 29 03:01 123

切换用户后在123下创建的文件和子目录为

drwxrwsr-x 2 chi root 4096 12月 29 03:04 123

-rw-rw-r-- 1 chi root 0 12月 29 03:07 456

```bash
[root@localhost tmp]# ls -l test/
总用量 0
-rw-r--r-- 1 root root 0 8月   1 21:21 123.txt
[root@localhost tmp]# ls -ld test/
drwxrwsr-x 2 root root 21 8月   1 21:21 test/
[root@localhost tmp]# chmod 2777 -R test
[root@localhost tmp]# ls -ld test/
drwxrwsrwx 2 root root 21 8月   1 21:21 test/
[root@localhost tmp]# su - ping
上一次登录：三 8月  1 21:22:02 CST 2018pts/0 上
[ping@localhost ~]$ cd /tmp
[ping@localhost tmp]$ echo "123" > test/ping.txt
[ping@localhost tmp]$ ls -l test/
总用量 4
-rwxrwsrwx 1 root root 0 8月   1 21:21 123.txt
-rw-rw-r-- 1 ping root 4 8月   1 21:24 ping.txt
```

sticky:stick\_bit,数字为1，语法高亮为绿色

rwt： t：防删除位

只作用于目录，用于可被其他人访问的目录中，使文件无法被属主外的用户删除(root除外)

用法：

* chmod o+t dir
* chmod 1777 dir (目录要有--x权限，无则为rwxrwxrwT，无意义)

  如:

  目录123为drwxrwxrwt 2 root root 4096 12月 29 03:45 123

  目录下创建文件456为-rwxrwxrwx 1 root root 0 12月 29 03:45 456

  rm -fr 456 结果为rm: 无法删除"456": 不允许的操作

  目录123为drwxrwxrwx 2 root root 4096 12月 29 03:45 123

  目录下创建文件456为-rwxrwxrwx 1 root root 0 12月 29 03:45 456

  rm -fr 456 结果为删除成功。


---

# 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:

```
GET https://lack.gitbook.io/operation-and-maintenance/linuxri-chang-yun-wei/linuxwen-jian-te-shu-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.
