linux的6种权限

1.suid

普通用户不能访问某些文件,如果root在该文件的所有者(u)上添加s权限就可以使得普通用户在访问该文件的时候形成root的进程,这样就可以使得普通用户可以访问该文件。
eg:
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 33544 Dec 14 2019 /usr/bin/passwd
(默认添加了s权限,如果原来就有就为小写的s)

[root@localhost ~]# chmod u-s /usr/bin/passwd
[root@localhost ~]# ll /usr/bin/passwd
-rwxr-xr-x. 1 root root 33544 Dec 14 2019 /usr/bin/passwd
[root@localhost ~]# su - redhat
[redhat@localhost ~]$ passwd
Changing password for user redhat.
Current password:
New password:
Retype new password:
passwd: Authentication token manipulation error
[redhat@localhost ~]$
【进程:】
[root@localhost ~]# ps -ef | grep passwd
redhat 6046 6016 0 11:10 pts/0 00:00:00 passwd
root 6049 4999 0 11:10 pts/1 00:00:00 grep --color=auto passwd

(如果我们把s权限拿掉,redhat就无法更改自己的密码了,我们也可以看到进程里面的所有者是redhat)

在这里插入图片描述

在这里插入图片描述

注:ps -ef | grep passwd 过滤进程中的passwd

2.Sgid

任何一个用户进某一个目录下创建文件让其创建的文件的所属组为该目录的所属组(默认用户在创建文件时的所属组为该用户)
eg:
[root@localhost ~]# rm -rf /data
[root@localhost ~]# ll /data
ls: cannot access ‘/data’: No such file or directory
[root@localhost ~]# mkdir /data
[root@localhost ~]# ll /data
total 0
[root@localhost ~]# cd /data/
[root@localhost data]# ll
total 0
[root@localhost data]# cd …
[root@localhost /]# ll /data/ -d
drwxr-xr-x. 2 root root 6 Apr 4 15:05 /data/
[root@localhost /]# chmod 777 /data/
[root@localhost /]# ll /data/ -d
drwxrwxrwx. 2 root root 6 Apr 4 15:05 /data/
[root@localhost /]# cd /data/
[root@localhost data]# touch roottest01
[root@localhost data]# ll
total 0
-rw-r–r--. 1 root root 0 Apr 4 15:14 roottest01
[root@localhost data]# su - redhat
[redhat@localhost ~]$ cd /data/
[redhat@localhost data]$ redhattest01
bash: redhattest01: command not found…
Failed to search for file: /mnt/BaseOS was not found
[redhat@localhost data]$ touch redhattest01
[redhat@localhost data]$ ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 15:15 redhattest01
-rw-r–r--. 1 root root 0 Apr 4 15:14 roottest01
[redhat@localhost data]$ exit

在这里插入图片描述

[root@localhost data]# cd …
[root@localhost /]# chmod g+s /data
[root@localhost /]# ll /data/ -d
drwxrwsrwx. 2 root root 44 Apr 4 15:15 /data/
[root@localhost /]# cd /data/
[root@localhost data]# touch roottext02
[root@localhost data]# ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 15:15 redhattest01
-rw-r–r--. 1 root root 0 Apr 4 15:14 roottest01
-rw-r–r--. 1 root root 0 Apr 4 15:16 roottext02
[root@localhost data]# cd …
[root@localhost /]# chown .haha /data
chown: invalid user: ‘.haha’
[root@localhost /]# id haha
id: ‘haha’: no such user
[root@localhost /]# useradd haha
[root@localhost /]# id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha)
[root@localhost /]# chgrp haha /data
[root@localhost /]# ll /data/ -d
drwxrwsrwx. 2 root haha 62 Apr 4 15:16 /data/
[root@localhost /]# cd /data/
[root@localhost data]# touch roottext03
[root@localhost data]# ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 15:15 redhattest01
-rw-r–r--. 1 root root 0 Apr 4 15:14 roottest01
-rw-r–r--. 1 root root 0 Apr 4 15:16 roottext02
-rw-r–r--. 1 root haha 0 Apr 4 15:33 roottext03
[root@localhost data]# su - redhat
[redhat@localhost ~]$ cd /data/
[redhat@localhost data]$ touch text02
[redhat@localhost data]$ ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 15:15 redhattest01
-rw-r–r--. 1 root root 0 Apr 4 15:14 roottest01
-rw-r–r--. 1 root root 0 Apr 4 15:16 roottext02
-rw-r–r--. 1 root haha 0 Apr 4 15:33 roottext03
-rw-rw-r–. 1 redhat haha 0 Apr 4 15:34 text02
[redhat@localhost data]$
在这里插入图片描述

