Linux基础知识之文件的权限(二)

Linux的基础知识之文件的权限(二)

  除了基本的r,w,x之外,在linux传统的ext2、ext3、ext4文件系统下,还可以设置其他 的文件属性。如chattr,lsattr,而在CentOS7中默认利用xfs作为默认的文件系统,就不支持chattr的参数。

文件的特殊权限

 SUID:如果用户在运行某程序时,拥有该权限的话,就会以该程序的拥有者的身份运行该程序。

  •  chmod u+|-s FILE...

  • 位置:文件属主的执行位,有执行权限显示为s,否则S

以二进制程序passwd为例:

[root@Der_Tencent ~]# info passwd  #passwd用来修改用户的密码

File: *manpages*,  Node: passwd,  Up: (dir)

PASSWD(1)                       User utilities                       PASSWD(1)

NAME
       passwd - update user's authentication tokens

SYNOPSIS
       passwd  [-k]  [-l]  [-u  [-f]]  [-d] [-e] [-n mindays] [-x maxdays] [-w
       warndays] [-i inactivedays] [-S] [--stdin] [username]
[root@Der_Tencent ~]# whereis passwd   #查看passwd程序的位置
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz

  [root@Der_Tencent ~]# ls /usr/bin/passwd -al   #查看passwd的详细信息
  -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd  #o,g,o都有文件的执行权

  [root@Der_Tencent ~]# ls -al /etc/shadow    #查看用户存放密码的文件
  ---------- 1 root root 1757 Sep 22 21:51 /etc/shadow    #只有root可强制修改文件

用户可以修改自己密码,但是修改的密码以密文存放在/ec/shadow中;如果用户没有/etc/shadow的写权限,又怎么能将修改的密码写入文件中呢?

仔细观察passwd程序的属性发现属主中的执行权限变成了‘ s ’,这个便是SUID权限。

当其他用户执行passwd时,便以root用户的身份执行该程序。用户便能正常修改自己密码了。可以其他程序文件验证,如cat,less等。

    • 权限在程序结束后归还;

 

SGID

  • chmod g+|-s FILE...
  • 位置:文件属组的执行位,有执行权限显示为s,否则S

 

对二进制程序文件:程序文件拥有SGID权限后,执行该程序的用户拥有程序属组的权限。  

扫描二维码关注公众号,回复: 7343112 查看本文章
[root@Der_Tencent project]# cp /usr/bin/cat /tmp/project1    #将cat程序复制到工作目录,工作目录对用户需要有r,x,w视需要而给;
[root@Der_Tencent project]# chown der:der /tmp/project1     #更改文件的属主和属组
[root@Der_Tencent project]# su - der                #切换为der用户(提前创建der和bob用户,且他们不同组)
[der@Der_Tencent project1]$ ls -al 
total 72
drwxrwsr-x 3 der der 4096 Sep 23 14:46 .
drwxrwxrwt. 16 root root 4096 Sep 23 14:56 ..
-rwxr-xr-x 1 der der 54160 Sep 23 14:46 cat            #程序的属主和属主均为der,且其他用户有执行权
[der@Der_Tencent project1]$ touch test               #新建测试文件  
[der@Der_Tencent project1]$ echo "This a test" > test      #写入测试数据
[der@Der_Tencent project1]$ ls -al test
-rw-rw-r-- 1 der der 12 Sep 23 15:19 test
[der@Der_Tencent project1]$ chmod o-r test            #去除其他人对测试文件的r权限
[root@Der_Tencent ~]# su - bob                   #切换用户,当前用户为root,否则需要密码
[bob@Der_Tencent project1]$ ./cat test              #执行文件为当前目录下的,而不是默认的/usr/bin/cat
./cat: test: Permission denied
[root@Der_Tencent project]# su - der               
[der@Der_Tencent project1]$ chmod g+s cat            #在组的执行位上加上SGID权限
[der@Der_Tencent project1]$ ls -l cat
-rwxr-sr-x 1 der der 54160 Sep 23 14:46 cat
[root@Der_Tencent project]# su - bob
[bob@Der_Tencent project1]$ ./cat test             #bob用户这时拥有der组的权利,可进行r操作
This a test

对目录:目录拥有该权限,用户在该目录下创建的文件(目录)的属组均为该目录的属组。

[root@Der_Tencent project]# chown :project /tmp/project1    #创一个工作组project****修改文件的权限只有root用户才有权利******
[root@Der_Tencent project1]# su - bob
[bob@Der_Tencent ~]$ touch /tmp/project1/test1          #此时在工作目录内创建的文件属组为文件的创建者
[bob@Der_Tencent project1]$ ls -l test1
-rw-rw-r--   1 bob  bob         0 Sep 23 15:45 test1
[bob@Der_Tencent project1]$ exit
[root@Der_Tencent ~]# chmod g+s /tmp/project1/        #给属组加上SGID权限
[root@Der_Tencent ~]# ls -al /tmp/project1
total 72
drwxrwsrwx   3 boob  project  4096 Sep 23 15:51 .       #属组x权限位变为s   
drwxrwxrwt. 15 root root     4096 Sep 23 15:57 ..
[root@Der_Tencent ~]# su - bob
[bob@Der_Tencent ~]$ touch /tmp/project1/test2
[bob@Der_Tencent ~]$ ls -al /tmp/project1/test2
-rw-rw-r-- 1 bob project 0 Sep 23 15:59 /tmp/project1/test2  #新建的文件属组变成了project工作组

