第六章  文件系统权限


学习目标:

能够解析Linux文件系统权限的工作机制

掌握实用命令工具修改文件权限和属主

掌握特权位和粘贴位权限,用户权限掩码


6.1 了解基本的文件系统权限


r  读权限: 对于文件,代表用户可以读取文件的内容

对于目录,代表用户可以对目录里的内容进行列表


w  写权限: 对于文件,代表用户可以修改文件的内容

对于目录,代表用户可以在目录里创建,删除文件,修改文件名字等


x  执行权限: 对于文件,如果这个文件本身是可执行文件(脚本,命令,程序),代表用户可以执行(运行)文件

对于目录,代表用户可以进入到该目录


一个用户要对目录有基本的正常访问操作,必须具备r和x权限


用户对资源来说,有三种角色:

User(u): 主人(文件所有者)

Group(g): 所属组的成员

Other(O): 其他人

首选判断用户是否为文件的所有者,如果是,就按所有者的权限进行访问

如果不是文件的所有者,就继续判断是否为文件的所有组成员,如果是,就按组的权限去访问

如果不是文件的所有组的成员,最终用户就是文件的其他的人,按其他人的权限去访问.


rwx,   rwx,    r-x

User   Group   Other




6.2  使用命令工具修改文件权限和属主


考试必考


相关命令: chmod,chown,chgrp


使用数字描述权限

--- 000 0

--x 001 1

-w- 010 2

-wx 011 3

r-- 100 4

r-x 101 5

rw- 110 6

rwx 111 7


rwxr-xr-x  ==> 755

rw-r--r--  ==> 644

r--------  ==> 400

rw-r--r--  ==> 644

rwxrwxrwx  ==> 777



实验: 在server上lab permissions setup

要求 ateam组的成员可以访问/home/ateam-text目录,并且可以在目录下创建,删除文件. 在目录下,andy创建的文件可以让alice修改

相关用户的密码为password


root # mkdir /home/ateam-text

root # chown :ateam /home/ateam-text

root # chmod g=rwx /home/ateam-text


andy $ touch /home/ateam-text/andy-file1

andy $ chmod o=rw /home/ateam-text/andy-file1

andy $ touch /home/ateam-text/andy-file2

andy $ chown :ateam  /home/ateam-text/andy-file2

andy $ chmod g=rw /home/ateam-text/andy-file2




6.3 掌握特权位和粘贴位权限,用户权限掩码


考试必考


u+s,g+s,o+t

umask


setuid = u+s = 4

文件的执行有效身份为文件的拥有者,而不是执行者的身份

setgid = g+s = 2

文件的有效执行组身份为文件的拥有组,而不是执行者的组身份 

目录里新建的文件的拥有组会自动继承目录的拥有组身份

sticky = o+t = 1

对目录有写权限的用户仅仅可以删除目录里属于自己的文件,不能删除其他用户的文件  tom传的文件mary不可删除,管理员不受限制



umask

用于设置默认权限的.

默认新建文件,最大权限为666

默认新建目录,最大权限是777

相关文件/etc/bashrc,/etc/profile,~/.bashrc,~/.bash_profile

[root@localhost ~]# umask

0022





练习:在server上lab permissions setup


系统会自动建立了三个用户,分别为curly,larry,moe,这些用户都是stooges组的成员,帐号密码都为password

要求以上用户和组可以在/home/stooges目录里访问,创建,删除文件. 其他用户一律不允许访问该目录. 在该目录下新建的文件会自动属于stooges组拥有

所有普通用户新建的文件或目录不能被非组成员(其他人)访问.





==================================================================================================================================================================

上课笔记:

6.1了解基本的文件系统权限

[student@localhost Desktop]$ file /usr/bin/touch

/usr/bin/touch: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xb8a26c6e357429bbb3ba545feaa789c2d36758fc, stripped


用户对资源来说有三种 :

