linux下目录与文件的权限及特殊权限

一、Linux下目录与文件的权限

chown    更改文件或目录的所属主,所属组    

/etc/passwd 配置文件存放用户信息

useradd    添加用户

chown 所属主:所属组 文件或目录    

chown -R    更改目录及目录下面的文件所有权限

chown -v    可视化显示权限更改

[root@localhost tmp]# chown user1:root aa
[root@localhost tmp]# ls -l
drwxr-xr--. 2 user1 root 4096 Mar 18 16:41 aa
-rw-------. 1 root  root    0 Nov 13 09:25 yum.log
[root@localhost tmp]# ls -l aa/
-rw-r--r--. 1 root root 0 Mar 18 16:41 bb
[root@localhost tmp]# chown -R user1:user1 aa
[root@localhost tmp]# ls -ld aa/
drwxr-xr--. 2 user1 user1 4096 Mar 18 16:41 aa/
[root@localhost tmp]# ls -l aa/
-rw-r--r--. 1 user1 user1 0 Mar 18 16:41 bb


chmod    更改rwx权限    r=4    w=2    x=1    

chmod -R    更改目录及目录下面的文件权限

chmod -v    可视化显示权限更改

[root@localhost tmp]# mkdir aa
[root@localhost tmp]# ls -ld aa/
drwxr-xr-x. 2 root root 4096 Mar 18 16:46 aa/
[root@localhost tmp]# touch aa/bb
[root@localhost tmp]# ls -l aa/
-rw-r--r--. 1 root root 0 Mar 18 16:46 bb
[root@localhost tmp]# chmod -Rv 666 aa
mode of `aa' changed to 0666 (rw-rw-rw-)
mode of `aa/bb' changed to 0666 (rw-rw-rw-)

umask    指定目录或文件的默认权限root用户是022,普通用户是002

更改umask值    umask 001    

[root@localhost ~]# umask
0022
[root@localhost ~]# umask 001
[root@localhost ~]# umask
0001
[root@localhost ~]# mkdir aa
[root@localhost ~]# touch bb
[root@localhost ~]# ls -l
drwxrwxrw-. 2 root root 4096 Mar 18 16:26 aa
-rw-rw-rw-. 1 root root    0 Mar 18 16:26 bb

如果更改umask值为001,目录权限为776  rwxrwxrw- 文件权限为666  rw-rw-rw- 文件根本不需要x权限,所以权限还是不变。


root下默认目录为755 rwxr-xr-x,文件为644    rw-r--r--
目录最大权限为777    rwxrwxrwx    777

                     ----w--w-    022

                     rwxr-xr-x    755

文件最大权限为666    rw-rw-rw-    666

                     ----w--w-    022

                     rw-r--r--    644

一个目录必须具有rx权限,可以进入目录 cd,可以列出目录内容 ls 

目录若没有x执行权限,我们不能cd到这个目录下,但是可以ls这个目录。

[root@localhost tmp]# mkdir aa
[root@localhost tmp]# ls -ld aa/
drwxr-xr-x. 2 root root 4096 Mar 18 16:33 aa/
[root@localhost tmp]# chmod 754 aa
[root@localhost tmp]# ls -ld aa/
drwxr-xr--. 2 root root 4096 Mar 18 16:33 aa/
[root@localhost tmp]# su - user1
[user1@localhost ~]$ cd /tmp/
[user1@localhost tmp]$ cd aa
-bash: cd: aa: Permission denied
[user1@localhost tmp]$ ls -ld aa/
drwxr-xr--. 2 root root 4096 Mar 18 16:33 aa/

普通用户默认目录为775 rwxrwxr-x,文件为664    rw-rw-r--


二、修改文件、目录的特殊权限    

chattr

+i    增加后,文件不能被删除、重命名、设定连接、写入、新增数据

[root@localhost tmp]# chattr +i aa
[ root@localhost tmp]# touch aa/cc 
touch: cannot touch `aa/cc': Permission denied 
[ root@localhost tmp]# chattr -i aa 
[ root@localhost tmp]# touch aa/cc 
[ root@localhost tmp]# chattr +i aa 
[ root@localhost tmp]# rm -rf aa/cc 
rm: cannot remove `aa/cc': Permission denied 
[ root@localhost tmp]# mv aa bb 
mv: cannot move `aa' to `bb': Operation not permitted

