linux下的用户管理及linux下的权限管理

编程入门 行业动态 更新时间:2024-10-11 09:19:47

linux下的用户管理及linux下的<a href=https://www.elefans.com/category/jswz/34/1771295.html style=权限管理"/>

linux下的用户管理及linux下的权限管理

1.权限查看及读取

文件权限是系统最底层安全设定方法之一,它保证文件可以被可用的用户做相应操作

1.权限查看

命令功能
ls -l file##查看文件权限 ls -l =ll
ls -ld dir##查看目录权限 ls -ld =ll -d

2.权限的读取

文件的属性被叫做文件的元数据(meta data)"
一种元数据用1个byte来记录内容"

文件权限信息

    - | rw-r--r-- | . | 1 | root | root | 0 |Apr 12 10:57 |westos[1]    [2]      [3] [4]  [5]    [6]   [7]      [8]       [9]

目录权限信息

    d | rw-r--r-- | . | 2 | root | root | 0 | Apr 12 10:57 |westosdir[1]   [2]      [3] [4]  [5]    [6]   [7]     [8]          [9]

[1] 表示文件类型
- 普通文件

[root@foundation50 mnt]# ls -l westos
-rw-r--r--. 1 root root 0 Aug 12 23:29 westos

d 目录

[root@foundation50 mnt]# ls -ld westosdir/
drwxr-xr-x. 2 root root 6 Aug 12 23:29 westosdir/

l 软连接 (理解为快捷方式)

[root@foundation50 mnt]# ls -l /home/westos/Desktop/westos
l rwxrwxrwx. 1 root root 6 Aug 12 23:29 /home/westos/Desktop/westos -> westos

b 块设备

[root@foundation50 Desktop]# ls -l /dev/sdb
brw-rw----. 1 root disk 8, 16 Aug 13 10:37 /dev/sdb

