文件基本权限

  •   rwx    r-x     r-x     user1  user1  FILENAME 类型 拥有者的权限 所属组的权限 其他人的权限 拥有者  属组   对象

对于文件:r读  w写  x执行

对于目录:r读(看到目录里面有什么) ls      w建文件、删除、移动 touch mkdir rm mv cp      x进入 cd 第一个表示文件的类型,如d目录,-(f)普通文件,L链接,b块设备,c字符设备,p管道文件,s套接字文件 修改权限的相关命令: chmod chmod [ugoa][+-=][rwx] 文件或目录

使用数字表示权限 r=4,w=2,x=1,用户权限=4+2+1=rwx

[root@localhost ~]# chmod 622 a.txt [root@localhost ~]# ll a.txt -rw--w--w- 1 root root 0 Feb 15 07:52 a.txt [root@localhost ~]#

chown 作用:修改文件拥有者和所属组 语法:chown USER:GROUP 对象 chown USER 对象 chown :GROUP 对象 [root@localhost ~]# chown rm:bin a.txt [root@localhost ~]# ll a.txt -rw--w--w- 1 rm bin 0 Feb 15 07:52 a.txt [root@localhost ~]# chown daemon a.txt [root@localhost ~]# ll a.txt -rw--w--w- 1 daemon bin 0 Feb 15 07:52 a.txt [root@localhost ~]# chown :sys a.txt [root@localhost ~]# ll a.txt -rw--w--w- 1 daemon sys 0 Feb 15 07:52 a.txt

-R递归(目录下的所有内容全部更改,否则只修改目录) [root@localhost ~]# chown rm test/ -R [root@localhost ~]# ll -d test/ dr-xr-xr-x 2 rm root 30 Feb 15 08:21 test/ [root@localhost ~]# ll test/ total 0 -rw-r--r-- 1 rm root 0 Feb 15 08:21 a.txt -rw-r--r-- 1 rm root 0 Feb 15 08:21 b.txt

一个文件只有读的权限,拥有者是否可以写这个文件? 文件所有者一定可以写文件 [root@localhost ~]# ll /home/rm/rm.txt -r--r--r-- 1 rm rm 30 Feb 15 08:01 /home/rm/rm.txt [root@localhost ~]# su - rm [rm@localhost ~]$ vim rm.txt 结果:可以正常写入,注意:保存时用wq!

特殊权限: SUID SGID Stickybit s对应的数值为:u 4,g 2,o 1 SUID: 限定:只能设置在二进制可执行程序上面。对目录文本设置无效。 功能:程序运行时的权限从执行者变更成程序所有者。 [root@localhost ~]# which passwd /usr/bin/passwd [root@localhost ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 Jan 29 2014 /usr/bin/passwd

[root@localhost ~]# which less /usr/bin/less [root@localhost ~]# less /etc/shadow ##root用户可以通过less访问passwd文件 [root@localhost ~]# [root@localhost ~]# su - u01 上一次登录:五 12月 8 03:03:06 CST 2017pts/1 上 [u01@localhost ~]$ less /etc/shadow /etc/shadow: 权限不够 [root@localhost ~]# chmod u+s /usr/bin/less 等同于 [root@localhost ~]# chmod 4755 /usr/bin/less [u01@localhost ~]$ less /etc/shadow ##可以通过less访问shadow和passwd文件 [u01@localhost ~]$ less /etc/passwd

SGID 限定:既可以给二进制可执行程序设置,也可以给目录设置。 功能:在设置了SGID权限的目录下建立文件时,新创建的文件的所属组会继承上级目录的所属组 [root@localhost ~]# mkdir 123 [root@localhost ~]# id u01 uid=2022(u01) gid=2022(u01) 组=2022(u01) [root@localhost ~]# chown :u01 123/ [root@localhost ~]# ll -d 123 drwxr-xr-x. 2 root u01 6 12月 8 04:38 123 [root@localhost ~]# touch 123/123.txt [root@localhost ~]# ll 123 总用量 0 -rw-r--r--. 1 root root 0 12月 8 04:39 123.txt

加上SGID [root@localhost ~]# chmod g+s 123 [root@localhost ~]# touch 123/456.txt [root@localhost ~]# ll 123 总用量 0 -rw-r--r--. 1 root root 0 12月 8 04:39 123.txt -rw-r--r--. 1 root u01 0 12月 8 04:41 456.txt

Stickybit 限定:只作用于目录 功能:目录下创建的文件只有root、文件创建者、目录所有者才能删除 [root@localhost ~]# ll -d /tmp/ drwxrwxrwt. 14 root root 4096 Feb 15 09:08 /tmp/

[root@localhost ~]# chmod 1777 /share/ [root@localhost ~]# ll -d /share/ drwxrwxrwt 2 root root 6 Feb 15 09:10 /share/

[u01@localhost ~]$ cd /share/ [u01@localhost share]$ ls [u01@localhost share]$ touch 123.txt

[root@localhost ~]# useradd u02 [root@localhost ~]# su - u02 [u02@localhost ~]$ cd /share/ [u02@localhost share]$ rm -rf 123.txt rm: cannot remove ‘123.txt’: Operation not permitted

扩展ACL

[root@localhost ~]# ll 123.txt -rw-r--r--. 1 root root 64 12月 8 01:16 123.txt

[root@localhost ~]# getfacl 123.txt ##查看acl权限

file: 123.txt

owner: root

group: root

user::rw- group::r-- other::r--

[root@localhost ~]# setfacl -m u:u01:rw 123.txt ##设置acl给u01用户读写的权限 [root@localhost ~]# getfacl 123.txt

file: 123.txt

owner: root

group: root

user::rw- user:u01:rw- group::r-- mask::rw- other::r--

对目录进行设置 [root@localhost ~]# setfacl -R -m u:u02:rw 123 (-R一定要在-m前面,表示目录下所有文件)

[root@localhost ~]# getfacl !$ getfacl 123

file: 123

owner: root

group: u01

flags: -s-

user::rwx user:u02:rw- group::r-x mask::rwx other::r-x

删除acl [root@localhost ~]# setfacl -x u:u01 123.txt #删除单个用户的acl权限 [root@localhost ~]# setfacl -b 123.txt #删除所有用户的acl权限

实战-创建一个让root都无法删除的黑客文件 Linux文件系统扩展属性:chattr(添加权限) lsattr(取消权限) +a 只能追加内容 +i 不能被修改

[root@localhost ~]# echo "123456" >123.txt [root@localhost ~]# cat 123.txt 123456 [root@localhost ~]# chattr +a 123.txt [root@localhost ~]# echo "789" >123.txt -bash: 123.txt: 不允许的操作 [root@localhost ~]# echo "789" >>123.txt [root@localhost ~]# cat 123.txt 123456 789 [root@localhost ~]# rm -rf 123.txt rm: 无法删除"123.txt": 不允许的操作 [root@localhost ~]# chattr +i 123.txt [root@localhost ~]# echo "123456" >>123.txt -bash: 123.txt: 权限不够

[root@localhost ~]# lsattr 123.txt ##查看 ----ia---------- 123.txt [root@localhost ~]# chattr -i 123.txt ##取消i权限 [root@localhost ~]# chattr -a 123.txt ##取消a权限 [root@localhost ~]# lsattr 123.txt ---------------- 123.txt

实际应用: [root@localhost ~]# chattr +a /etc/passwd [root@localhost ~]# chattr +a /etc/shadow

##还能针对日志文件进行设置