用户(文件所有者

所属于的组(group)

other(其他人)

user group other 

首先判断文件是否是所有者,如果不是则判断是否所属的组,如果不是,则判定为other


看权限   ls -ld /tmp/test

student用户: sudo cho



修改权限的命令:chmod 777 /tmp/test



6.2 使用命令工具修改文件权限和属主


[student@localhost ~]$ cp /etc/hosts ./

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     Videos

Documents  hosts      Pictures  Templates

[student@localhost ~]$ ls -l

total 4

drwxr-xr-x. 2 student student   6 Jan  5  2015 Desktop

drwxr-xr-x. 2 student student   6 Jan  5  2015 Documents

drwxr-xr-x. 2 student student   6 Jan  5  2015 Downloads

-rw-r--r--. 1 student student 231 May 29 20:27 hosts

drwxr-xr-x. 2 student student   6 Jan  5  2015 Music

drwxr-xr-x. 2 student student   6 Jan  5  2015 Pictures

drwxr-xr-x. 2 student student   6 Jan  5  2015 Public

drwxr-xr-x. 2 student student   6 Jan  5  2015 Templates

drwxr-xr-x. 2 student student   6 Jan  5  2015 Videos

[student@localhost ~]$ chmod u+x,g+w,o-r ./hosts

[student@localhost ~]$ ls -l hosts

-rwxrw----. 1 student student 231 May 29 20:27 hosts

[student@localhost ~]$ 


[student@localhost ~]$ chmod go=rw ./hosts  不管以前是什么 ,执行后g和o都是rw

[student@localhost ~]$ chmod u-x,go=r ./hosts

[student@localhost ~]$ ls -l hosts

-rw-r--r--. 1 student student 231 May 29 20:27 hosts



dent@localhost ~]$ 

[student@localhost ~]$ mkdir test

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     test

Documents  hosts      Pictures  Templates  Videos

[student@localhost ~]$ cp /etc/passwd ./test/

[student@localhost ~]$ cd test

[student@localhost test]$ ls

passwd

[student@localhost test]$ ls -l

total 4

-rw-r--r--. 1 student student 2210 May 29 20:30 passwd

[student@localhost test]$ cd ..

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     test

Documents  hosts      Pictures  Templates  Videos

[student@localhost ~]$ ls ./test/

passwd

[student@localhost ~]$ ls ./test/ -l

total 4

-rw-r--r--. 1 student student 2210 May 29 20:30 passwd

[student@localhost ~]$ ls -ld ./test/

drwxrwxr-x. 2 student student 19 May 29 20:30 ./test/

[student@localhost ~]$ chmod g-w,o=---

chmod: missing operand after ‘g-w,o=---’

Try 'chmod --help' for more information.

[student@localhost ~]$ chmod g-w,o=--- ./test

[student@localhost ~]$ ls -ld ./test

drwxr-x---. 2 student student 19 May 29 20:30 ./test

[student@localhost ~]$ 

[student@localhost ~]$ chmod -R o=--- ./test

[student@localhost ~]$ ls -l ./test/passwd

-rw-r-----. 1 student student 2210 May 29 20:30 ./test/passwd

[student@localhost ~]$ cd home

bash: cd: home: No such file or directory

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     test

Documents  hosts      Pictures  Templates  Videos

[student@localhost ~]$ mkdri test

bash: mkdri: command not found...

Similar command is: 'mkdir'

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     test

Documents  hosts      Pictures  Templates  Videos

[student@localhost ~]$ mkdir  test

mkdir: cannot create directory ‘test’: File exists

[student@localhost ~]$ ls

Desktop    Downloads  Music     Public     test

Documents  hosts      Pictures  Templates  Videos




改用户和组命令


[root@localhost student]# chown daemon:root /home/student/hosts 同时改拥有者和组

[root@localhost student]# ls -l

total 4

drwxr-xr-x. 2 student student   6 Jan  5  2015 Desktop

