第四章:权限管理
linux的文件管理权限分为读、写和执行
[root@localhost ~]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960392 8月 3 2016 /bin/bash
一.权限基本概念
1.权限对象
属主: u
属组: g
其他人: o
基本权限类型:
读: r 4
写: w 2
执行: x 1
-rwxr-xr-x.
2.共分为五个部分
-:表示文件类型
rwx:用户属主的权限
r-x:用户属组的权限
r-x:其他用户的权限
.:是否启用facl
3.权限对文件的作用
r:可获取文件的数据;
w:可修改文件的数据;
x:可将此文件运行为进程;
4.权限对目录的作用
r:可使用ls命令获取其下的所有文件列表;
w:可修改此目录下的文件列表;即创建或删除文件,包括子目录。
x:可cd至此目录中;且可使用ls -l来获取所有文件的详细属性信息
5.权限组合
练习:
rw-rw-r-- 664
rwxrwxr-x 775
rwxr-x--- 750
rw------- 600
rwxr-xr-x 755
1
2
3
4
5
6
3、相关命令
二.设置权限
1.更改文件的属主、属组 //chown , chgrp命令
[root@lxw ~]# chown alice.hr file1 //改属主、属组
[root@lxw ~]# chown alice file1 //只改属主
[root@lxw ~]# chown .hr file1 //只改属组
[root@lxw ~]# chown -R zhouchen.hr dir1 //同时改变该目录及目录下的所有文件和目录所有者和所属组 //-R 等于替归
[root@lxw ~]# chgrp it file1 //改文件属组
[root@lxw ~]# chgrp -R it dir1 //改文件属组
2.更改权限
[root@lxw ~]# chmod u+x file1 //属主增加执行
[root@lxw ~]# chmod a=rwx file1 //所有人等于读写执行
[root@lxw ~]# chmod a=- file1 //所有人没有权限
[root@lxw ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@lxw ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果
[root@tianyun ~]# chmod 644 file1 //权限644为 rw-r--r--
[root@tianyun ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file11
3.权限案例UGO
[root@lxw ~]# groupadd hr //创建用户组
[root@lxw ~]# useradd hr01 -G hr //指定用户hr01至hr组
[root@lxw ~]# useradd hr02 -G hr //指定用户hr02至hr组
[root@lxw ~]# mkdir /home/hr //创建/home/下的hr目录
[root@lxw ~]# chgrp hr /home/hr (chown .hr /home/hr)
[root@lxw ~]# chmod 770 /home/hr //给予hr目录rwxrwx---权限
[root@lxw ~]# ll -d /home/hr/ //以长格式查看hr目录权限 -d 查看目录
drwxrwx---. 2 root hr 4096 3月 13 14:26 /home/hr/
4.rwx对文件的影响
实战案例1:rwx对文件的影响
[root@lxw ~]# vim /home/file1 //vim进入file1文件
date
[root@tianyun ~]# ll /home/file1 //查看file1文件权限
-rw-r--r-- 1 root root 5 7月 26 14:43 /home/file1
[root@lxw ~]# chmod o+x /home/file1 //给予file1文件其他人x执行权限
[root@lxw ~]# chmod o+w /home/file1 //给予file1文件其他人w写权限
[root@lxw ~]# su - alice //切换至alice用户
[alice@lxw ~]$ cat /home/file1 //测试读,可执行
[alice@lxw ~]$ /home/file1 //测试执行
-bash: /home/file1: 权限不够
[alice@lxw ~]$ /home/file1
2017年 07月 26日 星期三 14:46:29 CST
[alice@tianyun ~]$ vim /home/file1 //测试写
date
ls
实战案例2:对目录没有w,对文件有rwx
[root@lxw ~]# mkdir /dir10 //创建dir10 目录
[root@lxw ~]# touch /dir10/file1 //创建file1 文件
[root@lxw ~]# chmod 777 /dir10/file1 //赋权file1 777权限
[root@lxw ~]# ll -d /dir10/ //查看diir10目录权限
drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir10/
[root@lxw ~]# ll /dir10/file1 //查看file1文件权限
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir10/file1
[alice@lxw ~]$ cat /dir10/file1 //读执行成功
[alice@lxw ~]$ rm -rf /dir10/file1 //执行权限失败
rm: 无法删除"/dir10/file1": 权限不够
实战案例3:对目录有w,对文件没有任何权限
[root@lxw ~]# chmod 777 /dir10/ //赋予dir10 777权限
[root@lxw ~]# chmod 000 /dir10/file1 //赋予file1 000权限
[root@lxw ~]# ll -d /dir10/ //查看dir10权限
drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/
[root@lxw ~]# ll /dir10/file1 //查看file1权限
----------. 1 root root 0 3月 11 18:37 /dir10/file1
[alice@lxw ~]$ cat /dir10/file1 //alice 查看r权限不够
cat: /dir10/file1: 权限不够
[alice@lxw ~]$ rm -rf /dir10/file1 // 执行x权限删除成功
[alice@lxw ~]$ touch /dir10/file2 // 写w权限创建成功
对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
注意事项
文件: x 权限小心给予
目录: w 权限小心给予
再次认识一下文件和目录:
三.基本权限ACL
文件权限管理之: ACL设置基本权限(r、w、x)
UGO设置基本权限: 只能一个用户,一个组和其他人
ACL 设置基本权限: r,w,x
ACL基本用法
设置:
当发现文件的权限后出现+ 号,说明有 facl 权限
[root@lxw ~]# touch /home/test.txt //创建test.txt
[root@lxw ~]# ll /home/test.txt //查看test.txt权限
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
[root@lxw ~]# getfacl /home/test.txt //查看acl权限
[root@lxw ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[root@lxw ~]# setfacl -m u:jack:- /home/test.txt //增加用户jack权限
[root@lxw ~]# setfacl -m o::rw /home/test.txt
[root@lxw ~]# setfacl -x g:hr /home/test.txt //删除组hr的acl权限
[root@lxw ~]# setfacl -b /home/test.txt //删除所有acl权限
[root@lxw ~]# man setfacl //man 手册 查看帮助信息 //EXAMPLES,示例
[root@lxw ~]# getfacl file1 |setfacl --set-file=- file2 //复制file1的ACL权限给file2
四.高级权限
高级权限suid,sgid,sticky
1.1.高级权限的类型
suid 4
sgid 2
sticky 1 粘滞位
1.2.设置特殊权限
a、字符
chmod u+s file //u 与文件属主拥有一样的权限
chmod g+s dir //g 与和文件属主同组的用户拥有一样的权限
chmod o+t dir // o 与其他用户拥有一样的权限
b、数字
chmod 4777 file
chmod 7777 file
chmod 2770 dir
chmod 3770 dir
suid 普通用户通过suid提权 <针对文件>
在进程文件(二进制,可执行)上增加suid权限
[root@tianyun ~]# chmod u+s /usr/bin/cat
[root@tianyun ~]# chmod u+s /usr/bin/rm
[alice@tianyun ~]$ cat /root/file1.txt
Sgid权限
chmod g+s dirname ## 当一个目录拥有sgid权限,则该目录下新创建的文件或者目录都继承该目录的属组
**Sticky 权限 **
在该目录下的文件只能有改文件的所有者和目录的所有者以及root 来删除Chmod o+t dirname 在该目录下的文件只能有 该文件的所有者 和目录
1.普通用户可以修改密码
alice /usr/bin/passwd /etc/shadow
[alice@lxw ~]$ ll /etc/shadow //查看配置文件权限
---------- 1 root root 1487 6月 4 13:43 /etc/shadow
[alice@lxw ~]$ ll /usr/bin/passwd //查看配置文件权限
-rwsr-xr-x. 1 root root 30768 2月 17 2012 /usr/bin/passwd
[alice@lxw ~]$ passwd //以普通用户修改密码
2.给用户配置sudo权限
[alice@lxw ~] visudo
· visudo大概在一百行左右** 写在root ALL=(ALL) ALL ##写在下面**
alice ALL=(ALL) ALL //用户sudo权限的用户 可以再那台机器上执行=(变身为谁 )
alice ALL=(ALL) /bin/cat,/bin/ls //命令单独配置,用“,”隔开
NOPASSWD ( NOPASSWD )免密 列:gitlab -runner ALL=(ALL) NOPASSWD:ALL%wheel ALL=(ALL) ALL % 组名 对组实行sudo提权 nopasswd
suid: 基本针对所有用户,任何用户在执行有suid权限的程序时(例如/usr/bin/rm),都是以root身份在执行。
which //查找命令
chmod u+s a.txt //与文件属主拥有一样的权限
chmod g+s dir //与文件属主同组的用户有一样的权限
chmod o+t dir //与其他用户有一样的权限
chmod 4777 a.txt
chmod 7777 a.txt
chmod 2777 a.txt
chmod 3777 a.txt
suid 普通用户通过suid提权
cat a.txt > b.txt //a.txt >覆盖b.txt >> 覆盖
lsattr a.txt //查看扩展文件特有属性
man chattr //查看man手册
chattr +i //attr这个文件不可删除、追加、改变路径、覆盖
chattr +A //Atime 限制Atime的变化 默认有
chattr +a //attr 权限 这个文件只能追加,不可覆盖和其他命令
echo 111> a.txt //追加111至a.txt内容内 >>追加
chattr -a a.txt //减除a权限
chattr -i a.txt //减除i 权限
chattr -A file300 //减除 A 权限
umask //查看当前umask权限
本文章纯属个人学习心得总结,希望大家相互学习,后续会持续跟新,谢谢大家的 观看!