注明:
1.
普通用户不能修改权限
修改当前目录的权限时,最后面应该加 .
eg:
[redhat@localhost data]$ chmod 477 /data
chmod: changing permissions of ‘/data’: Operation not permitted
[redhat@localhost data]$ chmod 477 data
chmod: cannot access ‘data’: No such file or directory
[redhat@localhost data]$ cd …
[redhat@localhost /]$ chmod 477 /data
chmod: changing permissions of ‘/data’: Operation not permitted
[redhat@localhost /]$ exit
logout
[root@localhost data]# chmod 477 data
chmod: cannot access ‘data’: No such file or directory
[root@localhost data]# chmod 477 .
[root@localhost data]# ll /data/ -d
dr–rwsrwx. 2 root haha 94 Apr 4 15:34 /data/
[root@localhost data]#

2 ll /data/ -d 可以显示目录的详细情况

3.通过命令演示 sticky 的用法(chmod o+t )

用户可以删除其他用户在同一个目录下创建的文件
如果在该目录下的其他人添加t权限(o+t)就可以阻止删除
eg:
[root@localhost ~]# rm -rf /data
[root@localhost ~]# ll /data
ls: cannot access ‘/data’: No such file or directory
[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data
[root@localhost data]# ll
total 0
[root@localhost data]# chmod 777 .
[root@localhost data]# ll /data/ -d
drwxrwxrwx. 2 root root 6 Apr 4 15:59 /data/
[root@localhost data]# touch root01
[root@localhost data]# ll
total 0
-rw-r–r--. 1 root root 0 Apr 4 16:00 root01
[root@localhost data]# su - redhat
[redhat@localhost ~]$ cd /data
[redhat@localhost data]$ ll
total 0
-rw-r–r--. 1 root root 0 Apr 4 16:00 root01
[redhat@localhost data]$ touch redhat01
[redhat@localhost data]$ ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 16:01 redhat01
-rw-r–r--. 1 root root 0 Apr 4 16:00 root01
[redhat@localhost data]$ rm -rf root01
[redhat@localhost data]$ ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 16:01 redhat01
[redhat@localhost data]$

在这里插入图片描述

[root@localhost data]# touch root02
[root@localhost data]# ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 16:01 redhat01
-rw-r–r--. 1 root root 0 Apr 4 16:09 root02
[root@localhost data]# chmod o+t .
[root@localhost data]# ll /data/ -d
drwxrwxrwt. 2 root root 36 Apr 4 16:09 /data/
[root@localhost data]# su - redhat
[redhat@localhost ~]$ cd /data
[redhat@localhost data]$ ll
total 0
-rw-rw-r–. 1 redhat redhat 0 Apr 4 16:01 redhat01
-rw-r–r--. 1 root root 0 Apr 4 16:09 root02
[redhat@localhost data]$ rm -rf root02
rm: cannot remove ‘root02’: Operation not permitted
[redhat@localhost data]$

注明:
只要该目录的其他人的权限有t,不管是在修改权限之前创建的文件还是修改权限之后创建的文件,其他用户都不可以删除root所创建的文件

4.通过命令演示 facl 的用法(chattr ±i hehe 设置一个操作系统级的权限基于文件系统)

在这里插入图片描述

注明:root给自己加了一个权限,使之不能删除文件,但是。。。。
自己自己加的也有办法自己解决。

5.umask

umask 是权限掩码,用法:新创建一个文件时不想要哪个权限就把哪个权限放进掩码里面。
默认的 umask 是 022,当 umask 是 022 时创建文件和目录时注意文件和目录权限的区别。

在这里插入图片描述

6.FACL

-rw-rw-r–. 1 redhat redhat 0 Apr 4 16:01 redhat01
权限的最后面有一个小点 . 代表文件访问控制列表 (facl)
作用:精准的让某一个用户,某一个组进行权限控制。
eg:
只让haha没有该文件的权限(如果修改o就会让其他人比如说lisi也没有权限)
[root@localhost ~]# rm -rf /data
[root@localhost ~]# mkdir /data
[root@localhost ~]# chmod 777 /data
[root@localhost ~]# ll /data/ -d
drwxrwxrwx. 2 root root 6 Apr 4 16:34 /data/
[root@localhost ~]# id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha)
[root@localhost ~]# su - haha
[haha@localhost ~]$ cd /data
[haha@localhost data]$ pwd
/data
[haha@localhost data]$ touch hehe
[haha@localhost data]$ ll
total 0
-rw-rw-r–. 1 haha haha 0 Apr 4 16:37 hehe
[haha@localhost data]$ exit
logout
[root@localhost ~]# ll /data/
total 0
-rw-rw-r–. 1 haha haha 0 Apr 4 16:37 hehe
[root@localhost ~]# ll /data/ -d
drwxrwxrwx. 2 root root 18 Apr 4 16:37 /data/
[root@localhost ~]# setfacl -m u:zhangsan:- /data
setfacl: Option -m: Invalid argument near character 3
[root@localhost ~]# setfacl -m u:haha:- /data
[root@localhost ~]# ll /data/ -d
drwxrwxrwx+ 2 root root 18 Apr 4 16:37 /data/
(. 没有了,有了一个加号)
[root@localhost ~]# su - haha
[haha@localhost ~]$ cd /data/
-bash: cd: /data/: Permission denied
[haha@localhost ~]$
(这时haha这个用户就不能访问/data目录了)

只让haha这个用户访问

[root@localhost ~]# chmod 700 /data/
[root@localhost ~]# ll /data/ -d
drwx------+ 2 root root 18 Apr 4 16:37 /data/
[root@localhost ~]# setfacl -m u:haha:rwx /data/
(u换成g就变成组)
(如果还想添加一个用户进行访问:)
([root@localhost ~]# setfacl -m u:redhat:rwx /data/)
[root@localhost ~]# ll /data/ -d
drwxrwx—+ 2 root root 18 Apr 4 16:37 /data/
[root@localhost ~]# su - haha
[haha@localhost ~]$ cd /data
[haha@localhost data]$ su - redhat
Password:
[haha@localhost data]$ exit
logout
[root@localhost ~]# su - redhat
[redhat@localhost ~]$ cd /data
-bash: cd: /data: Permission denied
[redhat@localhost ~]$

去掉刚刚只能haha访问的情况
[root@localhost ~]# setfacl -x u:haha /data
[root@localhost ~]# getfacl /data
getfacl: Removing leading ‘/’ from absolute path names
#file: data
#owner: root
#group: root
user::rwx
group::rwx
mask::rwx
other::—

[root@localhost ~]#

注明:
1.Setfacl -m u:zhangsan:- /data
(setfacl 代表设置文件访问控制列 -m代表修改。U:代表用户,-代表—没有权限。权限可以换成r–,r-x,)
2.用命令getfacl 可以很清楚看到权限的情况
[root@localhost ~]# getfacl /data
getfacl: Removing leading ‘/’ from absolute path names
#file: data
#owner: root
#group: root
user::rwx
user:haha:rwx
group::rwx
mask::rwx
other::—

[root@localhost ~]#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值