linux权限管理

13.权限管理

13.1 u,g,o权限讲解

u user
g group
o other
​
--- --- ---
 u   g   o
rwx r-x --x
​
r read
w write
x execute
​
[root@qfedu.com ~]# chmod 权限 文件
[root@qfedu.com ~]# chmod u-r-w a.txt
[root@qfedu.com ~]# chmod u+r a.txt
[root@qfedu.com ~]# chmod g+w a.txt
[root@qfedu.com ~]# chmod o+x a.txt
[root@qfedu.com ~]# chmod u+w,g-w,o+w a.txt
[root@qfedu.com ~]# chmod a-r a.txt    a表示ugo3个位置
[root@qfedu.com ~]# chmod -r a.txt    (建议少用)
​
4=r 2=w 1=x
rwx 4+2+1=7
r-- 4
r-x 4+1=5
745
​
[tom@qfedu.com ~]$ chmod 604 a.txt 
[tom@qfedu.com ~]$ ll a.txt
-rw----r-- 1 tom tom 44 Nov 25 11:43 a.txt
​
umask   root默认022,普通用户默认002.     用umask码计算权限目录从777开始,文件从666开始
root目录默认755,文件默认644
普通用户目录默认775,文件默认664
​

13.2设置及更改权限 chown chmod chgrp