drwxr-xr-x. 2 student student   6 Jan  5  2015 Documents

drwxr-xr-x. 2 student student   6 Jan  5  2015 Downloads

-rw-r--r--. 1 daemon  root    231 May 29 20:27 hosts

drwxr-xr-x. 2 student student   6 Jan  5  2015 Music

drwxr-xr-x. 2 student student   6 Jan  5  2015 Pictures

drwxr-xr-x. 2 student student   6 Jan  5  2015 Public

drwxr-xr-x. 2 student student   6 Jan  5  2015 Templates

drwxr-x---. 2 student student  19 May 29 20:30 test

drwxr-xr-x. 2 student student   6 Jan  5  2015 Videos

[root@localhost student]# 


[root@localhost student]# chown bin /home/student/hosts   改拥有者

[root@localhost student]# ls -l /home/student/hosts 

-rw-r--r--. 1 bin root 231 May 29 20:27 /home/student/hosts

[root@localhost student]# ls -lh /home/student/hosts 


[root@localhost student]# chown :daemon /home/student/hosts   改组

[root@localhost student]# ls -lh /home/student/hosts 

-rw-r--r--. 1 bin daemon 231 May 29 20:27 /home/student/hosts



[root@localhost student]# chgrp root /home/student/hosts 改组 




数字表示权限

--- 000 0

--x 001 1

-w- 010 2

-wx 011 3

r-- 100 4

r-x 101 5

rw- 110 6

rwx 111 7


读 写 执 基础数是   4   2   1 ,三者组合。



[root@localhost test]# chmod 755 /home/student/hosts 

[root@localhost test]# ls -lh !$

ls -lh /home/student/hosts

-rwxr-xr-x. 1 bin root 231 May 29 20:27 /home/student/hosts

[root@localhost test]# chmod 600 /home/student/hosts 

[root@localhost test]# ls -lh !$

ls -lh /home/student/hosts

-rw-------. 1 bin root 231 May 29 20:27 /home/student/hosts

[root@localhost test]# chmod 664 /home/student/hosts 

[root@localhost test]# ls -lh /home/student/hosts

-rw-rw-r--. 1 bin root 231 May 29 20:27 /home/student/hosts






实验: 在server上lab permissions setup

要求 ateam组的成员可以访问/home/ateam-text目录,并且可以在目录下创建,删除文件. 在目录下,andy创建的文件可以让alice修改

相关用户的密码为password


root # mkdir /home/ateam-text

root # chown :ateam /home/ateam-text

root # chmod g=rwx /home/ateam-text


andy $ touch /home/ateam-text/andy-file1

andy $ chmod o=rw /home/ateam-text/andy-file1  改这个文件的其他权限

andy $ touch /home/ateam-text/andy-file2

[root@localhost ateam-text]# chgrp student ./tom-file2  把这个改成其他的所属组


如上 student用户登陆可以修改 tom创建的两个文件。

总结:如上,A用户的目录或文件,有两种方式可以让其他用户获取修改的权限,一种是修改o的权限,一种是修改文件所属的组。






6.3 掌握特权位和粘贴位权限,用户权限掩码



[student@localhost /]$ ls -lh /usr/bin/passwd

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

[student@localhost /]$ ls -lh /bin/touch

-rwxr-xr-x. 1 root root 61K Jan 25  2014 /bin/touch

[student@localhost /]$ 


如上,特权位s

[root@localhost ~]# chmod u-s /usr/bin/passwd  将其减掉后变成了w

设定特权位 chmod 4775 /usr/bin/passwd

好处:在执行命令的有效身份是文件的拥有者root,所以任何人在执行这个文件时的权限 不是任何人,而是拥有者的权限。



组位加特权位:

[root@localhost ~]# chmod g+s /usr/bin/cat  运行组身份

[root@localhost ~]# chmod u+s /usr/bin/cat  运行者身份




umask

用于设置默认权限的.

