Linux 权限解析

一、权限分类

1.基本权限

2.特殊权限

3.高级权限

4.隐藏权限

二、基本权限

权限对象

属主----------->u--------rwx

属组----------->g--------rwx

其他人-------->o--------rwx

基本权限类型

读:r -------4

写:w-------2

执行:x-----1

2.1 更改权限

chown :改变文件或者目录的属主和属组

chmod:为文件和目录设置访问权限

chown

修改文件的属主、属组

[root@localhost opt]# chown xiaozhang.xiaozhang file
[root@localhost opt]# ll -d file
-rw-r--r--. 1 xiaozhang xiaozhang 0 8月  31 04:01 file

修改文件的属主

[root@localhost opt]# ll -d file
-rw-r--r--. 1 root xiaozhang 0 8月  31 04:01 file

修改文件的属组

[root@localhost opt]# chown .root file
[root@localhost opt]# ll -d
[root@localhost opt]# ll -d file
-rw-r--r--. 1 root root 0 8月  31 04:01 file

修改目录及目录下面文件的属主属组

[root@localhost opt]# chown  -R xiaozhang.xiaozhang dir
[root@localhost opt]# ll -d dirdrwxr-xr-x. 2 xiaozhang xiaozhang 6 8月  30 15:50 dir

chmod

a.使用符号

[root@localhost opt]# chmod u=r,g=rw,o=x file
[root@localhost opt]# ll -d file
-r--rw---x. 1 root root 0 8月  31 04:01 file
[root@localhost opt]# chmod u+x,o+r file
[root@localhost opt]# ll -d file
-r-xrw-r-x. 1 root root 0 8月  31 04:01 file
[root@localhost opt]# chmod a-r file
[root@localhost opt]# ll -d file
---x-w---x. 1 root root 0 8月  31 04:01 file

b.使用数字

r=4 w =2 x=1

[root@localhost opt]# chmod 777 file
[root@localhost opt]# ll -d file
-rwxrwxrwx. 1 root root 0 8月  31 04:01 file
[root@localhost opt]# chmod 664 file
[root@localhost opt]# ll -d file
-rw-rw-r--. 1 root root 0 8月  31 04:01 file
[root@localhost opt]# chmod 000 file
[root@localhost opt]# ll -d file
----------. 1 root root 0 8月  31 04:01 file

修改目录及目录下面的文件

chmod -R  目录

三、高级权限

3.1高级权限的类型

suid=====4 提权 只针对二进制文件生效

sgid=====2 继承属组权限 只针对目录设置

sticky====1 权限控制

3.2suid

在可执行的命令文件上增加suid权限,一旦提权,所有用户都可以像root一样执行命令

suid提权

提权前

[root@localhost ~]# su xiaozhang
[xiaozhang@localhost root]$ cat file
cat: file: 权限不够

提权后

[root@localhost ~]# chmod u+s /usr/bin/cat
[root@localhost ~]# su xiaozhang
[xiaozhang@localhost root]$ cat file
123

取消提权

[root@localhost ~]# chmod u-s /usr/bin/cat

 3.3 SGID

SGID主要用在目录上-----如果用户在此目录下具有w权限的话,使用者在此目录下建立新文件,则创建的这个文件的群组与此目录的群组相同。

授权前

[root@localhost ~]# mkdir dir
[root@localhost ~]# ls
anaconda-ks.cfg  dir  file
[root@localhost ~]# chmod 775  dir
[root@localhost ~]# chown .hr dir
[root@localhost ~]# touch dir/file
[root@localhost ~]# ll -d dir/file
-rw-r--r--. 1 root root 0 8月  31 05:09 dir/file

授权后

[root@localhost ~]# chmod g+s dir
[root@localhost ~]# touch dir/file2
[root@localhost ~]# ll -d dir/file2
-rw-r--r--. 1 root hr 0 8月  31 05:11 dir/file2

给dir目录授权后,在其目录下创建的文件的属组跟随dir的属组(不管root,还是普通用户)

3.4 SBIT

这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。
SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。

[root@localhost opt]# chmod o+t dir
[root@localhost opt]# chmod o+w dir
[root@localhost opt]# ll -d /opt/dir
drwxr-xrwt. 2 root hr 6 8月  30 15:50 /opt/dir
[root@localhost opt]# su xiaozhang
[xiaozhang@localhost opt]$ cd dir
[xiaozhang@localhost dir]$ touch file
[root@localhost opt]# su tom
[tom@localhost opt]$ cd dir
[tom@localhost dir]$ rm -f file
rm: 无法删除"file": 不允许的操作

四、sudo

sudo和suid的区别

sudo : 有针对性,针对某个用户能够以root的身份执行某些

详见Linux 权限 sudo_泡面在做烫发的博客-CSDN博客

五、隐藏权限

隐藏权限防止root误删

i  系统不允许对这个文件进行任何的修改

a 系统只允许在这个文件之后追加数据,不允许覆盖这个文件

A 不要修改这个文件最后的访问时间

创建文件并附权

