原创作品,允许转载,转载时请务必以超链接形式标明文章 、作者信息和本声明。否则将追究法律责任。            

                   

本节所讲内容:

      文件基本权限

      文件高级权限

      实战-创建一个让root都无法删除的***文件

 

查看文件权限

[root@localhost ~]#touch liulang

[root@localhost ~]# llliulang

-rw-r--r-- 1 root root0 Nov 30 20:35 liulang

 

文件基本权限

  rwx    r-x       r-x      user1  user1  FILENAME

类型  拥有者的权限 所属组的权限 其他人的权限 拥有者  属组   对象

 

对于文件:r读  w写  x执行

对于目录:r读(看到目录里面有什么)       cat   more  less   ls

     w建文件、删除、移动                  touch   mkdir   rm   mv  cp

     x进入                                        cd

 

修改权限的相关命令:

chmod      

作用:修改文件权限

u-w               user                      拥有者

g+x               group                  

o=r                other                    其他人

a+x               all                        所有人

 

[root@localhost ~]#touch  tuluo

[root@localhost ~]# lltuluo

-rw-r--r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod u+x tuluo

[root@localhost ~]# lltuluo

-rwxr--r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod g-r tuluo

[root@localhost ~]# lltuluo

-rwx---r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod o=w tuluo

[root@localhost ~]# lltuluo

-rwx----w- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod a+x tuluo

[root@localhost ~]# lltuluo

-rwx--x-wx 1 root root0 Nov 30 20:45 tuluo

 

chmod g+/-/=  rwx   用法同上

 

修改目录的权限

[root@localhost ~]#mkdir fengchen

[root@localhost ~]# ll-d fengchen/

drwxr-xr-x 2 root root6 Nov 30 20:55 fengchen/

[root@localhost ~]#chmod u-w fengchen/

[root@localhost ~]# ll-d fengchen/

dr-xr-xr-x 2 root root6 Nov 30 20:55 fengchen/

[root@localhost ~]#chmod g+w fengchen/

[root@localhost ~]# ll-d fengchen/

dr-xrwxr-x 2 root root6 Nov 30 20:55 fengchen/

 

修改other权限:

chmod o+/-/=  r 、w 、 x  用法同上

chmod  o+/-/= rwx 用法同上

chmod a+/-/= rwx  用法同上

chmod  +/-/= rwx  用法同上  #不写a也可以

 

使用数字表示权限

  rwx    r-x        r-x    user1  user1  FILENAME

类型  拥有者的权限 所属组的权限 其他人的权限 属主  属组 

 

rwx

r--      -w-   --x

100     010     001                    二进制                   进制转换器

4            2            1                   十进制

 

rw-    的值是多少?                    4+2=6

r-x                                             4+1=5

rwxr-xr-x 的值是多少?rwx=4+2+1=7          r-x=4+1=5  r-x=4+1=5     rwxr-xr-x=755 

 

[root@localhost ~]#touch  longyu

[root@localhost ~]# lllongyu

-rw-r--r-- 1 root root0 Nov 30 21:00 longyu

[root@localhost ~]#chmod 633 longyu

[root@localhost ~]# lllongyu

-rw--wx-wx 1 root root0 Nov 30 21:00 longyu

 

chown

作用:修改文件拥有者和所属组

语法:chown USER:GROUP 对象

chown USER 对象

chown :GROUP 对象

-R 递归(目录下的所有内容全部更改,否则只修改目录)

[root@localhost ~]#touch eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 root root0 Nov 30 21:05 eshao

[root@localhost ~]#useradd eshao

[root@localhost ~]#chown eshao:bin eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 eshao bin0 Nov 30 21:05 eshao

[root@localhost ~]#chown daemon eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 daemon bin0 Nov 30 21:05 eshao

[root@localhost ~]#chown :nobody eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 daemonnobody 0 Nov 30 21:05 eshao

 

-R递归

[root@localhost ~]#chown -R :daemon alen/

[root@localhost ~]# llalen/

total 0

-rw-r--r-- 1 rootdaemon 0 Nov 30 21:10 alen.txt

-rw-r--r-- 1 rootdaemon 0 Nov 30 21:10 eshao.txt

 

一个文件只有读的权限,拥有者是否可以写这个文件?   文件所有者一定可以写文件

[root@localhost~]# useradd yunwu

[root@localhost ~]#touch huiji

[root@localhost ~]# cphuiji  /home/yunwu/

[root@localhost ~]# su- yunwu

[yunwu@localhost ~]$vim huiji

结果:可以正常写入内容

 

设置文件默认权限的补码:

系统用户:#umask022

普通用户:#umask002

 

计算方法:

文件默认权限=666-umask值  666-022=644

目录默认权限=777-umask 值  777-022=755

#这是一个好的记忆方法,但不严谨。

 

umask掩码为033 . 666-033=633  结果为: 644

计算方法:

6  6 6            umask   0  3   3

110 110110                 000 011  011  |  取反

                             111 100  100   \/

110 110110   与  111100  100

    |        /

110 100 100

6   4   4

 

了解:

/etc/bashrc

if [ $UID -gt 199 ]&& [ "`id -gn`" = "`id -un`" ]; then  # id -gn显示组名,id -un 显示用户名

        umask 002   #普通用户

else

        umask 022   #系统用户

fi

 

特殊权限:

SUID               SGID                 Stickybit

s对应的数值为:u 4,g  2,o   1

SUID