+a    增加该属性后,只能追加不能删除,非root用户不能修改属性

文件只能追加,目录下可以建立子目录与文件,可以写文件,不能删除子目录和文件,也不能改名。

[ root@localhost tmp]# touch 11 
[ root@localhost tmp]# ls -l
 -rw-r--r--. 1 root  root    0 Mar 18 17:10 11 
 [ root@localhost tmp]# echo 'abc'>11 
 [ root@localhost tmp]# cat 11 abc 
 [ root@localhost tmp]# chattr +a 11 
 [ root@localhost tmp]# echo 'def'>11
 bash: 11: Operation not permitted 
 [ root@localhost tmp]# echo 'def'>>11
 [ root@localhost tmp]# cat 11 abc def
[ root@localhost tmp]# chmod 666 11
chmod: changing permissions of `11': Operation not permitted 
[ root@localhost tmp]# chown user1:root 11
 chown: changing ownership of `11': Operation not permitted
[ root@localhost tmp]# rm -rf 11
 rm: cannot remove `11': Operation not permitted


lsattr    查看文件或目录的特殊权限

[root@localhost tmp]# lsattr
-------------e- ./yum.log
----i--------e- ./aa

lsattr -d    查看目录的特殊属性

[root@localhost tmp]# lsattr -d aa 
-----a-------e- aa

lsattr -R    列出当前目录及子目录的特殊属性

[ root@localhost tmp]# lsattr -R 
-------------e- ./yum.log 
-----a-------e- ./aa 
./aa: 
-------------e- ./aa/bb 
-------------e- ./aa/cc

set_uid 使普通用户临时拥有执行这条命令所属主的权限

set_uid=4    set_gid=2     stick_bit=1

[ root@localhost ~]# umask 0022
[ root@localhost ~]# which passwd /usr/bin/passwd
[ root@localhost ~]# ls -l /usr/bin/passwd
 - rwsr-xr-x. 1 root root 25980 2月  22 2012 /usr/bin/passwd

 rws=rwx+s    小s说明文件具有rwx的权限 大S说明文件没有x权限 

passwd所属主为root用户,有了rws权限后普通用户也具有root的权限,所以普通用户可以自己更改密码。

执行者必须对文件拥有x权限(否则即使设置了SUID,由于你对文件根本无法执行SUID此时也无效,此时权限位显示S);

举例说明:

[ root@localhost tmp]# chmod u-sx /usr/bin/passwd  
[ root@localhost tmp]# ls -l /usr/bin/passwd  
-rw-r-xr-x. 1 root root 25980 Feb 22  2012 /usr/bin/passwd
[root@localhost ~]# chmod u+s /usr/bin/passwd 
[root@localhost ~]# ls -l /usr/bin/passwd
-rwSr-xr-x. 1 root root 25980 2月  22 2012 /usr/bin/passwd

大写S 说明文件没有x权限,不能执行,没有意义。


chmod u-s /usr/bin/passwd    可执行的文件、二进制的才可以设置s权限;

[ root@localhost tmp]# chmod u-s /usr/bin/passwd  
[ root@localhost tmp]# ls -l /usr/bin/passwd  
-rwxr-xr-x. 1 root root 25980 Feb 22  2012 /usr/bin/passwd

u-s 后文件具有rwx权限

[ root@localhost ~]# ls -l /usr/bin/passwd  
-rwsr-xr-x. 1 root root 25980 2月  22 2012 /usr/bin/passwd
[ root@localhost tmp]# chmod 755 /usr/bin/passwd  
[ root@localhost tmp]# ls -l /usr/bin/passwd  
-rwxr-xr-x. 1 root root 25980 Feb 22  2012 /usr/bin/passwd 
[ root@localhost tmp]#  chmod 4755 /usr/bin/passwd 
[ root@localhost tmp]# ls -l /usr/bin/passwd  
-rwsr-xr-x. 1 root root 25980 Feb 22  2012 /usr/bin/passwd

本身passwd是755 ,最前面加了suid 这个值为4,所以这个passwd的权限是4755

[ root@localhost ~]# chmod 6755 /usr/bin/passwd 
[ root@localhost ~]# ls -l /usr/bin/passwd  
-rwsr-sr-x. 1 root root 25980 2月  22 2012 /usr/bin/passwd

6是代表suid4+sgid2=6 然后本身的权限是755,所以是6755


set_gid    使普通用户临时具有文件所属组的权限,可以作用于文件和目录

chmod g+s  目录名

更改完成后在目录下面新建立的目录都是,父目录所属组的属性

新建立的目录拥有父目录所属组的权限,继承父目录的。

[ root@localhost tmp]# mkdir aa 
[ root@localhost tmp]# ls -ld aa/ 
drwxr-xr-x. 2 root root 4096 Mar 19 10:04 aa/
[root@localhost tmp]# chown user1:user1 aa
[root@localhost tmp]# ls -ld aa/
drwxr-xr-x. 3 user1 user1 4096 Mar 19 10:05 aa/
[root@localhost tmp]# chmod  g+s aa/
[ root@localhost tmp]# ls -ld aa/ 
drwxr-sr-x. 4 user1 user1 4096 Mar 19 10:07 aa/
[root@localhost tmp]# mkdir aa/cc
[ root@localhost tmp]# ls -l aa 
drwxr-sr-x. 2 root user1 4096 Mar 19 10:06 cc 
[ root@localhost tmp]# touch aa/1.txt 
[ root@localhost tmp]# ls -l aa/1.txt  
-rw-r--r--. 1 root user1 0 Mar 19 10:07 aa/1.txt
[ root@localhost tmp]# chmod g-s aa/ 
[ root@localhost tmp]# ls -ld aa 
drwxr-xr-x. 4 user1 user1 4096 Mar 19 10:07 aa 
[ root@localhost tmp]# mkdir aa/dd 
[ root@localhost tmp]# ls -l aa/ 
drwxr-sr-x. 2 root user1 4096 Mar 19 10:06 cc 
drwxr-xr-x. 2 root root  4096 Mar 19 10:10 dd

去掉guid的s权限后,在目录创建的目录不会继承父目录的属性;


sticky_bit     防删除位,rwt 属性只有创建者可以删除自己的,其他用户不可以删除。root具有至高无上的权限都可以删除。

chmod o+t 目录名

[ root@localhost ~]# ls -ld /tmp/ 
drwxrwxrwt. 3 root root 4096 3月  19 19:30 /tmp/
[ root@localhost tmp]# mkdir 11
[ root@localhost tmp]# chmod 777 11 
[ root@localhost tmp]# ls -ld 11/ 
drwxrwxrwx 2 root root 4096 3月  19 19:40 11/ 
[ root@localhost tmp]# chmod o+t 11/ 
[ root@localhost tmp]# ls -ld 11/ 
drwxrwx rwt 2 root root 4096 3月  19 19:40 11/

目录必须有x权限才可以进去,小写t说明目录有x权限,T说明没有x权限,文件是T显示的。

[ root@localhost 11]# su - test1 
[ test1@localhost ~]$ cd /tmp/11/ 
[ test1@localhost 11]$ mkdir 12
[ test1@localhost 11]$ touch 123 
drwxrwxr-x 2 test1 test1 4096 3月  19 20:14 12 
-rw-rw-r-- 1 test1 test1    0 3月  19 20:15 123 
drwxr-xr-x 2 root  root  4096 3月  19 20:14 root
[ root@localhost 11]# su - test2 
[ test2@localhost ~]$ cd /tmp/11/ 
[ test2@localhost 11]$ ls -l 
drwxrwxr-x 2 test1 test1 4096 3月  19 20:14 12 
-rw-rw-r-- 1 test1 test1    0 3月  19 20:15 123 
drwxr-xr-x 2 root  root  4096 3月  19 20:14 root 
[ test2@localhost 11]$ rm -rf 12 
rm: 无法删除"12": 不允许的操作 
[ test2@localhost 11]$ rm -rf 123 
rm: 无法删除"123": 不允许的操作 
[ test2@localhost 11]$ touch 234.txt 
[ test2@localhost 11]$ ls -l 
drwxrwxr-x 2 test1 test1 4096 3月  19 20:14 12 
-rw-rw-r-- 1 test1 test1    0 3月  19 20:15 123 
-rw-rw-r-- 1 test2 test2    0 3月  19 20:16 234.txt 
drwxr-xr-x 2 root  root  4096 3月  19 20:14 root

可以创建目录或文件,但是不能删除其他用户的目录或文件。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值