c 字符设备 (显示字符的设备,/dev/pts/0 特点是把内容写到 /dev/pts/0 就会显示在屏幕上

   [root@foundation50 mnt]# ls -l /dev/pts/0crw--w----. 1 westos tty 136, 0 Aug 12 23:27 /dev/pts/0

s socket套接字 (相当于程序的一道门,进入程序的接口)
p 管道 |

[2] 用户权限
u 用户 g 所属组 o 其他人

   rw-|r--|r--         u    g    o         

[3] 系统的selinux开启 (内核性的加强防火墙,安全上下文)
ls -Z olelee 查看文件的安全上下文,当selinux关闭时再建立文件.会消失

[4] 如果是目录表示目录中子目录的个数,如果是文件表示硬连接的个数
软连接:

硬连接:

目录:
[root@foundation50 mnt]# ls -ld westosdir/
drwxr-xr-x. 2 root root 6 Aug 13 11:13 westosdir/
[root@foundation50 mnt]# mkdir westosdir/westos
[root@foundation50 mnt]# ls -ld westosdir/
drwxr-xr-x. 3 root root 20 Aug 13 11:16 westosdir/
文件:
[root@foundation50 mnt]# ls -l westos
-rw-r--r--. 1 root root 0 Aug 13 11:14 westos
[root@foundation50 mnt]# cp westos oldlee
[root@foundation50 mnt]# ls -i westos oldlee 查看节点号
868637908 oldlee  868636607 westos
[root@foundation50 mnt]# rm -fr oldlee 
[root@foundation50 mnt]# ln westos oldlee   硬连接
[root@foundation50 mnt]# ls -l westos 
-rw-r--r--. 2 root root 0 Aug 13 11:14 westos   数字由2变为1
[root@foundation50 mnt]# ls -l oldlee 
-rw-r--r--. 2 root root 0 Aug 13 11:14 oldlee
[root@foundation50 mnt]# ls -i westos oldlee  查看节点号 ,一个节点对应了两块数据区域
868636607 oldlee  868636607 westos

[5] 文件拥有者
[6] 文件拥有组
[7] 对于文件:文件内容大小

[root@foundation50 mnt]# echo 1 > westos
[root@foundation50 mnt]# ls -l westos  1个字符加一个换行符
-rw-r--r--. 2 root root 2 Aug 13 11:46 westos  

对于目录:目录中子文件或子目录的元数据大小(- | rw-r–r-- | . | 1 | root | root | 0 |Apr 12 10:57 |westos,前8个每个用一个字节来记录,文件名几个字节就是几个)

 [root@foundation50 mnt]# ls -ld westosdir/
drwxr-xr-x. 2 root root 6 Aug 13 16:01 westosdir/   原数据大小为6(.  ..)
[root@foundation50 mnt]# touch westosdir/11   在westosdir目录里建一个文件
[root@foundation50 westosdir]# ls -l 11  文件11的属性大小为10
-rw-r--r--. 1 root root 0 Aug 13 16:03 11
[root@foundation50 mnt]# ls -ld westosdir/   
drwxr-xr-x. 2 root root 16 Aug 13 16:03 westosdir/   元数据大小为10+6=16

[8] 文件最后一次被修改的时间, 包括文件属性和文件内容最后一次修改的时间

[root@foundation50 mnt]# ls -l westoslee 
-rw-r--r--. 2 root root 2 Aug 13 11:46 westoslee
[root@foundation50 mnt]# echo 123 > westoslee  修改文件内容
[root@foundation50 mnt]# ls -l westoslee 
-rw-r--r--. 2 root root 4 Aug 13 16:28 westoslee   时间改变
[root@foundation50 mnt]# mv westoslee  lee  修改文件名字时间也会被改变

3.文件用户用户组管理

命令功能
chown username file更改文件拥有者
chgrp groupname file更改文件拥有组
chown username:groupname file同时更改文件的拥有者和拥有组
chown/chgrp -R user/group dir更改目录本身及目录中内容的拥有者或者拥有组
实验环境建立
[root@foundation50 Desktop]# cd /linux/
[root@foundation50 linux]# mkdir westosdir
[root@foundation50 linux]# touch westos{1..3}
[root@foundation50 linux]# touch westosdir/westosfile{1..3}
监控命令:watch ls -R /linux[root@foundation50 ~]# chown lee /linux/westos1  更改文件拥有者
[root@foundation50 ~]# chgrp liu /linux/westos1    更改文件拥有组
[root@foundation50 ~]# chown lee.liu /linux/westos2  同时更改文件拥有者拥有组
[root@foundation50 ~]# chown lee:liu /linux/westos3  同时更改文件拥有者拥有组
[root@foundation50 ~]# chown lee /linux/westosdir/  更改i目录本身文件
[root@foundation50 ~]# chgrp liu /linux/westosdir/
[root@foundation50 ~]# chown -R lee /linux/westosdir/  更改目录本身拥有者及目录中内容(包括文件和目录)的拥有者
[root@foundation50 ~]# chgrp  -R lee /linux/westosdir/
[root@foundation50 ~]# chown -R  root:root /linux/

4.用户身份匹配

user>group>other

5.权限类型

命令用途
- 权限未开启
r可读对于文件:可以读取文件内容 对于目录:可以ls列出目录中的文件
w可写 对于文件:可以更改文件内容;对于目录:可以在目录中新建或者删除文件
x可执行 对于文件:可以用文件名称调用文件内记录的程序 ;对于目录:可以进入目录中
[root@foundation50 ~]# ls -l /linux/westos1
-rwx------. 1 lee westos 5 Aug 15 15:35 /linux/westos1   
[root@foundation50 ~]# id lee  可以看出lee这个用户对westos1这个文件享有用户权力 
uid=1001(lee) gid=1001(lee) groups=1001(lee)
[root@foundation50 ~]# su -- westos    切换用户westos
[westos@foundation50 root]$ id westos   可以看出westos用户对文件westos1享有组的权力
uid=1000(westos) gid=1000(westos) groups=1000(westos)
[root@foundation50 ~]# usermod -G westos root  
[root@foundation50 ~]# id root
uid=0(root) gid=0(root) groups=0(root),1000(westos)   可以看出root用户对westos1这个文件享有组的权力
注意:组的权力不仅要看用户组,还要看所有组
[lee@foundation50 ~]$ chmod u+r /linux/westos1
[lee@foundation50 ~]$ cat /linux/westos1
[lee@foundation50 ~]$ echo 123 > /linux/westos1  没有写的权限
bash: /linux/westos1: Permission denied
[lee@foundation50 ~]$ chmod u+w /linux/westos1  加上写的权限
[lee@foundation50 ~]$ echo 123 > /linux/westos1
[lee@foundation50 ~]$ cat /linux/westos1 
123
[lee@foundation50 ~]$ echo date > /linux/westos1  
[lee@foundation50 ~]$ /linux/westos1
bash: /linux/westos1: Permission denied    没有执行权限
[lee@foundation50 ~]$ chmod u+x /linux/westos1  加上执行权限
[lee@foundation50 ~]$ /linux/westos1   date命令就可以执行了
Sun Aug 15 15:36:21 CST 2021[lee@foundation50 root]$ ls -ld /linux/westosdir/  
d---------. 2 lee root 63 Aug 13 22:27 /linux/westosdir/
[lee@foundation50 root]$ ls /linux/westosdir/     没有读的权力
ls: cannot open directory '/linux/westosdir/': Permission denied
[lee@foundation50 root]$ chmod u+r /linux/westosdir/   加上读的权力
[lee@foundation50 root]$ ls /linux/westosdir/  可以读取目录里面的文件了
ls: cannot access '/linux/westosdir/westosfile1': Permission denied
ls: cannot access '/linux/westosdir/westosfile2': Permission denied
ls: cannot access '/linux/westosdir/westosfile3': Permission denied
westosfile1  westosfile2  westosfile3注意对于目录来说写的程序要建立在执行权力之上,没有执行权力就进不去门和谈写
[lee@foundation50 root]$ chmod u+x /linux/westosdir/  加上执行权力
[lee@foundation50 root]$ cd /linux/westosdir/   可以进入这个门了,也可以查看门里的东西了
[lee@foundation50 westosdir]$ ls
westosfile1  westosfile2  westosfile3 [lee@foundation50 westosdir]$ rm -fr westosfile1  删除文件没有权限
rm: cannot remove 'westosfile1': Permission denied  
[lee@foundation50 linux]$ chmod u+w westosdir/   加入写的权限
[lee@foundation50 linux]$ cd westosdir/
[lee@foundation50 westosdir]$ rm -fr westosfile1   可以删除[lee@foundation50 westosdir]$ chmod ugo=--- /linux/westos2   不能更改别的用户的文件
chmod: changing permissions of '/linux/westos2': Operation not permitted

6、设定普通权限的方法

命令功能
chmod设定文件权限

6.1、复制权限

命令功能
chmod – reference westos2 westos1westos1复制westos2的权限
-rwx------. 1 lee  root  5 Aug 15 15:35 westos1
-rw-r--r--. 1 root root  0 Aug 13 16:43 westos2
将westos2的文件权限复制给westos1
[root@foundation50 ~]# chmod --reference /linux/westos2 /linux/westos1
-rw-r--r--. 1 lee  root  5 Aug 15 15:35 westos1
-rw-r--r--. 1 root root  0 Aug 13 16:43 westos2

6.2 字符方式更改文件的权力

[root@foundation50 ~]# chmod u+x /linux/westos1
[root@foundation50 ~]# chmod u-w /linux/westos1
[root@foundation50 ~]# chmod ug+w /linux/westos1
[root@foundation50 ~]# chmod u-r,g-w,o+x /linux/westos1
[root@foundation50 ~]# chmod u=rx,go=--- /linux/westos1

6.3 数字方式设定权限

只要记住r、w、x 分别代表数字几即可

命令用途
r4
w2
x1
[root@foundation50 ~]# chmod 500 /linux/westos1    wx-|---|---
[root@foundation50 ~]# chmod 630 /linux/westos1   rw-|wx-|---
[root@foundation50 ~]# chmod 777 /linux/westos1  rwx|rwx|rwx
[root@foundation50 ~]# chmod 000 /linux/westosdir/   更改目录的权限
[root@foundation50 ~]# chmod -R 640 /linux/westosdir/  更改目录的权限及目录里面文件的权限

7.系统默认权限设定

系统本身存在的意义共享资源
从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高
既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放 把不安全的权力默认保留

7.1如何保留权力

umask表示系统保留权力

命令功能
umask查看保留权力
命令功能
umask 权限值临时设定系统预留权力

文件默认权限 = 777-umask-111 (022是系统预留的,111是文件系统预留的)
目录默认权限 = 777-umask

注:umask值越大系统安全性越高

7.2、umask临时更改

umask 077 :设定(在设置之后当前新建的文件和目录的值就是所设定,当退出当前的程序之后,再次建立就会和之前一样,也就是说临时设定只对当前的程序起作用)
用umask :查看

7.3 、永久更改 (编辑系统中的配置文件)

(1)更改shell系统配置文件

  vim /etc/bashrc          ##shell系统配置文件74         if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then75              umask 002                  #普通用户的umask76         else77              umask 022 -- 077           #root用户的umask78         fi

(2)更改系统配置文件

vim /etc/profile               ##系统环境配置文件59          if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then60               umask 002                 #普通用户的umask 61          else62               umask 022 -- 077          #root用户的umask  63          fi(text 命令:  gt 表示的是>  ,lt 表示 <  ,eq 表示 =  ,不等号是 nt) 
( gn 是初始组的名字 ,un 是用户名字 )

(3)调用

source /etc/bashrc   ##source作用时使我们更改的内容立即被系统识别(source 后面只能呢个跟一个文件,不能加多个)
source /etc/profile

8、特殊权限

现象:一个用户既能删除自己目录里的文件,也能删除别人目录里的文件,这样在生产中不合理,所以引入粘制位

8.1、stickyid 粘制位 (只针对目录)

如果一个目录stickyid开启,那么这个目录中的文件,只能被文件所有人删除,别人目录中的文件不能别删除

如何设置粘制位 权限

命令用户
chmod 1原始权限 dir设置之后最后一位变为t
chmod o+t dir设置之后最后一位变为t
[root@foundation50 Desktop] # mkdir /linux/pub
[root@foundation50 Desktop]#  chmod 777 /linux/pub/
[root@foundation50 Desktop]# su -- lee   用lee用户建立leefile文件
[lee@foundation50 Desktop]$ touch /linux/pub/leefile
[lee@foundation50 Desktop]$ su -- linux  用linux用户建立linuxfile文件
[linux@foundation50 Desktop]$ touch /linux/pub/linuxfile
[linux@foundation50 Desktop]$ rm -fr /linux/pub/linuxfile  linux用户可以删除自己在public里的文件
[linux@foundation50 Desktop]$ rm -fr /linux/pub/leefile    linux用户可以删除别人在public里的文件
[root@foundation50 Desktop]# chmod o+t /linux/pub/ 开启粘制位
[root@foundation50 Desktop]# ls -ld /linux/pub/  
drwxrwxrwt. 2 root root 38 Aug 16 22:08 /linux/pub/    t出现,粘制位以开启
[root@foundation50 Desktop]# su -- lee
[lee@foundation50 Desktop]$ rm -fr /linux/pub/leefile   可以删除自己的文件
[lee@foundation50 Desktop]$ rm -fr /linux//pub/linuxfile  别人的文件删除不了
rm: cannot remove '/linux//public/linuxfile': Operation not permitted   报错!!

8.2 强制位sgid

针对目录: 目录中新建的文件自动归属到目录的所属组中
设定:
chmod 2源文件权限 dir
chmod g+s dir

[root@foundation50 Desktop]# mkdir /linux/sc
[root@foundation50 Desktop]# chgrp shengchan /linux/sc/
[root@foundation50 Desktop]# chmod 1770 /linux/sc/
[lee@foundation50 Desktop]$ ls -ld /linux/sc/   sc目录的所属组为shengchan
drwxrwx--T. 2 root shengchan 18 Aug 17 11:45 /linux/sc/
[root@foundation50 Desktop]# su -- lee   切换用户lee
[lee@foundation50 Desktop]$ id lee
uid=1001(lee) gid=1001(lee) groups=1001(lee),1004(shengchan)
[lee@foundation50 Desktop]$ touch /linux/sc/file  用lee用户在sc目录里建文件
[lee@foundation50 Desktop]$ ls -l /linux/sc/file 
-rw-rw-r--. 1 lee lee 0 Aug 17 15:27 /linux/sc/file  sc目录里新建的文件没有归属到sc的所属组中   ,而是谁建立的就是谁的组,不合理!!!
[root@foundation50 Desktop]# chmod g+s /linux/sc/  开启强职位
[root@foundation50 Desktop]# ls -ld /linux/sc/
drwxrws--T. 2 root shengchan 18 Aug 17 11:45 /linux/sc/  s出现,强职位已开启
[root@foundation50 Desktop]# su -- lee  切换用户
[lee@foundation50 Desktop]$ touch /linux/sc/file1 在sc建立文件
[lee@foundation50 Desktop]$ ls -l /linux/sc/file1
-rw-rw-r--. 1 lee shengchan 0 Aug 17 15:44 /linux/sc/file1  sc目录中新建的文件自动归属到sc目录的所属组中   

针对文件:
只针对二进制的可执行文件(c程序)(二进制文件:源代码编写的程序通过编译形成的可执行的文件)
当运行二进制可执行文件时都是文件拥有组身份运行,和执行用户无关

[root@foundation50 Desktop]# ls -l /bin/cat   二进制cat程序的所有人为root,所有组为root
-rwxr-xr-x. 1 root root 51856 Jan 11  2019 /bin/cat 
[root@foundation50 Desktop]# su -- westos 切换用户
[westos@foundation50 Desktop]$ /bin/cat  开启程序产生进程
[westos@foundation50 Desktop]$ ps ax -o user,group,command | grep cat  查看cat的进程,可以发现谁开启的程序,就是以这个用户组的身份运行
( ps (进程扫描)  ax  (a = all ,x 表示在后台运行) -o (表示要查看进程的什么东西) user ,group ,comm(进程的名字) | grep cat (要查看cat 用grep 对cat 进行过滤))
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
westos   westos   cat
westos   westos   cat
westos   westos   /usr/bin/nautilus --gapplication-service
westos   westos   /usr/bin/gedit --gapplication-service
westos   westos   /bin/cat   westos开启的程序,就是以westos组的身份运行
westos   westos   grep --color=auto cat
[root@foundation50 Desktop]# chmod g+s /bin/cat  开启强制位
[root@foundation50 Desktop]# ls -l /bin/cat 
-rwxr-sr-x. 1 root root 51856 Jan 11  2019 /bin/cat  s出现,强制位以开启
[root@foundation50 Desktop]# su -- westos  切换用户
[westos@foundation50 Desktop]$ /bin/cat  运行cat程序
[westos@foundation50 Desktop]#  ps ax -o user,group,command |grep cat  查看进程
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
westos   root     cat
westos   root     cat
westos   root     /bin/cat       变成root,以文件拥有组身份运行
root     root     grep --color=auto cat

8.3 冒险位suid

冒险位 (在超级用户下做)
只针对二进制的可执行文件(c程序)
当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关

设定: chmod 4原属性 file
chmod 4755 和 chmod u + s是一个效果,结果是当用ls -l 查看文件时,文件名称变为了红色(用户+ s 为红色,组 +s 为黄色,不加的时候为绿色)

[westos@foundation50 Desktop]$ /bin/cat
[root@foundation50 ~]# ps ax -o user,group,command | grep cat
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
westos   root     cat
westos   root     cat
westos   westos   /usr/bin/nautilus --gapplication-service
westos   westos   /usr/bin/gedit --gapplication-service
westos   root     /bin/cat  用户是谁开启的进程就是谁的 westos
root     root     grep --color=auto cat
[root@foundation50 ~]# chmod u+s /bin/cat   开启冒险位
[root@foundation50 ~]# su -- westos
[westos@foundation50 ~]#  ps ax -o user,group,command | grep cat
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
westos   root     cat
westos   root     cat
westos   westos   /usr/bin/gedit --gapplication-service
root     root     /bin/cat   文件拥有者身份运行,和执行用户无关
root     root     grep --color=auto c

9、acl列表权限(开启acl

用户功能
setfacl -m u: (指定用户)lee:rw (权力) westosfile (对那个文件或者目录)#设定权限
setfacl -m g:westos:rw westosfile设定组权限 g表示组
setfacl -x u:lee westosfile删除列表中的lee
setfacl -x g:caiwu westosfile删除列表中的财务组
setfacl -b westosfile关闭列表功能(不论是否开启都可以关闭)
getfacl /linux/westosfile查看权限列表
[root@foundation50 Desktop]# chmod 777 /linux/westos/
[root@foundation50 Desktop]# setfacl -m u:westos:--- /linux/westos/   指定westos用户权限   
[root@foundation50 Desktop]# su -- westos
[westos@foundation50 Desktop]$ cd /linux/
[westos@foundation50 linux]$ rm -fr westos/    
rm: cannot remove 'westos/': Permission denied  
[root@foundation50 linux]# setfacl -m u:lee:--- /linux/westos/     指定lee用户权限
[root@foundation50 linux]# su -- lee
[lee@foundation50 linux]$ rm  -fr westos/
bash: rm-fr: command not found...
[lee@foundation50 linux]$ rm -fr westos/
rm: cannot remove 'westos/': Permission denied
[root@foundation50 linux]# setfacl -m g:shengchan:--- /linux/westos/   指定组权限
[root@foundation50 linux]# getfacl /linux/westos/   查看权限列表
getfacl: Removing leading '/' from absolute path names
# file: linux/westos/
# owner: root
# group: root
user::rwx
user:westos:---
user:lee:---
group::rwx
group:shengchan:---
mask::rwx
other::rwx
[root@foundation50 linux]# setfacl -x u:lee /linux/westos/  删除lee
[root@foundation50 linux]# setfacl -x g:shengchan /linux/westos/  删除shengchan组
[root@foundation50 linux]# getfacl /linux/westos/
getfacl: Removing leading '/' from absolute path names
# file: linux/westos/
# owner: root
# group: root
user::rwx
user:westos:---
group::rwx
mask::rwx
other::rwx

10、acl 权限优先级

拥有者 > acl特殊指定用户 > 权限多的组 >权限少的组 > 其他

11、acl mask 控制

mask是能够赋予指定用户权限的最大阀值
----rwx—+ 1 root caiwu 0 Apr 18 09:03 westosfile
当+出现时,此处为mask 的权限值,而不在代表组的权限值

问题
当设定完毕文件的acl列表之后用chmod缩小了文件拥有组的权力
mask会发生变化
恢 复: setfacl -m m::权限 文件

[root@foundation50 Desktop]# mkdir /linux/westosfile
[root@foundation50 Desktop]# ls -ld /linux/westosfile/
drwxr-xr-x. 2 root root 6 Aug 18 10:50 /linux/westosfile/
[root@foundation50 Desktop]# setfacl -m u:westos:rwx /linux/westosfile/  指定用户westos权限 ,开启acl列表
[root@foundation50 Desktop]# ls -ld /linux/westosfile/
drwx**rwx**r-x+ 2 root root 6 Aug 18 10:50 /linux/westosfile/   +号出现acl列表已开启 代表mask权限
[root@foundation50 Desktop]# getfacl /linux/westosfile/  查看acl列表
getfacl: Removing leading '/' from absolute path names  
# file: linux/westosfile/
# owner: root
# group: root
user::rwx
user:westos:rwx
group::r-x
mask::rwx   
other::r-x
[root@foundation50 Desktop]# chmod g-x /linux/westosfile  用chmod减少组的执行权限
[root@foundation50 Desktop]# getfacl /linux/westosfile/ 查看acl列表
getfacl: Removing leading '/' from absolute path names
# file: linux/westosfile/
# owner: root
# group: root
user::rwx
user:westos:rwx			#effective:rw-   
group::r-x			#effective:r--
mask::rw-   
other::r-x
[root@foundation50 Desktop]# setfacl -m m:rwx /linux/westosfile/  恢复msak权限
[root@foundation50 Desktop]# getfacl /linux/westosfile/
getfacl: Removing leading '/' from absolute path names
# file: linux/westosfile/
# owner: root
# group: root
user::rwx
user:westos:rwx
group::r-x
mask::rwx  恢复到rwx
other::r-x
[root@foundation50 Desktop]# setfacl -m g::0 /linux/westosfile/  如果要减少组的权限则用setfacl设置
[root@foundation50 Desktop]# getfacl /linux/westosfile/
getfacl: Removing leading '/' from absolute path names
# file: linux/westosfile/
# owner: root
# group: root
user::rwx
user:westos:rwx
group::---    权限设置为0
mask::rwx
other::r-x

12、acl 列表的默认权限

命令用途
setfacl -m u:lee:rwx /mnt/westosdir只对于/mnt/westosdir目录本身生效
setfacl -Rm u:lee:rwx /mnt/westosdir对于/mnt/westosdir目录和目录中已经存在的内容生效
注意:以上的命令之针对与存在的文件生效,新建文件是不会被设定的
命令用途
setfacl -m d:u:lee:rwx /mnt/westosdir/针对与/mnt/westosdir目录中新建文件生效 ;d: 表示只对新建立的起作用
[root@foundation50 Desktop]# mkdir /linux/westosdir  
[root@foundation50 Desktop]# touch /linux/westosdir/file
[root@foundation50 Desktop]# touch /linux/westosdir/file1
[root@foundation50 Desktop]# touch /linux/westosdir/file2
[root@foundation50 Desktop]# setfacl -m u:westos:rwx /linux/westosdir/  只是目录本身开启了acl权限,目录里面的文件没有开启
Every 2.0s: ls -lR /l...  foundation50.ilt.example.com: Wed Aug 18 15:28:42 2021/linux/:
total 0
drwxrwxr-x+ 2 root root 44 Aug 18 15:24 westosdir  /linux/westosdir:
total 0
-rw-r--r--. 1 root root 0 Aug 18 15:24 file
-rw-r--r--. 1 root root 0 Aug 18 15:24 file1
-rw-r--r--. 1 root root 0 Aug 18 15:24 file2
[root@foundation50 Desktop]# setfacl -Rm u:westos:rwx /linux/westosdir/  目录本身和里面的文件都开启了acl列表权限
Every 2.0s: ls -lR /l...  foundation50.ilt.example.com: Wed Aug 18 15:35:43 2021/linux/:
total 0
drwxrwxr-x+ 2 root root 44 Aug 18 15:24 westosdir/linux/westosdir:
total 0
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file1
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file2[root@foundation50 Desktop]# touch /linux/westosdir/file3  开启acl列表后再新建文件
Every 2.0s: ls -lR /l...  foundation50.ilt.example.com: Wed Aug 18 15:37:35 2021/linux/:
total 0
drwxrwxr-x+ 2 root root 57 Aug 18 15:36 westosdir/linux/westosdir:
total 0
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file1
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file2
-rw-r--r--. 1 root root 0 Aug 18 15:36 file3  新建的文件没有开启acl列表
如何让新建的文件也生效acl列表权限??
[root@foundation50 Desktop]# setfacl -m d:u:westos:rwx /linux/westosdir/   -d 表示对新文件起作用
[root@foundation50 Desktop]# touch /linux/westosdir/file4  新建文件
Every 2.0s: ls -lR /l...  foundation50.ilt.example.com: Wed Aug 18 15:42:56 2021/linux/:
total 0
drwxrwxr-x+ 2 root root 70 Aug 18 15:40 westosdir/linux/westosdir:
total 0
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file1
-rw-rwxr--+ 1 root root 0 Aug 18 15:24 file2
-rw-r--r--. 1 root root 0 Aug 18 15:36 file3
-rw-rw-r--+ 1 root root 0 Aug 18 15:40 file4 新建的文件也开启了acl列表

13、attr权限

问题:超级用户即使没有权力他还是可以新建以及删除文件和目录
解决:attr权限可以限制所有用户
设置方法:

命令功能
chattr +i file不能作任何的更改 (谁都不可以改,但是可以看)
chattr +a能添加不能删除
lsattr dir/file查看attr权限
lsatter -d dir查看目录的权力
[root@foundation50 Desktop]# mkdir /linux/test
[root@foundation50 Desktop]# ls -ld /linux/test/
drwxr-xr-x. 2 root root 6 Aug 18 16:01 /linux/test/
[root@foundation50 Desktop]# touch /linux/test/file
[root@foundation50 Desktop]# rm -fr /linux/test/file 
[root@foundation50 Desktop]# chattr +a /linux/test/  能添加不能删除
[root@foundation50 Desktop]# lsattr -d /linux/test/
-----a-------------- /linux/test/  生效
[root@foundation50 Desktop]# touch /linux/test/file  能建立
[root@foundation50 Desktop]# rm -fr /linux/test/file   不能删除
rm: cannot remove '/linux/test/file': Operation not permitted  
[root@foundation50 Desktop]# chattr -a /linux/test/  减去a
[root@foundation50 Desktop]# rm -fr /linux/test/file  就可以删除文件
[root@foundation50 Desktop]# chattr +i /linux/test/  不能作任何更改,只能看
[root@foundation50 Desktop]# lsattr -d /linux/test/    查看
----i--------------- /linux/test/
[root@foundation50 test]# touch /linux/test/file   不能建立
touch: setting times of '/linux/test/file': No such file or directory  
[root@foundation50 test]# rm -fr /linux/test/file   不能删除
rm: cannot remove '/linux/test/file': Operation not permitted

练习题 : 新建目录/sc 、 /cw 、 /js 、/pub
/sc 目录是生产部门的数据存储目录,只能被生产部门的人员读写,并且在 sc 中建立的文件都属于生产部门;
/cw 目录是财务部门的数据存储目录,只能被财务部门的人员读写,并且在 cw 中建立的文件都属于财务部门;
/ js 目录是技术部门的数据存储目录,只能被技术部门的人员读写,并且在 js 中建立的文件都属于技术部门;
/pub 为公司人员公共目录,可以被公司任何员工读写,但是只能删除自己的文件。
westosadmin 用户可以对/sc 、 /cw 、 /js 、/pub 做任何操作。

[root@westoslinux Desktop]# mkdir /sc /cw /js /pub
[root@westoslinux Desktop]# chgrp shengchan /sc
[root@westoslinux Desktop]# chgrp jishu /js
[root@westoslinux Desktop]# chgrp caiwu /cw
[root@westoslinux Desktop]# chmod ningt /sc /cw /js
[root@westoslinux Desktop]# chmod 1777 /pub
[root@westoslinux Desktop]# ls -ld /sc /cw /js /pub
drwxrws--T 2 root caiwu     6 Jan 29 21:54 /cw
drwxrws--T 2 root jishu     6 Jan 29 21:54 /js
drwxrwxrwt 2 root root      6 Jan 29 21:54 /pub
drwxrws--T 2 root shengchan 6 Jan 29 21:54 /sc

更多推荐

linux下的用户管理及linux下的权限管理

本文发布于:2023-07-28 19:49:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权限   用户   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!