限定:只能设置在二进制可执行程序上面。对目录文本设置无效。

功能:程序运行时的权限从执行者变更成程序所有者。

 

[root@localhost ~]# ll/usr/bin/passwd

-rwsr-xr-x. 1 root root27832 Jan 30  2014 /usr/bin/passwd

[jiangfeng@localhost~]$ less /etc/shadow

/etc/shadow: Permissiondenied

 

[root@localhost ~]#chmod u+s /usr/bin/less

[root@localhost ~]# ll /usr/bin/less

-rwsr-xr-x. 1 root root158240 Feb  4  2014 /usr/bin/less

验证:

[jiangfeng@localhost~]$ less /etc/shadow                  成功读取

 

注意:

[root@localhost ~]#chmod u+s /usr/bin/less                            等同于

[root@localhost ~]#chmod 4755 /usr/bin/less

 

 

SGID

限定:既可以给二进制可执行程序设置,也可以给目录设置。

功能:在设置了SGID权限的目录下建立文件时,新创建的文件的所属组会继承上级目录的所属组

 

[root@localhost ~]#chown :bin test/

[root@localhost ~]# ll-d test/

drwxr--r-- 2 root bin 6Nov 30 21:55 test/

[root@localhost ~]# cdtest/

[root@localhost test]#touch a.txt

[root@localhost test]#ll a.txt

-rw-r--r-- 1 root root0 Nov 30 21:56 a.txt

 

[root@localhost ~]#chmod g+s test/

[root@localhost ~]# ll-d test/

drwxr-Sr-- 2 root bin30 Nov 30 21:57 test/

验证:

[root@localhost ~]# cdtest/

[root@localhost test]# touchb.txt

[root@localhost test]#ll b.txt

-rw-r--r-- 1 root bin 0Nov 30 21:57 b.txt

 

Stickybit

限定:只作用于目录

功能:目录下创建的文件只有root、文件创建者、目录所有者才能删除

 

[root@localhost ~]# ll-d /tmp/

drwxrwxrwt. 14 rootroot 4096 Nov 30 21:59 /tmp/

 

[root@localhost ~]#mkdir /share

[root@localhost ~]#chmod 777 /share/

[root@localhost ~]# ll-d /share/

drwxrwxrwx 2 root root6 Nov 30 22:00 /share/

[root@localhost ~]#chmod o+t /share/

[root@localhost ~]# ll-d /share/

drwxrwxrwt 2 root root6 Nov 30 22:00 /share/

 

[root@localhost ~]# su- eshao

[eshao@localhost ~]$ cd/share/

[eshao@localhostshare]$ ls

jiangfeng.txt

[eshao@localhostshare]$ rm -rf jiangfeng.txt

rm: cannot remove‘jiangfeng.txt’: Operation not permitted

[eshao@localhostshare]$ echo  aaa > jiangfeng.txt

 

扩展ACL

[root@localhost ~]#touch b.txt

 

查看

[root@localhost ~]#getfacl  b.txt

# file: b.txt

# owner: root

# group: root

user::rw-

group::r--

other::r--

 

设置

[root@localhost ~]#setfacl -m u:eshao:rwx b.txt

[root@localhost ~]#getfacl  b.txt

# file: b.txt

# owner: root

# group: root

user::rw-

user:eshao:rwx

group::r--

mask::rwx

other::r--

 

对目录进行设置

[root@localhost ~]#setfacl -R -m u:jiangfeng:rw- test

 (-R一定要在-m前面,表示目录下所有文件)

[root@localhost ~]#getfacl !$

getfacl test

# file: test

# owner: root

# group: bin

# flags: -s-

user::rwx

user:jiangfeng:rw-

group::r--

mask::rw-

other::r--

 

删除acl      

[root@localhost ~]#setfacl -x u:eshao b.txt                        #删除单个用户的权限

[root@xuegod163~]# setfacl -b b.txt                                  #删除所有acl权限

 

      实战-创建一个让root都无法删除的***文件

 

REHL6 Linux文件系统扩展属性:chattr  lsattr

+a  只能追加内容

+i  不能被修改

 

[root@localhost ~]#touch xiaobai

[root@localhost ~]#echo xiaobaihaoniubi  > xiaobai

[root@localhost ~]# catxiaobai

xiaobaihaoniubi

[root@localhost ~]#chattr +a xiaobai

[root@localhost ~]#echo xiaobaizhendehaoniubi > xiaobai

-bash: xiaobai:Operation not permitted

[root@localhost ~]#echo xiaobaizhendehaoniubi >> xiaobai

[root@localhost ~]# catxiaobai

xiaobaihaoniubi

xiaobaizhendehaoniubi

[root@localhost ~]# rm-rf xiaobai

rm: cannot remove‘xiaobai’: Operation not permitted

 

[root@localhost ~]#chattr +i xiaobai

[root@localhost ~]#echo xiaobaizhendehaoniubima? >> xiaobai

-bash: xiaobai:Permission denied

[root@localhost ~]# rm-rf xiaobai

rm: cannot remove‘xiaobai’: Operation not permitted

 

[root@localhost ~]#lsattr xiaobai

----ia----------xiaobai

 

[root@localhost ~]#lsattr xiaobai

----ia----------xiaobai

[root@localhost ~]#chattr -i xiaobai

[root@localhost ~]#chattr -a xiaobai

[root@localhost ~]#lsattr xiaobai

----------------xiaobai