控制对文件的访问
权限分为两大类:
访问权限:读、写、执行
归属:所有者、所属组
一、管理目录和文件属性
1、查看文件属性(ls -l、ll)
例:drwxr-xr-x. 3 root root 16 May 28 09:28 home
d:文件类型
rwx:所有者的权限
r-x:所属组的权限
r-x:其他用户的权限
3:引用计数(两种情况)
(1)目录:子目录个数
[root@stw ~]# cd Desktop/
[root@stw Desktop]# ls -a
. ..
(2)文件:这个值-1=硬链接的个数
[root@stw ~]# touch a
[root@stw ~]# ll
total 8
-rw-r--r--. 1 root root 0 Aug 4 10:51 a
-rw-------. 1 root root 1886 Jul 23 12:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 10:51 Desktop
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Documents
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Downloads
-rw-r--r--. 1 root root 1917 Jul 23 12:27 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Music
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Pictures
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Public
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Templates
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Videos
[root@stw ~]# ln a harda
[root@stw ~]# ll
total 8
-rw-r--r--. 2 root root 0 Aug 4 10:51 a
-rw-------. 1 root root 1886 Jul 23 12:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 10:51 Desktop
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Documents
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Downloads
-rw-r--r--. 2 root root 0 Aug 4 10:51 harda
-rw-r--r--. 1 root root 1917 Jul 23 12:27 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Music
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Pictures
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Public
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Templates
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Videos
root:所有者(用户名)
root:所属组(组名)
16:大小,单位为字节B
May 28 09:28 创建时间
home 目录或者文件名
2、文件权限
读 写 执行
字符表示 r w x
数字表示 4 2 1
[root@stw ~]# ll
total 8
-rw-------. 1 root root 1886 Jul 23 12:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Desktop
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Documents
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Downloads
-rw-r--r--. 1 root root 1917 Jul 23 12:27 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Music
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Pictures
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Public
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Templates
drwxr-xr-x. 2 root root 6 Jul 23 12:27 Videos
文件:r(查看文件内容–4) w(修改文件内容、名称,删除此文件–2) x(运行此文件–1)
目录:r(列出子文件、子目录) w(创建、修改此目录的子文件、子目录) x(一定有x权限才能用cd切换)
文件:
只读:r–
读写:rw-
读写执行:rwx(755)
目录:
只读:r-x
读写:rwx
读写执行:rwx
3、设置修改文件权限
chmod命令
a、使用数值改变文件的权限
chmod 777 test1 —给test1文件的所有者、所属组、其他用户全部给与读写执行的权限。
[root@stw tmp]# chmod 744 file1 //所有者:读、写、执行,所有组:只读,其他:只读
[root@stw tmp]# ll
total 0
-rwxr--r--. 1 root root 0 Aug 4 11:06 file1
b、使用字母来改变文件的权限
-----参数设置
a 所有用户
u 所有者
g 同组用户
o 除去创建者和同组用户之外的用户(其他用户)
增加权限
- 清除权限
= 设置唯一权限
-----常用设置
g+w – 增加组用户的写权限
o-rwx – 清除其他用户的全部访问权限
u+x – 允许文件属主执行文件
a+rw – 允许所有用户读和写文件
ug+r – 允许文件属主和属组用户读文件
g=rx – 设置属组用户只能读和执行文件(不可写)
通过增加 -R 参数,可以改变整个目录树的权限。
chmod o+w test2 —给test2文件的其他用户添加写的权限
chmod go-rw test3 —给test3文件的所属组和其他用户移除读写权限
chmod a-rw test4 —给test4文件所有的用户移除读写权限
chmod -R g+w /home/test5/
给home目录下面的test5目录下所有的文件和目录的所属组添加写的权限
[root@stw tmp]# touch file1
[root@stw tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 4 11:06 file1
[root@stw tmp]# chmod g+w file1 //给file1的所有组增加一个写的权限
[root@stw tmp]# ll
total 0
-rw-rw-r--. 1 root root 0 Aug 4 11:06 file1
[root@stw tmp]# chmod u=rwx,g=rw,o=rw file1
[root@stw tmp]# ll
total 0
-rwxrw-rw-. 1 root root 0 Aug 4 11:06 file1
[root@stw tmp]# chmod u-x file1 //给file1的所有者减去一个执行的权限
[root@stw tmp]# ll
total 0
-rw-rw-rw-. 1 root root 0 Aug 4 11:06 file1
[root@stw tmp]# chmod a=rw file1 //所有的用户拥有读写的权限
[root@stw tmp]# ll
total 0
-rw-rw-rw-. 1 root root 0 Aug 4 11:06 file1
4、修改目录或文件的所有者或者所属组
chgrp 设置文件或者目录的所属组
chgrp 新的组名 文件/目录
chown 设置文件或者目录的所有者
chown 新的组名 文件/目录
[root@stw tmp]# chgrp stw file1
[root@stw tmp]# ll
total 0
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
[root@stw tmp]# chown stw file1
[root@stw tmp]# ll
total 0
-rwxr--r--. 1 stw stw 0 Aug 4 11:06 file1
chown 所有者:所属组 文件/目录(同时修改所有者和所属组)
chown 所有者.所属组 文件/目录
[root@stw tmp]# chown root:stw file1
[root@stw tmp]# ll
total 0
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
-R(递归)
[root@stw tmp]# mkdir dir1
[root@stw tmp]# cd dir1
[root@stw dir1]# touch a
[root@stw dir1]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 4 11:24 a
[root@stw dir1]# cd ..
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 root root 15 Aug 4 11:24 dir1
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
[root@stw tmp]# chown -R stw dir1 //把dir1和这个目录下的所有子文件、子目录的所有者都改成stw
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 stw root 15 Aug 4 11:24 dir1
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
[root@stw tmp]# cd dir1
[root@stw dir1]# ll
total 0
-rw-r--r--. 1 stw root 0 Aug 4 11:24 a
特殊权限
suid(u+s) 应用于二进制文件(命令),调用二进制文件的所有者的身份来执行
chmod u+s /usr/bin/mkdir
chmod 4xxx /usr/bin/mkdir
[root@stw ~]# mkdir /abc //root用户下可创建目录
[root@stw ~]# su - stw
[stw@stw ~]$ mkdir /cde //普通用户下不可创建目录
mkdir: cannot create directory ‘/cde’: Permission denied
[stw@stw ~]$ exit
logout
[root@stw ~]# which mkdir //查看mkdir目录位置
/usr/bin/mkdir
[root@stw ~]# ll /usr/bin/mkdir //可以看出普通用户对mkdir命令有执行权限
-rwxr-xr-x. 1 root root 79864 Oct 31 2018 /usr/bin/mkdir
[root@stw ~]# su - stw
Last login: Mon Aug 4 11:33:24 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp
[stw@stw tmp]$ mkdir cde //普通用户在tmp目录下可以执行
[stw@stw tmp]$ ll
total 0
drwxrwxr-x. 2 stw stw 6 Aug 4 11:34 cde
drwxr-xr-x. 2 stw root 15 Aug 4 11:24 dir1
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
[stw@stw tmp]$ mkdir /aa //证明普通用户不能在根目录下创建目录
mkdir: cannot create directory ‘/aa’: Permission denied
[root@stw ~]# chmod u+s /usr/bin/mkdir
[root@stw ~]# ll /usr/bin/mkdir
-rwsr-xr-x. 1 root root 79864 Oct 31 2018 /usr/bin/mkdir
[root@stw ~]# su - stw
Last login: Mon Aug 4 11:34:12 CST 2025 on pts/0
[stw@stw ~]$ mkdir /bcd
[stw@stw /]$ ll -d /bcd
drwxrwxr-x. 2 root stw 6 Aug 4 11:42 /bcd
[stw@stw /]$ cd /tmp
[stw@stw tmp]$ mkdir abcd
[stw@stw tmp]$ ll -d abcd
drwxrwxr-x. 2 root stw 6 Aug 4 11:47 abcd
[root@stw ~]# useradd yyy
[root@stw ~]# su - yyy
[yyy@stw ~]$ cd /tmp
[yyy@stw tmp]$ mkdir dir2
[yyy@stw tmp]$ ll
total 0
drwxrwxr-x. 2 root stw 6 Aug 4 11:47 abcd
drwxrwxr-x. 2 stw stw 6 Aug 4 11:34 cde
drwxr-xr-x. 2 stw root 15 Aug 4 11:24 dir1
drwxrwxr-x. 2 root yyy 6 Aug 4 15:14 dir2
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
注意:因为是调用root的身份来创建的目录,所以这个目录的所有者是root,所属组还是用普通用户创建时,它本来应该归属的组(stw),而且不管是在哪个目录下创建,或者哪个用户创建,只要root用户用了chmod u+s /usr/bin/mkdir这个命令,用户创建的目录的所有者都是root。
chmod u-s /usr/bin/mkdir(取消)
[root@stw ~]# chmod u-s /usr/bin/mkdir
[root@stw ~]# su - stw
Last login: Mon Aug 4 11:41:55 CST 2025 on pts/0
[stw@stw ~]$ mkdir /dir3
mkdir: cannot create directory ‘/dir3’: Permission denied
sgid(g+s) 应用于二进制文件或者目录,当作用在目录的时候,拥有继承权限
chmod g+s /tmp/test
chmod 2xxx /tmp/test
[root@stw ~]# chmod g+s /usr/bin/mkdir
[root@stw ~]# su - stw
Last login: Mon Aug 4 15:17:16 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp
[stw@stw tmp]$ mkdir cc
[stw@stw tmp]$ ll
total 0
drwxrwxr-x. 2 root stw 6 Aug 4 11:47 abcd
drwxrwxr-x. 2 stw root 6 Aug 4 15:19 cc
drwxrwxr-x. 2 stw stw 6 Aug 4 11:34 cde
drwxr-xr-x. 2 stw root 15 Aug 4 11:24 dir1
drwxrwxr-x. 2 root yyy 6 Aug 4 15:14 dir2
-rwxr--r--. 1 root stw 0 Aug 4 11:06 file1
调用g+s则所有者不变,所属组变为root
继承权限:(比如:如果对test目录做了g+s,test属于stw组,那么在test目录下,不管是什么用户创建子目录或者子文件,新创建出来的子文件或者子目录也属于stw组)
[root@stw tmp]# mkdir test
[root@stw tmp]# chmod 777 test
[root@stw tmp]# ll
total 0
drwxrwxrwx. 2 root root 6 Aug 4 15:22 test
[root@stw tmp]# chgrp stw test
[root@stw tmp]# ll
total 0
drwxrwxrwx. 2 root stw 6 Aug 4 15:22 test
[root@stw tmp]# su - stw
Last login: Mon Aug 4 15:19:12 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp/test
[stw@stw test]$ ls
[stw@stw test]$ mkdir dir1
[stw@stw test]$ ll
total 0
drwxrwxr-x. 2 stw stw 6 Aug 4 15:24 dir1
[stw@stw test]$ exit
logout
[root@stw tmp]# su - yyy
Last login: Mon Aug 4 15:14:31 CST 2025 on pts/0
[yyy@stw ~]$ cd /tmp/test
[yyy@stw test]$ mkdir dir2
[yyy@stw test]$ ll
total 0
drwxrwxr-x. 2 stw stw 6 Aug 4 15:24 dir1
drwxrwxr-x. 2 yyy yyy 6 Aug 4 15:25 dir2
[yyy@stw test]$ exit
logout
[root@stw tmp]# chmod g+s test //对test这个目录设置g+s
[root@stw tmp]# ll
total 0
drwxrwsrwx. 4 root stw 30 Aug 4 15:25 test
[root@stw tmp]# cd test
[root@stw test]# mkdir dir3
[root@stw test]# touch file3
[root@stw test]# ll
total 0
drwxrwxr-x. 2 stw stw 6 Aug 4 15:24 dir1
drwxrwxr-x. 2 yyy yyy 6 Aug 4 15:25 dir2
drwxr-sr-x. 2 root stw 6 Aug 4 15:26 dir3
-rw-r--r--. 1 root stw 0 Aug 4 15:26 file3
[root@stw test]# su - yyy
Last login: Mon Aug 4 15:24:45 CST 2025 on pts/0
[yyy@stw ~]$ cd /tmp/test
[yyy@stw test]$ touch file222
[yyy@stw test]$ mkdir dir222
[yyy@stw test]$ ll
total 0
drwxrwxr-x. 2 stw stw 6 Aug 4 15:24 dir1
drwxrwxr-x. 2 yyy yyy 6 Aug 4 15:25 dir2
drwxrwsr-x. 2 yyy stw 6 Aug 4 15:27 dir222
drwxr-sr-x. 2 root stw 6 Aug 4 15:26 dir3
-rw-rw-r--. 1 yyy stw 0 Aug 4 15:27 file222
-rw-r--r--. 1 root stw 0 Aug 4 15:26 file3
sticky(o+t) 应用于目录,对目录使用了sticky权限,对该目录具有写入权限的任何用户(root用户除外)仅能删除自己为属主的文件或目录
chmod o+t /tmp/test
[root@stw test]# cd /tmp
[root@stw tmp]# rm -rf *
[root@stw tmp]# mkdir test
[root@stw tmp]# chmod 777 test
[root@stw tmp]# ll
total 0
drwxrwxrwx. 2 root root 6 Aug 4 15:36 test
[root@stw tmp]# useradd user1
[root@stw tmp]# useradd user2
[root@stw tmp]# useradd user3
[root@stw tmp]# su - user1
Last login: Mon Aug 4 15:39:12 CST 2025 on pts/0
[user1@stw ~]$ cd /tmp/test
[user1@stw test]$ touch file1
[user1@stw test]$ exit
logout
[root@stw tmp]# su - user2
Last login: Mon Aug 4 15:39:27 CST 2025 on pts/0
[user2@stw ~]$ cd /tmp/test
[user2@stw test]$ touch file2
[user2@stw test]$ exit
logout
[root@stw tmp]# su - user3
[user3@stw ~]$ cd /tmp/test
[user3@stw test]$ touch file3
[user1@stw test]$ ll
total 0
-rw-rw-r--. 1 user1 user1 0 Aug 4 15:41 file1
-rw-rw-r--. 1 user2 user2 0 Aug 4 15:41 file2
-rw-rw-r--. 1 user3 user3 0 Aug 4 15:40 file3
[user1@stw test]$ rm -rf file3 //user1可以删除user3用户的文件
[user1@stw test]$ ll
total 0
-rw-rw-r--. 1 user1 user1 0 Aug 4 15:41 file1
-rw-rw-r--. 1 user2 user2 0 Aug 4 15:41 file2
[root@stw tmp]# chmod o+t test
[root@stw tmp]# su - user1
Last login: Mon Aug 4 15:41:33 CST 2025 on pts/0
[user1@stw ~]$ cd /tmp/test
[user1@stw test]$ ll
total 0
-rw-rw-r--. 1 user1 user1 0 Aug 4 15:41 file1
-rw-rw-r--. 1 user2 user2 0 Aug 4 15:41 file2
[user1@stw test]$ rm -rf file2 //user1不能删除user2用户的文件
rm: cannot remove ‘file2’: Operation not permitted
chmod 1xxx /tmp/test (1777: 1:特殊权限位,7:所有者,7:所属组,7:其他用户)
[root@stw tmp]# mkdir test
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 4 15:49 test
[root@stw tmp]# chmod 1777 test
[root@stw tmp]# ll
total 0
drwxrwxrwt. 2 root root 6 Aug 4 15:49 test
特殊权限: 1:o+t 2:g+s 4:u+s
默认情况下,root用户新建一个新的目录,该目录默认的访问权限为755
root用户新建一个文件,该文件默认的访问权限为644
普通用户新建一个新的目录,该目录默认的访问权限为775
普通用户新建一个文件,该文件默认的访问权限为664
[root@stw tmp]# touch file1
[root@stw tmp]# mkdir dir1
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 4 15:54 dir1 //755 777与022抵消
-rw-r--r--. 1 root root 0 Aug 4 15:54 file1 //644 666与022抵消
[root@stw tmp]# su - stw
Last login: Mon Aug 4 15:23:32 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp
[stw@stw tmp]$ mkdir dir2
[stw@stw tmp]$ touch file2
[stw@stw tmp]$ ll
total 0
drwxr-xr-x. 2 root root 6 Aug 4 15:54 dir1
drwxrwxr-x. 2 stw stw 6 Aug 4 15:54 dir2 //775 777与002抵消
-rw-r--r--. 1 root root 0 Aug 4 15:54 file1
-rw-rw-r--. 1 stw stw 0 Aug 4 15:54 file2 //664 666与002抵消
root用户的默认umask值为022
普通用户默认的umask值为002
创建一个文件,文件的最大权限为666,创建一个目录,目录的最大权限为777,然后看你用什么用户进行创建,会与你用户对应的umask值做抵消,一般不会更改umask值。
ACL权限(访问控制列表)
给特定的用户或组对特定的文件或者目录赋予特定的权限
例:
[root@stw tmp]# mkdir test
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 4 16:05 test
//stw属于其他用户,对test没有写的权限
查看test目录的权限信息
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
只允许stw用户对test文件有写的权限
[root@stw tmp]# setfacl -m u:stw:w test
[root@stw tmp]# ll
total 0
drwxrwxr-x+ 2 root root 6 Aug 4 16:05 test //做了acl之后用ll查看权限不准确
[root@stw tmp]# getfacl test //用getfacl查看权限信息
# file: test
# owner: root
# group: root
user::rwx
user:stw:-w-
group::r-x
mask::rwx
other::r-x
只允许yyy组对test文件有读、写、执行的权限
[root@stw tmp]# setfacl -m g:yyy:rwx test
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:stw:-w-
group::r-x
group:yyy:rwx
mask::rwx
other::r-x
只允许stw用户对目录test有读写执行的权限,且在目录test下新建的文件或目录继承目录test的权限
setfacl -m u:stw:rwx test
setfacl -m d:u:stw:rwx test
[root@stw tmp]# mkdir test
[root@stw tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 4 16:19 test
[root@stw tmp]# cd test
[root@stw test]# mkdir dir1
[root@stw test]# cd ..
[root@stw tmp]# setfacl -m u:stw:rwx test
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:stw:rwx
group::r-x
mask::rwx
other::r-x
[root@stw tmp]# su - stw
Last login: Mon Aug 4 15:54:36 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp/test
[stw@stw test]$ touch file111 //在test中可以创建文件
[stw@stw test]$ ls
dir1 file111
[stw@stw test]$ cd dir1
[stw@stw dir1]$ touch file222
touch: cannot touch ‘file222’: Permission denied //在test的字目录下不能创建文件
[stw@stw dir1]$ exit
logout
[root@stw tmp]# setfacl -m d:u:stw:rwx test //test的子目录继承目录test的权限
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:stw:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:stw:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@stw tmp]# cd test
[root@stw test]# ls
dir1 file111
[root@stw test]# getfacl dir1
//但是是要继承之后的新创建的目录才有test的权限,原来的目录依旧没有test的权限
# file: dir1
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@stw test]# mkdir dir2
[root@stw test]# getfacl dir2
# file: dir2
# owner: root
# group: root
user::rwx
user:stw:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:stw:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@stw test]# su - stw
Last login: Mon Aug 4 16:22:09 CST 2025 on pts/0
[stw@stw ~]$ cd /tmp/test
[stw@stw test]$ ls
dir1 dir2 file111
[stw@stw test]$ cd dir2
[stw@stw dir2]$ touch file333
[stw@stw dir2]$ ll
total 0
-rw-rw-r--+ 1 stw stw 0 Aug 4 16:26 file333
删除目录b/的default权限(删除继承)
setfacl -k b/
[root@stw test]# setfacl -k dir2
[root@stw test]# getfacl dir2
# file: dir2
# owner: root
# group: root
user::rwx
user:stw:rwx
group::r-x
mask::rwx
other::r-x
删除文件a的acl权限
setfacl -b a
[root@stw test]# setfacl -b dir2
[root@stw test]# getfacl dir2
# file: dir2
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
删除default权限和acl权限
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:stw:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:stw:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@stw tmp]# setfacl -b test
[root@stw tmp]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
注意:-b:删除所有acl权限,包括default权限