默认新建文件,最大权限为666   用它来减去umask的值,查看源文件可知,一般和管理员的默认umask是不同的。

默认新建目录,最大权限是777  用它来减去umask的值

相关文件/etc/bashrc,/etc/profile,~/.bashrc,~/.bash_profile

[root@localhost ~]# umask

0022

[root@localhost ~]# umask

0022

[root@localhost ~]# ^C

[root@localhost ~]# mkdir 1  

[root@localhost ~]# ls -l

total 12

drwxr-xr-x. 2 root root    6 May 29 22:55 1

-rw-------. 1 root root 8619 May  7  2014 anaconda-ks.cfg

drwxr-xr-x. 2 root root    6 Dec 30  2014 Desktop

drwxr-xr-x. 2 root root    6 Dec 30  2014 Documents

drwxr-xr-x. 2 root root    6 Dec 30  2014 Downloads

drwxr-xr-x. 2 root root    6 Dec 30  2014 Music

drwxr-xr-x. 2 root root    6 Dec 30  2014 Pictures

drwxr-xr-x. 2 root root    6 Dec 30  2014 Public

drwxr-xr-x. 2 root root    6 Dec 30  2014 Templates

drwxr-xr-x. 2 root root    6 Dec 30  2014 Videos

[root@localhost ~]# umask 0777    改UMASK

[root@localhost ~]# mkdir 2

[root@localhost ~]# ls -l 2

total 0

[root@localhost ~]# ls -l

total 12

drwxr-xr-x. 2 root root    6 May 29 22:55 1

d---------. 2 root root    6 May 29 22:56 2

-rw-------. 1 root root 8619 May  7  2014 anaconda-ks.cfg

drwxr-xr-x. 2 root root    6 Dec 30  2014 Desktop

drwxr-xr-x. 2 root root    6 Dec 30  2014 Documents

drwxr-xr-x. 2 root root    6 Dec 30  2014 Downloads

drwxr-xr-x. 2 root root    6 Dec 30  2014 Music

drwxr-xr-x. 2 root root    6 Dec 30  2014 Pictures

drwxr-xr-x. 2 root root    6 Dec 30  2014 Public

drwxr-xr-x. 2 root root    6 Dec 30  2014 Templates

drwxr-xr-x. 2 root root    6 Dec 30  2014 Videos

[root@localhost ~]# umsdk 0002

bash: umsdk: command not found...

[root@localhost ~]# umask 0002  改UMASK

[root@localhost ~]# mkdir 3

[root@localhost ~]# ls -l

total 12

drwxr-xr-x. 2 root root    6 May 29 22:55 1

d---------. 2 root root    6 May 29 22:56 2

drwxrwxr-x. 2 root root    6 May 29 22:56 3

-rw-------. 1 root root 8619 May  7  2014 anaconda-ks.cfg

drwxr-xr-x. 2 root root    6 Dec 30  2014 Desktop

drwxr-xr-x. 2 root root    6 Dec 30  2014 Documents

drwxr-xr-x. 2 root root    6 Dec 30  2014 Downloads

drwxr-xr-x. 2 root root    6 Dec 30  2014 Music

drwxr-xr-x. 2 root root    6 Dec 30  2014 Pictures

drwxr-xr-x. 2 root root    6 Dec 30  2014 Public

drwxr-xr-x. 2 root root    6 Dec 30  2014 Templates

drwxr-xr-x. 2 root root    6 Dec 30  2014 Videos

[root@localhost ~]# 


用umask来干预用户创建的用户目录和文件,只对当前命令生效

普通是0002,管理员是0022,


系统会自动建立了三个用户,分别为curly,larry,moe,这些用户都是stooges组的成员,帐号密码都为password

要求以上用户和组可以在/home/stooges目录里访问,创建,删除文件. 其他用户一律不允许访问该目录. 在该目录下新建的文件会自动属于stooges组拥有

所有普通用户新建的文件或目录不能被非组成员(其他人)访问.