一.普通权限
d r-xr-xr-x. 9 gdm gdm 0 Feb 25 09:25 1509
1 2 3 4 5 6 7
文件类型 文件权限 使用次数 所有者文件 所属者文件 文件大小 文件时间
linux中一切皆文件,每个文件都有类型。
1.文件类型
- 标记文件说明 标记文件类型
- 表示普通文件/一般文件
d 目录文件
l link 符号链接,快捷方式,也叫软连接
b 块设备文件 块设备:以数据块为单位的存储设备,比如硬盘,文件系统块 ,一般是4k, 随机存取设备
c 字符设备 顺序存取 ,比如鼠标、键盘等输出的
p pipline 管道文件
s socket 套接字文件 (套接字:IP+PORT)
2.文件的权限
r-x | r-x | r-x | .
a | b | c | d
a: 该文件的所有者对文件的权限
b: 该文件的所属组的用户对文件的权限
c:除了a,b描述的用户的其他用户对该文件的权限
d:扩展权限(.代表跟selinux相关)
r: 对文件内容的读取
w:对文件内容的修改
x:对文件的可执行权 注:如果过不是可执行文件,就算有权限,也不能执行
-:没有对应的权限
3.硬链接,软连接
表示文件或子目录的个数或链接的数量
文件 数据,元数据(用来描述数据的数据)
. 当前目录
.. 当前目录的上一级目录
4.当前文件的所有者
5.当前文件的所属者
6.文件的大小
注:如果是目录,该目录的大小不是该目录所有文件的大小之和 默认单位:字节
7.文件修改时间
访问时间 修改时间 改变时间 创建时间
访问时间: access time,当读取文件内容的时候
修改时间: modify time 默认显示,修改文件内容的时候
改变时间:change time,标记文件元素数据发生变化的时间
创建时间:create time,文件的创建时间
二.特殊权限(SET UID)
2.1 SUID:u+s,让进程不再属于他的发起者,而是程序文件本身
注:SUID仅可用在二进制文件,而对目录无效(在调用期间会暂时获得文件的所有者权限)
[root@localhost ~]# cd /home/wyy/
[root@localhost wyy]# ll
total 0
[root@localhost wyy]# touch file1
[root@localhost wyy]# ll
total 0
-rw-r--r--. 1 root root 0 Jul 14 22:15 file1
[root@localhost wyy]# chown wyy /bin/touch
[root@localhost wyy]# ll /bin/touch
-rwxr-xr-x. 1 wyy root 96320 Apr 14 2020 /bin/touch
[root@localhost wyy]# touch file2
[root@localhost wyy]# ll #诶,这咋没改过来呢,还是在root用户下的
total 0
-rw-r--r--. 1 root root 0 Jul 14 22:15 file1
-rw-r--r--. 1 root root 0 Jul 14 22:17 file2
[root@localhost wyy]# chmod u+s /bin/touch
[root@localhost wyy]# ll /bin/touch #这块标红了,但不代表就是错的,看他的rws有s代表特殊权限
-rwsr-xr-x. 1 wyy root 96320 Apr 14 2020 /bin/touch
[root@localhost wyy]# touch file3
[root@localhost wyy]# ll #看他变过来了
total 0
-rw-r--r--. 1 root root 0 Jul 14 22:15 file1
-rw-r--r--. 1 root root 0 Jul 14 22:17 file2
-rw-r--r--. 1 wyy root 0 Jul 14 22:20 file3
[root@localhost wyy]# chmod u-s /bin/touch #一定要减权限,要不然创建啥都是wyy,有的还没有权限
2.2 SGID:g+s
SGID可以有两个方面:
1.文件:如果SGID在二进制文件上,无论用户是谁,在运行进程的时候,他的有效用户组将会变成该程序的用户组所有者
2.目录:如果SGID在A目录上,A目录建立的文件或目录的用户组,将会是A目录的用户组
#练习题:创建共享文件/test,wyy组用户对/test目录里的文件可读可写
#1.创建/test,修改属组为wyy
[root@localhost wyy]# mkdir /test
[root@localhost wyy]# cd /
[root@localhost /]# ls -ld /test
drwxr-xr-x. 2 root root 6 Jul 14 22:34 /test
[root@localhost /]# chown :wyy /test
[root@localhost /]# ls -ld /test
drwxr-xr-x. 2 root wyy 6 Jul 14 22:34 /test
#2.g+s共享目录里新创建的文件属组是wyy
[root@localhost /]# chmod g+s /test
[root@localhost /]# ls -ld /test #r-s这就有特殊权限了
drwxr-sr-x. 2 root wyy 6 Jul 14 22:34 /test
[root@localhost /]# cd /test
[root@localhost test]# touch file1
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root wyy 0 Jul 14 22:38 file1
#二进制文件
[root@localhost test]# whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
[root@localhost test]# ll /usr/bin/mkdir
-rwxr-xr-x. 1 root root 84824 Apr 14 2020 /usr/bin/mkdir
[root@localhost test]# chmod g+s /usr/bin/mkdir
[root@localhost test]# ll /usr/bin/mkdir #他变成黄色了,因为还是在root底下
-rwxr-sr-x. 1 root root 84824 Apr 14 2020 /usr/bin/mkdir
[root@localhost test]# mkdir test1
[root@localhost test]# ll -d test1/
drwxr-sr-x. 2 root wyy 6 Jul 14 22:43 test1/
[root@localhost test]# su - wyy
Last login: Thu Jul 14 07:56:21 EDT 2022 on pts/0
[wyy@localhost ~]$ mkdir test2
[wyy@localhost ~]$ ll #所以换一个用户,看得出来组用户还是root
total 0
-rw-r--r--. 1 root root 0 Jul 14 22:15 file1
-rw-r--r--. 1 root root 0 Jul 14 22:17 file2
-rw-r--r--. 1 wyy root 0 Jul 14 22:20 file3
drwxrwxr-x. 2 wyy root 6 Jul 14 22:44 test2
[wyy@localhost ~]$ exit
logout
[root@localhost test]# chmod g-s /usr/bin/mkdir
[root@localhost test]# ll /usr/bin/mkdir
-rwxr-xr-x. 1 root root 84824 Apr 14 2020 /usr/bin/mkdir
[root@localhost test]# su - wyy
Last login: Thu Jul 14 22:44:11 EDT 2022 on pts/0
[wyy@localhost ~]$ mkdir test3
[wyy@localhost ~]$ ll #看这里,其实他的组用户是他自己
total 0
-rw-r--r--. 1 root root 0 Jul 14 22:15 file1
-rw-r--r--. 1 root root 0 Jul 14 22:17 file2
-rw-r--r--. 1 wyy root 0 Jul 14 22:20 file3
drwxrwxr-x. 2 wyy root 6 Jul 14 22:44 test2
drwxrwxr-x. 2 wyy wyy 6 Jul 14 22:48 test3
2.3 sticky:o+t,不能删除其他用户在同目录里城建的文件,可以删除自己创建的文件(防删除位)
作用:在具有t权限的目录下,用户在该目录下具有wx权限,该用户在该目录下创建文件或目录时,只有文件拥有者与root才有权利删除。
#练习题:
1.创建目录/test;
[root@localhost /]# mkdir test
[root@localhost /]# ll -d test
drwxr-xr-x. 2 root root 6 Jul 14 23:09 test
[root@localhost /]# chmod 777 /test/
[root@localhost /]# ll -d test
drwxrwxrwx. 2 root root 6 Jul 14 23:09 test
2.添加三个用户;
[root@localhost ~]# cat /etc/passwd
xiaohong:x:1005:1005::/home/xiaohong:/bin/bash
rhcsa:x:1000:1000:rhcsa:/home/rhcsa:/bin/bash
wyy:x:1004:1004::/home/wyy:/bin/bash
[root@localhost ~]# su - xiaohong
Last login: Thu Jul 14 23:25:47 EDT 2022 on pts/1
[xiaohong@localhost ~]$
[root@localhost ~]# su - rhcsa
[rhcsa@localhost ~]$
[root@localhost ~]# su - wyy
Last login: Thu Jul 14 23:01:39 EDT 2022 on pts/1
[wyy@localhost ~]$
3.让每一个文件在目录里面写东西;
[xiaohong@localhost ~]$ cd /test/
[xiaohong@localhost test]$ ll
total 0
[xiaohong@localhost test]$ touch xiaohong.txt
[xiaohong@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
[rhcsa@localhost ~]$ cd /test/
[rhcsa@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
[rhcsa@localhost test]$ touch rhcsa.txt
[rhcsa@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:12 rhcsa.txt
[wyy@localhost ~]$ cd /test/
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:12 rhcsa.txt
[wyy@localhost test]$ touch wyy.txt
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:12 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
4.用户3删除1和2用户文件;
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:12 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:28 xiaohong.txt
[wyy@localhost test]$ rm -rf xiaohong.txt
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:12 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
[wyy@localhost test]$ rm -rf rhcsa.txt
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
5.目录o+s;
[root@localhost /]# ll -d /test/
drwxrwxrwx. 2 root root 21 Jul 14 23:32 /test/
[root@localhost /]# chmod o+t /test/
[root@localhost /]# ll -d /test/
drwxrwxrwt. 2 root root 21 Jul 14 23:32 /test/
6.重复3、4操作,验证是否能够删除其他用户在目录下面创建的文件
[xiaohong@localhost test]$ ll
total 0
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
[xiaohong@localhost test]$ touch xiaohong.txt
[xiaohong@localhost test]$ ll
total 0
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[rhcsa@localhost test]$ ll
total 0
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[rhcsa@localhost test]$ touch rhcsa.txt
[rhcsa@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:35 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:35 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[wyy@localhost test]$ rm -rf xiaohong.txt
rm: cannot remove 'xiaohong.txt': Operation not permitted
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:35 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[wyy@localhost test]$ rm -rf rhcsa.txt
rm: cannot remove 'rhcsa.txt': Operation not permitted
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:35 rhcsa.txt
-rw-rw-r--. 1 wyy wyy 0 Jul 14 23:13 wyy.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[wyy@localhost test]$ rm -rf wyy.txt #这个老六终于不能删其他的了,只能删自己的
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 rhcsa rhcsa 0 Jul 14 23:35 rhcsa.txt
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
[root@localhost /]# chmod o-t /test/
[root@localhost /]# ll -d /test/
drwxrwxrwx. 2 root root 43 Jul 14 23:36 /test/
[root@localhost /]#
[wyy@localhost test]$ rm -rf rhcsa.txt #去掉权限,这个老六又可以了
[wyy@localhost test]$ ll
total 0
-rw-rw-r--. 1 xiaohong xiaohong 0 Jul 14 23:34 xiaohong.txt
2.4 chattr:设置隐藏权限
语法:chattr [选项] 文件
lsattr:查看用户隐藏权限
选项:
+/- 添加或减少隐藏权限
a 仅追加内容,无法覆盖或删除文件(改文件也无法删除)
[root@localhost /]# cp /var/log/messages /home/xiaohong/
[root@localhost /]# cd /home/xiaohong/
[root@localhost xiaohong]# ll
total 1424
-rw-------. 1 root root 1455513 Jul 14 23:44 messages
[root@localhost xiaohong]# lsattr messages
-------------------- messages
[root@localhost xiaohong]# chattr +a messages
[root@localhost xiaohong]# ll messages
-rw-------. 1 root root 1455513 Jul 14 23:44 messages
[root@localhost xiaohong]# lsattr messages
-----a-------------- messages
[root@localhost xiaohong]# vi messages
'''
在里面删除几行,不能:x出不去,用:wq!也出不去加了权限之后,不能修改
''''
[root@localhost xiaohong]# rm -rf messages #也删除不了,用过一次之后,会变成默认,a权限就没有了
rm: cannot remove 'messages': Operation not permitted
[root@localhost xiaohong]# ll
total 4272
-rw-------. 1 root root 1455513 Jul 14 23:44 messages
-rw-------. 1 root root 1455513 Jul 15 01:51 messages~
-rw-------. 1 root root 1455513 Jul 15 01:51 messagez~
[root@localhost xiaohong]# chattr +a messages
[root@localhost xiaohong]# ll
total 4272
-rw-------. 1 root root 1455513 Jul 14 23:44 messages
-rw-------. 1 root root 1455513 Jul 15 01:51 messages~
-rw-------. 1 root root 1455513 Jul 15 01:51 messagez~
[root@localhost xiaohong]# lsattr
-----a-------------- ./messages
[root@localhost xiaohong]# echo "12345678" >> ./messages #就可以追加
三.修改文档权限
3.1 chomd:改变文件或目录权限
语法:chomd [选项] 权限 文件或目录
选项:
--reference -RFILE 根据参考文献设置权限
-R 递归将权限应用于子目录下的所有文件
+/- 在原有权限基础上修改权限
数字 用rwx(=421)权限数字修改权限
chomd中,u代表所有者,g代表所属组,o代表其他用户,a代表所有人(有三种方法:u=rw,g=r,o=-、755、u+wx,g+w,不能把这三个兄弟混为一谈哦)
[root@localhost ~]# ls -l
total 3196
drwxr-xr-x. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# chmod u=r 111file
[root@localhost ~]# ll
total 3196
dr--r-xr-x. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# chmod u=rw,g=r,o=- 111file
[root@localhost ~]# ll
total 3196
drw-r-----. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# chmod u+wx,g+w 111file
[root@localhost ~]# ll
total 3196
drwxrw----. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# chmod g-w 111file
[root@localhost ~]# ll
total 3196
drwxr-----. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# chmod 755 111file/
[root@localhost ~]# ll
total 3196
drwxr-xr-x. 2 root root 6 Jul 14 07:22 111file
[root@localhost ~]# ll
total 3196
drwxr-xr-x. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root wyy 6 Jul 14 07:23 123
-rw-------. 1 root root 1563 May 31 03:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Jun 21 02:16 file2
-rw-r--r--. 2 root root 10 Jun 21 02:02 filebak
-rw-r--r--. 2 root root 10 Jun 21 02:02 filebak1
-rw-r--r--. 1 root root 447041 Jul 12 01:41 'WPSͼƬƴͼ(2).png'
-rw-r--r--. 1 root root 2809450 Jul 12 01:21 wyy.tar.bz2
[root@localhost ~]# chmod --reference=anaconda-ks.cfg 111file/
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root wyy 6 Jul 14 07:23 123
-rw-------. 1 root root 1563 May 31 03:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Jun 21 02:16 file2
-rw-r--r--. 2 root root 10 Jun 21 02:02 filebak
-rw-r--r--. 2 root root 10 Jun 21 02:02 filebak1
-rw-r--r--. 1 root root 447041 Jul 12 01:41 'WPSͼƬƴͼ(2).png'
-rw-r--r--. 1 root root 2809450 Jul 12 01:21 wyy.tar.bz2
3.2 chown:修改文件或目录所有者与所属组
语法:chown [选项] [所有者] [:[所属组]] 文件或目录
选项:
-R 递归将权限应用于子目录下的所有文件
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root wyy 6 Jul 14 07:23 123
[root@localhost ~]# chown root:root 123
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root root 6 Jul 14 07:23 123
[root@localhost ~]# chown :wyy 123
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root wyy 6 Jul 14 07:23 123
[root@localhost ~]# chown wyy 123
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 wyy wyy 6 Jul 14 07:23 123
[root@localhost ~]# chown root 123 #只改变所有者
[root@localhost ~]# ll
total 3196
drw-------. 2 root root 6 Jul 14 07:22 111file
drwxr-xr-x. 2 root wyy 6 Jul 14 07:23 123