linux文件特殊权限

文件的特殊权限

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

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

lsattr 命令

lsattr 命令:

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

格式:lsattr [option] file|fir

用法:

  • -R 逐级查看目录下的子目录和文件

  • -d 只看目录本身

[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

> 追加但会覆盖原有的内容

>> 追加至文件的尾部

[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,无执行权限

[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

[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 结果为删除成功。

最后更新于

这有帮助吗?