Table of Contents
一.如何查看及读取权限信息
1.查看文件权限信息
ls -l filename
2.查看目录权限信息
ls -ld directory
例如:
[dw@localhost Desktop]$ ls -l file
-rw-rw-r--. 1 dw dw 0 Jan 6 15:49 file
[dw@localhost Desktop]$ ls -ld test/
drwxrwxr-x. 2 dw dw 6 Jan 6 15:49 test/
3. 权限信息各字段的解释
各字段 | - | rw-r--r--. | 1 | dw | dw | 1 | Dec 28 19:45 | file |
文件 | 类型 | 权限 | 文件硬连接个数 | 所属用户名 | 所属组 | 文件大小 | 文件最后一次更改的时间 | 文件名 |
目录 | 类型 | 权限 | 目录中子目录个数 | 所属用户名 | 所属组 | 目录中子文件数据大小 | 目录中内容最后一次被修改的时间 | 目录名 |
注:其中权限分为用户的、组的、普通用户的,r为读,w为写,x为执行
二.文件拥有者及拥有组
1.用户对于文件的身份划分
分为:用户拥有者(user) 文件所属组(group) 其他人(other)
2.更改文件拥有者及拥有组的方法
更改文件拥有者及拥有组只有root用户才能修改
命令为:
chown 用户名 文件名
chgrp 组名称 文件名
chown -R 用户名 文件名
chgrp -R 组名称 文件名
举例:
chown student /mnt/westos1 更改文件所属用户
chgrp student /mnt/westos2 更改文件所属组
chown student.student /mnt/westos3 同时更改文件所属用户与所属组
chown root:root /mnt/westos3 更改更改文件所属用户与所属组
chown -R student /mnt/westosdir/ 更改文件夹及所属文件的所属用户(没有-R,则只更改文件夹,
其下文件不变)
[root@localhost 桌面]# touch file
[root@localhost 桌面]# mkdir test
[root@localhost 桌面]# ls -l file
-rw-r--r--. 1 root root 0 1月 6 17:16 file
[root@localhost 桌面]# chown student file
[root@localhost 桌面]# chgrp student file
[root@localhost 桌面]# ls -l file
-rw-r--r--. 1 student student 0 1月 6 17:16 file
[root@localhost 桌面]# touch test/file1
[root@localhost 桌面]# ls -ld test/
drwxr-xr-x. 2 root root 19 1月 6 17:18 test/
[root@localhost 桌面]# ls -lR test/
test/:
总用量 0
-rw-r--r--. 1 root root 0 1月 6 17:18 file1
[root@localhost 桌面]# chown student test/
[root@localhost 桌面]# ls -l -d test/
drwxr-xr-x. 2 student root 19 1月 6 17:18 test/
[root@localhost 桌面]# ls -lr test/
总用量 0
-rw-r--r--. 1 root root 0 1月 6 17:18 file1
[root@localhost 桌面]# chgrp -R student test/
[root@localhost 桌面]# ls -l -d test/
drwxr-xr-x. 2 student student 19 1月 6 17:18 test/
[root@localhost 桌面]# ls -lr test/
总用量 0
-rw-r--r--. 1 root student 0 1月 6 17:18 file1
三.文件权限的理解
1.文件权限划分
文件权限可以分为三个部分,每三位分别对应于用户拥有者、所有组、其他用户的读、写和执行权限
2.权限类型:
- 表示权限关闭
r readable | w writeable | x excutable | |||
---|---|---|---|---|---|
对于文件 可查看文件中的内容 | 对于目录 可列出目录中的文件名称 | 对于文件 可更改文件中的内容 | 对于目录 可在其下新建和删除文件或目录
| 对于文件 可用文件名调用文件中记录的程序 | 对于目录 可进入目录 |
四.文件权限设定方式
文件权限设定用chmod 命令,分为三种用法方式。
1.字符方式
chmod <u|g|o><+|-|=><r|w|x> 目标
其中+表示开放权限,-表示关闭权限,=表示设定最终权限结果。
[root@localhost 桌面]# touch file
[root@localhost 桌面]# ll file
-rw-r--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod u+x file
[root@localhost 桌面]# ll file
-rwxr--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod g-r file
[root@localhost 桌面]# ll file
-rwx---r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod o=rw file
[root@localhost 桌面]# ll file
-rwx---rw-. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod go=r file
[root@localhost 桌面]# ll file
-rwxr--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod -r file
[root@localhost 桌面]# ll file
--wx------. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod u-w,go+r file
[root@localhost 桌面]# ll file
---xr--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod a-x file
[root@localhost 桌面]# ll file
----r--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod +w file
[root@localhost 桌面]# ll file
--w-r--r--. 1 student student 0 1月 8 17:15 file
[root@localhost 桌面]# chmod +x file
[root@localhost 桌面]# ll file
--wxr-xr-x. 1 student student 0 1月 8 17:15 file
注:其中a 表示对全部操作,如果前面不指定对象的话,r和x权限会对u、g、o都进行操作,但是w权限只会对u进行操作,此时可以用到a来表示对全部进行操作。
2.数字方式
权限可以用一个八进制数来表示
权限 | 二进制 | 八进制 |
--- | 000 | 0 |
--x | 001 | 1 |
-w- | 010 | 2 |
r-- | 100 | 4 |
-wx | 011 | 3 |
r-x | 101 | 5 |
rw- | 110 | 6 |
rwx | 111 | 7 |
这样命令就可以写成:
chmod 数字 文件名
例如:
注:每一位数字分别对应对u、g、o的权限状态的二进制和。
3.权限复制
复制方式:chmod --reference=属性源文件 TAG
例如:
五.系统预留权限阀直
1.对于权限预留阀直的理解
1)资源存在意义在于共享,权限开放越大,共享效果越明显,但是安全性越差。
2)对于系统安全而言,开放权力越小,系统越安全。
3)在系统中开放应开放的权力,保留不安全的权力以确保系统功能及安全性。
2.权限预留阀直的设定
我们可以用umask 命令来查看并更改系统预留阀直。
例如:
这样只是更改当前shell下的直,要想永久更改,需要如下改变两个配置文件。
/etc/bashrc shell 配置文件 和 /etc/profile 系统环境配置文件
例如:
打开 /etc/bashrc 如下
更改里面的默认预留阀直
注:这里if下面的是uid大于199的用户,else 下为其余的用户,这里我们是root用户,故更改else下面的直。
对/etc/profile的更改同上。
之后重新打开shell,可以看到变化:
六.特殊权限
1.对特殊权限的理解
SUID | 只针对二进制可执行文件,任何用户使用拥有suid权限的文件发起其中记录的程序时都以文件拥有者的身份去执行。 |
SGID | 针对二进制可执行文件:不同用户使用拥有该权限的文件发起的程序都是以所拥有组的身份去执行。 |
针对目录:目录新建文件的所属组与该目录的所有组保持一致。 | |
STICKYID | 对于文件:表示文件即使没有被程序调用,也会被加载到交换空间中。 |
对于目录:表示当目录上有此权限时,所有用户在该目录下均可创建文件,但只有文件拥有者和root用户可以删除该目录下的文件。 |
2.对特殊权限的设定
SUID | chmod u+s TAG | chmod 4源文件属性 TAG |
SGID | chmod g+s TAG | chmod 2源文件属性 TAG |
STICKYID | chmod o+t TAG | chmod 1源文件属性 TAG |
3.特殊权限不同状态区别举例
1)SUID
可以看出当开放suid权限时用其他用户(student)运行文件时其被调用用户还是拥有者用户(root)。
2)SGID
可以看出当文件开放sgid权限时其他用户(student)运行文件时其被调用组也会保持为文件拥有者(root)。
对于目录:
[root@localhost 桌面]# cd /mnt/
[root@localhost mnt]# mkdir test
[root@localhost mnt]# touch /mnt/test/file
[root@localhost mnt]# ls -lR /mnt/test/
/mnt/test/:
总用量 0
-rw-r--rw-. 1 root root 0 1月 8 22:19 file
[root@localhost ~]# chmod 777 /mnt/test/
[root@localhost ~]# su - student
[student@localhost ~]$ cd /mnt/test/
[student@localhost test]$ touch file1
[student@localhost test]$ ls -l
总用量 0
-rw-r--rw-. 1 root root 0 1月 8 22:19 file
-rw-rw-r--. 1 student student 0 1月 8 22:22 file1
[student@localhost test]$ su - root
密码:
[root@localhost ~]# chmod g+s /mnt/test/
[root@localhost ~]# su - student
[student@localhost ~]$ cd /mnt/test/
[student@localhost test]$ touch fele3
[student@localhost test]$ ls -l
总用量 0
-rw-rw-r--. 1 student root 0 1月 8 22:23 fele3
-rw-r--rw-. 1 root root 0 1月 8 22:19 file
-rw-rw-r--. 1 student student 0 1月 8 22:22 file1
[student@localhost test]$
可以看到,当目录sgid权限时,其他用户在目录下建立文件时新文件的所有组与目录是一致的。
3)STICKYID
[root@localhost 桌面]# mkdir /mnt/pub
[root@localhost 桌面]# touch /mnt/pub/test
[root@localhost ~]# chmod 777 /mnt/pub/
[root@localhost ~]# su - student
[student@localhost ~]$ touch /mnt/pub/test1
[student@localhost ~]$ ls -lR /mnt/pub/
/mnt/pub/:
总用量 0
-rw-r--rw-. 1 root root 0 1月 8 19:46 test
-rw-rw-r--. 1 student student 0 1月 8 19:47 test1
[student@localhost ~]$ rm -rf /mnt/pub/test
[student@localhost ~]$ ls -lR /mnt/pub/
/mnt/pub/:
总用量 0
-rw-rw-r--. 1 student student 0 1月 8 19:47 test1
[student@localhost ~]$ su - root
密码:
[root@localhost ~]# chmod o+t /mnt/pub/
[root@localhost ~]# touch /mnt/pub/test2
[root@localhost ~]# ls /mnt/pub/
test1 test2
[root@localhost ~]# ls -ld /mnt/pub/
drwxrwxrwt. 2 root root 32 1月 8 19:49 /mnt/pub/
[root@localhost ~]# su - student
[student@localhost ~]$ ll /mnt/pub/
总用量 0
-rw-rw-r--. 1 student student 0 1月 8 19:47 test1
-rw-r--rw-. 1 root root 0 1月 8 19:49 test2
[student@localhost ~]$ rm -rf /mnt/pub/test1
[student@localhost ~]$ rm -rf /mnt/pub/test2
rm: 无法删除'/mnt/pub/test2': 不允许的操作
[student@localhost ~]$
可以看出当目录有stickyid权限时,除root用户和文件拥有者之外不能删除目录中的其他文件。
七.ACL权限列表
ACL权限列表是为了针对不同用户或组群分开设置对应的权限
1.有关facl的命令
getfacl 查看权限列表
setfacl -m 设定权限
setfacl -x 删除指定用户
setfacl -b 关闭列表功能
例如:
[root@localhost 桌面]# touch file
[root@localhost 桌面]# ll file
-rw-r--rw-. 1 root root 0 1月 8 22:34 file
[root@localhost 桌面]# setfacl -m u:student:rwx file
[root@localhost 桌面]# ll file
-rw-rwxrw-+ 1 root root 0 1月 8 22:34 file
[root@localhost 桌面]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
mask::rwx
other::rw-
[root@localhost 桌面]# setfacl -x u:student file
[root@localhost 桌面]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::rw-
[root@localhost 桌面]# ll file
-rw-r--rw-+ 1 root root 0 1月 8 22:34 file
[root@localhost 桌面]# setfacl -b file
[root@localhost 桌面]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r--
other::rw-
[root@localhost 桌面]# ll file
-rw-r--rw-. 1 root root 0 1月 8 22:34 file
注:文件权限信息最后的 ”.“ 表示权限列表关闭,”+“ 表示权限列表开启。
2.facl列表权限匹配顺序
资源拥有者>特殊指定用户>权力开放多的组>权力开放少的组>其他用户
3.facl的mask阀直
facl的mask阀直表示可以给指定用户的最大权限,当权限列表打开时用chmod命令更改文件的权限时很可能就会更改facl的mask阀直。
例如:
4.facl的default权限
facl的default权限是指设置目录的默认权限列表,命令为:setfacl -m d:<u|g>: 权限 权限目录
例如;
[root@localhost 桌面]# mkdir test
[root@localhost 桌面]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
group::r-x
other::rw-
[root@localhost 桌面]# setfacl -m u:student:rwx test/
[root@localhost 桌面]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
user:student:rwx
group::r-x
mask::rwx
other::rw-
[root@localhost 桌面]# touch test/file
[root@localhost 桌面]# getfacl test/file
# file: test/file
# owner: root
# group: root
user::rw-
group::r--
other::rw-
[root@localhost 桌面]# setfacl -m d:u:student:rwx test/
[root@localhost 桌面]# touch test/file1
[root@localhost 桌面]# getfacl test/file1
# file: test/file1
# owner: root
# group: root
user::rw-
user:student:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::rw-
注:default权限只对目录设定,并且只对目录中新出现的文件按照目录生效,对目录本身不生效,对目录中的原有文件不生效。