linux-root都不能修改文件权限

昨天在工作中遇到一个超越我现有认知的知识盲区,经过自己的反思觉得,工作了这么多年,对linux系统的深度和广度掌握的还是不够。现有认知觉得,linux中只要有root,啥都能干,也确实如此,但是在某种情况下,你还不能赤裸裸的直接干,干不了还。到底是个什么问题呢?Go ahead

问题现象

在这里插入图片描述

从上图可以看到,owner 为HwHiAiUser, group 为HwHiAiUser,当前用户为HwHiAiUser,libprotobuf.so文件的权限为-r-xr-x---,目的是想要使others用户可能具有r权限,但是发现chmod不了。既然当前用户HwHiAiUser不能修改文件的权限,那么root能不能修改呢?试试!

在这里插入图片描述

发现,root都搞不定!!!重点来啦!!!

解决办法

lsattr # 查看文件属性
sudo chattr -i libprotobuf.so # 注意chattr修改文件属性需要root权限
chmod 544 libprotobuf.so

在这里插入图片描述

原理分析

首先,我们看看chattr命令到底是什么?通过man chattr查看。

NAME
chattr - change file attributes on a Linux file system

SYNOPSIS
chattr [ -RVf ] [ -v version ] [ mode ] files…

DESCRIPTION
chattr changes the file attributes on a Linux file system.
The format of a symbolic mode is ±=[aAcCdDeijsStTu].
The operator ‘+’ causes the selected attributes to be added to the existing attributes of the files; ‘-’ causes them to be removed; and ‘=’ causes them to be the only attributes that the files have.
The letters ‘aAcCdDeijsStTu’ select the new attributes for the files: append only (a), no atime updates (A), compressed ©, no copy on write ©, no dump (d), synchronous directory updates (D), extent format (e),immutable (i), data journalling (j), secure deletion (s), synchronous updates (S), no tail-merging (t), top of directory hierarchy (T), and undeletable (u).

A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

The ‘e’ attribute indicates that the file is using extents for mapping the blocks on disk. It may not be removed using chattr(1).

从上面的解释可以发现,'i’属性非常强大,如果一个文件被设置成了’i’属性,那么该文件不能被删除、改名、链接,也不能写入数据。可以发现这个属性对于系统的数据安全有着非常重要的作用,即使是root账户都不能删除、修改文件,也不能修改文件的常规ogu的rwx权限。这就是file attribute 的 immutable属性。

reference:http://e2fsprogs.sourceforge.net/ext2intro.html

new types of files inspired from the 4.4 BSD filesystem have recently been added to Ext2fs. Immutable files can only be read: nobody can write or delete them. This can be used to protect sensitive configuration files. Append-only files can be opened in write mode but data is always appended at the end of the file. Like immutable files, they cannot be deleted or renamed. This is especially useful for log files which can only grow.

上面这段话引用于ext2fs,提到了该fs提供了两种新的文件属性,immutable和append-only,immutable前面已经介绍过了,append-only是指一个文件只能以追加模式写入,这种特性适合用在系统日志文件这种场景,只能追加且不能修改以前的数据。另外,chattr目前所了解到的可支持的fs是Ext2/3/4,其他的fs是否支持,没有验证过。

最后再解释一下 文件权限-rwxrwxrwx和文件属性aAcCdDeijsStTu的区别。有人说 “文件权限是操作系统进行管理文件系统的逻辑属性,而文件属性是FS的物理属性”,我暂不知道这一说法的出处,不知这种表达是否准确。其实在中文表达上 容易将 文件属性 这一表达方式弄混淆,有时文件属性可能泛指owner、group、others以及rwxst这些,所以需要注意一下。

我的理解是 owner、group、others以及rwxst这些文件属性更 "上层"一点,而lsattr的文件属性 更"底层"一点。总之理解了其本质是什么,能明白别人说的到底是什么,然后能正确运用就好了。

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sif_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值