一、文件的权限和归属
-
访问权限
读权限r:允许查看文件内容
写权限w:允许修改内容
可执行x:允许执行程序 -
归属
属主:拥有该文件的用户账号
属组:拥有该文件的组账号 -
查看文件的权限和归属
例:[root@localhost ~]# ls -l 总用量 4221444 drwxr-xr-x. 3 root root 14 7月 30 07:22 a -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg 第一位表示文件类型 -:代表普通文件 d:代表目录 l:表示软连接 p:表示管道文件 s:表示通信套接字文件 c:表示字符设备 b:表示块状设备
各权限表示,及8进制表示
字母表示 | 进制表示 |
---|---|
r | 4 |
w | 2 |
x | 1 |
注:目录默认权限"755" rwxr-xr-x
文件默认权限644 rw-r–r--
二、设置文件和目录的权限
-
chmod命令
格式:chmod 【选项】 文件或目录
R:以递归的方式设置目录及目录下的子目录或文件
u:属主
g:属组
o:其他人
a:所有人
+:添加
-:删除
=:重置例:[root@localhost ~]# ll 总用量 4 drwxr-xr-x. 2 root root 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-r--r--. 1 root root 0 8月 12 22:17 yi.txt [root@localhost ~]# chmod u-x,g+w,o+w aaa [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 root root 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-r--r--. 1 root root 0 8月 12 22:17 yi.txt [root@localhost ~]# chmod 666 yi.txt [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 root root 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 root root 0 8月 12 22:17 yi.txt
三、设置文件归属
-
chown命令与chgrp命令
格式:chown 属主 文件
chown :属组 文件
chown 属主:属组 文件
chgrp 属组 文件例:[root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 root root 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 root root 0 8月 12 22:17 yi.txt [root@localhost ~]# chown yi aaa [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 yi root 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 root root 0 8月 12 22:17 yi.txt [root@localhost ~]# chown :yi aaa [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 yi yi 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 root root 0 8月 12 22:17 yi.txt [root@localhost ~]# chown yi:yi yi.txt [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 yi yi 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 yi yi 0 8月 12 22:17 yi.txt [root@localhost ~]# chgrp root yi.txt [root@localhost ~]# ll 总用量 4 drw-rwxrwx. 2 yi yi 6 8月 12 22:17 aaa -rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg -rw-rw-rw-. 1 yi root 0 8月 12 22:17 yi.txt
四、权限掩码umask
- umask
作用:控制新建文件和目录的权限
对照表
umask值 | 目录权限 | 文件权限 |
---|---|---|
0 | 7 | 6 |
1 | 6 | 6 |
2 | 5 | 4 |
3 | 4 | 4 |
4 | 3 | 2 |
5 | 2 | 2 |
6 | 1 | 0 |
7 | 0 | 0 |
例:[root@localhost ~]# umask
0022
[root@localhost ~]# mkdir aa
[root@localhost ~]# touch yi.txt
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 6 8月 12 23:09 aa
-rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 8月 12 23:10 yi.txt
[root@localhost ~]# umask 0020
[root@localhost ~]# mkdir bb
[root@localhost ~]# touch er.txt
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 6 8月 12 23:09 aa
-rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg
drwxr-xrwx. 2 root root 6 8月 12 23:10 bb
-rw-r--rw-. 1 root root 0 8月 12 23:10 er.txt
-rw-r--r--. 1 root root 0 8月 12 23:10 yi.txt
五、ACL权限控制
注:acl需要安装
yum -y install acl
-
setfacl
格式: setfacl 【选项】 【参数】 文件或目录
-m:设置后续acl参数
-x:删除后续acl参数
-b:删除所有acl参数
-k:删除默认acl参数
-R:递归设置acl参数
-d:设置默认acl参数,只对目录有效u:用户名:权限 指定用户权限
g:组名:权限 指定组权限
m:权限 更改权限掩码例:[root@localhost ~]# for i in {1..5}; do useradd user$i ; done [root@localhost ~]# for i in {1..5}; do useradd user$i ; done [root@localhost ~]# cat /etc/passwd | grep ^user user1:x:8890:8890::/home/user1:/bin/bash user2:x:8891:8891::/home/user2:/bin/bash user3:x:8892:8892::/home/user3:/bin/bash user4:x:8893:8893::/home/user4:/bin/bash user5:x:8894:8894::/home/user5:/bin/bash [root@localhost ~]# ls anaconda-ks.cfg [root@localhost ~]# touch yi [root@localhost ~]# setfacl -m u:user1:rwx yi [root@localhost ~]# getfacl yi # file: yi # owner: root # group: root user::rw- user:user1:rwx group::r-- mask::rwx other::r--
-
getfacl
格式:getfacl 文件或目录例:[root@localhost ~]# getfacl yi # file: yi # owner: root # group: root user::rw- user:user1:rwx group::r-- mask::rwx other::r--
六、SUID,SGID,SBIT
SUID:执行者在执行瞬间将拥有文件属主的身份权限
SGID:执行者在执行过程中会获得该程序群组的支持
SBIT:该目录下的文件及目录仅其属主与root用户具有删除权限
例:[root@localhost ~]# chmod u+s yi
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg
-rwSr--r--. 1 root root 0 8月 13 01:02 yi
[root@localhost ~]# chmod g+s yi
[root@localhost ~]# chmod o+t yi
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1089 7月 8 21:21 anaconda-ks.cfg
-rwSr-Sr-T. 1 root root 0 8月 13 01:02 yi