[root@localhost opt]# touch file1 file2 file3
[root@localhost opt]# chattr +i file1
[root@localhost opt]# chattr +a file2
[root@localhost opt]# chattr +A file3
[root@localhost opt]# lsattr file1
----i----------- file1
[root@localhost opt]# lsattr file2
-----a---------- file2
[root@localhost opt]# lsattr file3
-------A-------- file3

案例一 i

[root@localhost opt]# rm -f file1
rm: 无法删除"file1": 不允许的操作
[root@localhost opt]# echo '123' > file1
-bash: file1: 权限不够
[root@localhost opt]# echo '1234' >> file1
-bash: file1: 权限不够
[root@localhost opt]# cat file1
[root@localhost opt]# mv file1 file
mv: 无法将"file1" 移动至"file": 不允许的操作
[root@localhost opt]# setfacl -m u:xiaozhang:rwx file1
setfacl: file1: 不允许的操作
[root@localhost opt]# chmod 777 file1
chmod: 更改"file1" 的权限: 不允许的操作

案例二 a

[root@localhost opt]# rm -f file2
rm: 无法删除"file2": 不允许的操作
[root@localhost opt]# mv file2 file
mv: 无法将"file2" 移动至"file": 不允许的操作
[root@localhost opt]# echo '123' > file2
-bash: file2: 不允许的操作
[root@localhost opt]# echo '1234' >> file2
[root@localhost opt]# cat file2
1234

案例三A

[root@localhost opt]# stat file3
  文件:"file3"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:16784948    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2023-09-01 19:25:36.814698250 +0800
最近更改:2023-09-01 19:25:36.814698250 +0800
最近改动:2023-09-01 19:26:25.138243828 +0800
创建时间:-
[root@localhost opt]# echo '123' > file3
[root@localhost opt]# echo '123123' >> file3
[root@localhost opt]# mv file3 file
[root@localhost opt]# cat file
123
123123
[root@localhost opt]# stat file
文件:"file"
  大小:11              块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:16784948    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2023-09-01 19:25:36.814698250 +0800
最近更改:2023-09-01 19:47:10.840585997 +0800
最近改动:2023-09-01 19:47:18.943984056 +0800
创建时间:-

扩展:

权限掩码 umask

root 用户默认的最高权限 

目录---777 文件----666

查看umask

[root@qfedu.com ~]# umask
0022 root账户默认
0002 普通用户默认

#通过计算得出root用户创建目录和文件的权限为:
也是现在root用户创建完目录和文件的默认权限:
目录:755
文件:644

普通用户的创建完目录和文件的默认权限为

目录:775

文件: 664

访问控制权限

setfacl 针对个人设置权限,一个人查看一个文件的权限

getfacl 查看权限

已知文件:
[root@xiaoming tmp]# ll
-rw-r--r-- 1 root root    0 11月 26 11:25 a.txt
如果希望只有xiaoming用户可以rwx操作/tmp/a.txt文件
那么我们可以这样做
setfacl -m u:xiaoming:rwx /tmp/a.txt
-m 设置facl权限
u:  用户,也可以指定组 g
xiaoming: 需要指定的用户
rwx: 权限

让我们来看下现在xiaoming是否拥有这个权限:
1、尝试对该文件进行操作
2、getfacl /tmp/a.txt
[root@xiaoming /tmp]# getfacl a.txt 
# file: a.txt
# owner: root
# group: root
user::rw-
user:xiaoming:rwx
group::r--
mask::rwx
other::r--
我们可以看到 xiaoming 拥有了对该文件的rwx权限

那么如何收回权限呢
方法1、
setfacl -m u:xiaoming:--- a.txt
方法2、
setfacl -x u:xiaoming a.txt

我们也可以设置该文件为所有人所有组访问
setfacl -m ::rwx a.txt

案例一:

只 给一个普通用户xiaozhang file文件的满权限,并查看

[root@localhost opt]# setfacl -m u:xiaozhang:rwx file
[root@localhost opt]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:xiaozhang:rwx
group::r--
mask::rwx
other::r--

(setfacl只给了xiaozhang满权限,其他用户权限不变)

收回权限

[root@localhost opt]# setfacl -m u:xiaozhang:---  file
[root@localhost opt]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:xiaozhang:---
group::r--
mask::r--
other::r--

案例二:

在root用户下,将chmod设置为000,能否给其他文件设置权限,如果不能,请修改

[root@localhost opt]# which  chmod
/usr/bin/chmod
[root@localhost opt]# ll -d /usr/bin/chmod
-rwxr-xr-x. 1 root root 58592 8月  20 2019 /usr/bin/chmod
[root@localhost opt]# chmod 000 /usr/bin/chmod
[root@localhost opt]# getfacl /usr/bin/chmod
getfacl: Removing leading '/' from absolute path names
# file: usr/bin/chmod
# owner: root
# group: root
user::---
group::---
other::---
[root@localhost opt]# setfacl  -m u:root:rwx /usr/bin/chmod
[root@localhost opt]# getfacl /usr/bin/chmod
getfacl: Removing leading '/' from absolute path names
# file: usr/bin/chmod
# owner: root
# group: root
user::---
user:root:rwx
group::---
mask::rwx
other::---
[root@localhost opt]# chmod 755 /usr/bin/chmod

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值