命令格式  [前提: user,group已经存在]
[root@tianyun ~]# chown alice.hr file1              //改属主、属组
[root@tianyun ~]# chown alice     file1             //只改属主
[root@tianyun ~]# chown        .hr file1            //只改属组
[root@tianyun ~]# chown -R zhangsan.hr dir1     //-R  目录及其目录中的所有文件属主、属组都被修改
​
[root@tianyun ~]# chgrp it file1             //改文件属组 
[root@tianyun ~]# chgrp -R it dir1           //改文件属组 
​
[root@tianyun ~]# chmod u+x file1         //属主增加执行
[root@tianyun ~]# chmod a=rwx file1      //所有人等于读写执行
[root@tianyun ~]# chmod a=- file1         //所有人没有权限
[root@tianyun ~]# chmod ug=rw,o=r file1     //属主属组等于读写,其他人只读
[root@tianyun ~]# ll file1                //以长模式方式查看文件权限,查看目录ll -d
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1      //显示的结果
​
使用数字
[root@tianyun ~]# chmod 644 file1
[root@tianyun ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file11
​
0  ---          4=r 2=w 1=x
1  --x
2  -w-
3  -wx
4  r--
5  r-x
6  rw-
7  rwx

案例:rwx对文件的影响

[root@tianyun ~]# vim /home/file1
date
[root@tianyun ~]# ll /home/file1
 -rw-r--r-- 1 root root 5 7月  26 14:43 /home/file1
 [root@tianyun ~]# chmod o+x /home/file1
 [root@tianyun ~]# chmod o+w /home/file1
[root@tianyun ~]# su - alice
[alice@tianyun ~]$ cat /home/file1          //测试读
[alice@tianyun ~]$ /home/file1               //测试执行
-bash: /home/file1: 权限不够
[alice@tianyun ~]$ /home/file1
2017年 07月 26日 星期三 14:46:29 CST
[alice@tianyun ~]$ vim /home/file1         //测试写
date
ls

案例:rwx对目录的影响,对目录没有w,对文件有rwx

[root@tianyun ~]# mkdir /dir10
[root@tianyun ~]# touch /dir10/file1
[root@tianyun ~]# chmod 777 /dir10/file1 
​
[root@tianyun ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 4096 3月  11 18:37 /dir10/
[root@tianyun ~]# ll /dir10/file1 
-rwxrwxrwx. 1 root root 0 3月  11 18:37 /dir10/file1
​
[alice@tianyun ~]$ cat /dir10/file1 
[alice@tianyun ~]$ rm -rf /dir10/file1 
rm: 无法删除"/dir10/file1": 权限不够

案例:rwx对目录的影响,对目录有w,对文件没有任何权限

[root@tianyun ~]# chmod 777 /dir10/
[root@tianyun ~]# chmod 000 /dir10/file1 
[root@tianyun ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月  11 18:37 /dir10/
[root@tianyun ~]# ll /dir10/file1 
----------. 1 root root 0 3月  11 18:37 /dir10/file1
​
[alice@tianyun ~]$ cat /dir10/file1 
cat: /dir10/file1: 权限不够
[alice@tianyun ~]$ rm -rf /dir10/file1 
[alice@tianyun ~]$ touch /dir10/file2

小结

对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)

注意事项

文件: x 权限小心给予

目录: w 权限小心给予


13.3基本权限acl getfacl setfacl

文件权限管理之: ACL设置基本权限(r、w、x)

UGO设置基本权限: 只能一个用户,一个组和其他人

ACL 设置基本权限: r,w,x

格式:
setfacl -m u:username:rwx filename
setfacl -m g:groupname:rwx filename
​
[root@tianyun ~]# touch /home/test.txt
[root@tianyun ~]# ll /home/test.txt 
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
​
[root@tianyun ~]# getfacl /home/test.txt
[root@tianyun ~]# setfacl -m u:alice:rw /home/test.txt          //增加用户alice权限
[root@tianyun ~]# setfacl -m u:jack:- /home/test.txt            //增加用户jack权限
[root@tianyun ~]# setfacl -m o::rw /home/test.txt               //增加其他人的权限
​
​
格式:
getfacl filename   //查看权限
setfacl -x u:username filename     //移除filename的username用户的acl
setfacl -x g:groupname filename    //移除filename的groupname组的acl
setfacl -b filename                //移除filename的所有acl权限
​
[root@tianyun ~]# ll /home/test.txt 
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[root@tianyun ~]# getfacl /home/test.txt
​
[root@tianyun ~]# setfacl -m g:hr:r /home/test.txt
[root@tianyun ~]# setfacl -x g:hr /home/test.txt                    //删除组hr的acl权限
[root@tianyun ~]# setfacl -b /home/test.txt                         //删除所有acl权限
​
[root@tianyun ~]# man setfacl /EXAMPLES 
​
[root@tianyun ~]#  getfacl file1 |setfacl  --set-file=- file2       //复制file1的ACL权限给file2
​

14.权限管理二

14.1高级权限 suid sgid sticky

suid 4

sgid 2

sticky 1 粘滞位

字符
chmod u+s file
chmod g+s dir 
chmod o+t dir
数字
chmod 4777 file 
chmod 7777 file 
chmod 2770 dir 
chmod 3770 dir
​
案例:sticky 用户只能删除自己的文件  <针对目录>
[root@tianyun ~]# mkdir /home/dir1
[root@tianyun ~]# chmod 777 /home/dir1
测试:user1在/home/dir1建立文件, user2尝试删除!
​
[root@tianyun ~]# chmod o+t /home/dir1
[root@tianyun ~]# ll -d /home/dir1
rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1
谁可以删除:
 root
 文件的所有者
 目录的所有者
 
​
案例:sgid 新建文件继承目录属组   <针对目录>
[root@tianyun ~]# mkdir /home/hr
[root@tianyun ~]# chgrp hr /home/hr/
[root@tianyun ~]# chmod g+s /home/hr
[root@tianyun ~]# ll -d /home/hr/
drwxr-sr-x. 2 root hr 4096 Dec  5 16:03 /home/hr/
​
[root@tianyun ~]# touch /home/hr/file9
[root@tianyun ~]# ll /home/hr/
-rw-r--r--. 1 root hr   0 Dec  5 16:03 file9
​
当一个目录有了sgid权限后,在该目录新创建的文件和目录都会继承该目录的所属组。
​
​
​
当一个可执行文件赋予了suid的权限,其他可以执行该文件的用户,在执行时会临时变身为该文件的所有者的身份
1. 文件的执行者拥有执行权限,只有在执行的瞬间才能触发suid的权限
suid是一种特殊的提权方式,针对命令不针对用户。
​
suid
给普通账户提权
让普通账户可以使用本来只有root账户可以执行的命令 给命令文件添加的权限
​
sgid
只能给目录添加,添加了sgid之后,后面不管谁来这个目录下创建文件,文件的所属组都会继承其父目录的所属组
​
sticky权限   t权限
防止在一个拥有写权限的目录下误删除其他人的文件
​

14.2sudo讲解

1、 添加附加组 wheel,并且该用户有密码
    usermod -aG wheel gz3
    passwd gz3
    后续gz3就可以root身份执行命令
        执行方式 sudo 命令,需要输入用户密码确认身份,每次认证身份后能保留一段时间的认证
        
2、 visudo添加权限
    visduo命令
    在文件中添加下列配置,不限制命令的执行
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
     新增下列内容
    gz3     ALL=(ALL)      ALL                 [sudo的时候需要认证]
    gz3     ALL=(ALL)      NOPASSWD:ALL        [sudo的时候不需要认证]
    
   限制执行的命令  [命令用which 查看命令所在的路径]
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    新增下列内容
    gz3     ALL=(ALL)       /bin/cat,/bin/ls,/bin/cd
    
   用户也是需要有密码的。
   
   第一个ALL为允许使用sudo的主机,第二个括号里的ALL为使用sudo后以什么身份(目的用户身份)来执行命令;
​

14.3文件属性chattr 设置文件属性(权限),针对所有用户,包括root

[root@tianyun ~]# touch file100 file200 file300 
[root@tianyun ~]# lsattr file100 file200 file300 
 -------------e- file100 
-------------e- file200
 -------------e- file300
 
[root@tianyun ~]# man chattr
​
[root@tianyun ~]# chattr +a file100 
[root@tianyun ~]# chattr +i file200 
[root@tianyun ~]# chattr +A file300
[root@tianyun ~]# lsattr file100 file200 file300
  -----a-------e- file100
 ----i--------e- file200
 -------A-----e- file300
​
​
[root@tianyun ~]# echo 111 > file100                 //以覆盖的方式写入
​
bash: file100: Operation not permitted
[root@tianyun ~]# rm -rf file100 
rm: cannot remove `file100': Operation not permitted
[root@tianyun ~]# echo 111 >> file100               //以追加的方式写入,例如日志文件
​
[root@tianyun ~]# echo 111 > file200
bash: file200: Permission denied
[root@instructor ~]# echo 111 >> file200
bash: file200: Permission denied
[root@tianyun ~]# rm -rf file200 
rm: cannot remove `file200': Operation not permitted
​
[root@tianyun ~]# chattr -a file100
[root@tianyun ~]# chattr -i file200
[root@tianyun ~]# chattr -A file300
​
​
​
当文件赋予了a属性,那么该文件只能以追加写的方式改变文件,其他方式均不能操作
​
当文件赋予了i属性,那么改文件不可变更
​
当文件赋予了A属性,文件的atime不会每次查看都改动 [已经没有意义,新版本中都自带该功能]
​
​
​
# stat file3  查看文件元数据
  File: ‘file3’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 33618945    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2021-07-16 09:18:04.426521525 +0800
Modify: 2021-07-16 09:18:04.426521525 +0800
Change: 2021-07-16 09:20:07.871513083 +0800
 Birth: -
​
​
Atime修改的条件
    1. 距离上次atime的变更已经超过1天[24h],且再次发生查看行为 [每次修改会自动刷新24h]
    2. 文件发生修改
    
Mtime变更条件
    1. 文件内容发生变换
    
Ctime变更条件
    1. 属性变化 [权限,所有者,所属组,大小]
    
Dtime   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值