SBIT

 对拥有工作目录有wx执行权限的用户来说,拥有SBIT权限,则该用户只能在目录下创建新文件,或删除自己的文件只能删除自己的文件

  • chmod o+|-t FILE...
  • 位置:其他用户的执行位,有执行权限显示为t,否则T
[der@Der_Tencent project1]$ ls -al /tmp
total 92
drwxrwxrwt. 15 root root    4096 Sep 23 16:16 .
[alice@Der_Tencent ~]$ rm -r /tmp/test
rm: remove write-protected regular empty file ‘/tmp/test/file1.txt’? y
rm: cannot remove ‘/tmp/test’: Operation not permitted
[der@Der_Tencent project1]$ rm -r /tmp/test
[der@Der_Tencent project1]$ 

 

chmod的数字表示法修改上述三种属性

  • SUID为4,SGID为2,SBIT为1
[root@Der_Tencent project1]# ls -al test2
-rw-r-xr-x 1 bob project 0 Sep 23 15:59 test2
[root@Der_Tencent project1]# chmod 7655 test2
[root@Der_Tencent project1]# ls -al test2
-rwSr-sr-t 1 bob project 0 Sep 23 15:59 test2

文件隐藏属性

  为了增加系统的安全性,在原来九个基本属性上还增加了隐藏属性。chattr指令只能在ext2、ext3、ext4的Linux传统文件系统上完整生效,其他的文件系统可能就无法完整的支持这个指令。(摘自《鸟哥的Linux私房菜:基础学习篇》)

  chattr chattr [+-=][ai] 文件或目录名称

选项与参数:
+ :增加某一个特殊参数,其他原本存在参数则不动。
- :移除某一个特殊参数,其他原本存在参数则不动。
= :设置一定,且仅有后面接的参数

a:只能增加数据,不能删除和修改数据,只有root才能设置

i:不能增加数据,不能删除改名,设置连接,只有root才能设置

[root@Der_Tencent ~]# mkdir test             #创建工作目录
[root@Der_Tencent ~]# cd test
[root@Der_Tencent test]# touch file1         #创建工作文件
[root@Der_Tencent test]# chattr +a file1       #添加a属性
[root@Der_Tencent test]# echo "Input data" > file1  #该符号会覆盖文件中的内容,而a属性中没有写权限  
-bash: file1: Operation not permitted
[root@Der_Tencent test]# echo "Input data" >> file1 #在file1中添加数据
[root@Der_Tencent test]# cat file1         
Input data
[root@Der_Tencent test]# chattr -a file1    #去除a权限
[root@Der_Tencent test]# chattr +i file1    #增加i权限
[root@Der_Tencent test]# echo "Input data" >> file1 #不能添加
-bash: file1: Permission denied
[root@Der_Tencent test]# rm file1     #不能删除
rm: remove regular file ‘file1’? y
rm: cannot remove ‘file1’: Operation not permitted
[root@Der_Tencent test]# chattr -i file1  
[root@Der_Tencent test]# echo "Input data again" >> file1   #去除权限之后便能添加
[root@Der_Tencent test]# cat file1
Input data
Input data again

文件默认权限:umask

文件权限的反向掩码,用于指定创建目录或文件时的默认值;即创建文件时减去的权限

  • umask:查看当前umask;
  • umask MASK:设置umask

文件默认权限:-rw-rw-rw-,即666;

目录默认权限:drwxrwxrwx,即777;

[root@Der_Tencent test]# umask  #查看当前掩码,为减去写权限,每一位对应的属性:特殊权限+属主+属组+其他用户,0表示不修改
0022
[root@Der_Tencent test]# touch test1
[root@Der_Tencent test]# ls -al test1
-rw-r--r-- 1 root root 0 Sep 23 17:15 test1 #文件的默认权限减去w权限

[root@Der_Tencent test]# mkdir mydir
[root@Der_Tencent test]# ls -al mydir/
total 8
drwxr-xr-x 2 root root 4096 Sep 23 17:16 .  #目录的默认权限减去写权限

[root@Der_Tencent test]# umask 055      #修改掩码,减去r和x权限
[root@Der_Tencent test]# touch test2
[root@Der_Tencent test]# ls -al test2
-rw--w--w- 1 root root 0 Sep 23 17:17 test2 #文件默认属性减去r,x(默认没有则不改变),此处勿用数字相减,否则容易出错。
  • 勿用数字相减,转换为对应的权限来计算;如上示例,文件的属性为:(-rw-rw-rw-)-(---r-xr-x)=-rw--w--w-  (注意第一个‘ - ’代表文件的类型)

猜你喜欢

转载自www.cnblogs.com/der1128/p/11573580.html