网站搜索

在 Linux 中使重要文件不可变(不可更改)的 5 个“chattr”命令


chattr(更改属性)是一个命令行 Linux 实用程序,用于在 Linux 系统中设置/取消设置文件的某些属性,以防止重要文件和文件夹的意外删除或修改,即使您已登录以 root 用户身份登录。

在 Linux 本机文件系统中,即 ext2、ext3、ext4、btrfs 等支持所有标志,尽管所有标志不会支持所有非本机 FS。一旦使用 chattr 命令设置了属性,即使拥有完全权限,也无法删除或修改文件/文件夹。

这对于在系统文件(例如包含用户信息的 passwd 和影子文件)中设置属性非常有用。

chattr 的语法
chattr [operator] [flags] [filename]
属性和标志

以下是可以使用 chattr 命令设置/取消设置的常见属性和关联标志的列表。

  1. 如果使用“A”属性集访问文件,则其 atime 记录不会更新。
  2. 如果使用“S”属性集修改文件,则更改会在磁盘上同步更新。
  3. 文件被设置了‘a’属性,只能以附加模式打开进行写入。
  4. 文件设置有“i”属性,无法修改(不可变)。意味着不重命名,不创建符号链接,不执行,不可写,只有超级用户可以取消设置属性。
  5. 设置具有“j”属性的文件,其所有信息在更新到文件本身之前都会更新到 ext3 日志。
  6. 文件设置了‘t’属性,没有尾部合并。
  7. 运行转储过程时,具有属性“d”的文件将不再是备份的候选者。
  8. 当删除具有“u”属性的文件时,会保存其数据。这使得用户能够请求取消删除。
操作员
  1. + :将该属性添加到文件的现有属性中。
  2. :删除文件现有属性的属性。
  3. = :保留文件现有的属性。

在这里,我们将演示一些 chattr 命令示例,用于设置/取消设置文件和文件夹的属性。

1. 如何为文件添加属性以防止删除

为了演示目的,我们分别使用了文件夹 demo 和文件 important_file.conf。在设置属性之前,请确保使用“ls -l”命令验证现有文件是否设置了任何属性。您看到结果了吗,目前没有设置任何属性。

[root@tecmint tecmint]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Aug 31 18:02 demo
-rwxrwxrwx. 1 root root 0 Aug 31 17:42 important_file.conf

要设置属性,我们使用 + 符号,要取消设置,请使用 chattr 命令的 符号。因此,让我们使用 +i 标志在文件上设置不可变位,以防止任何人删除文件,即使 root 用户也无权删除它。

[root@tecmint tecmint]# chattr +i demo/
[root@tecmint tecmint]# chattr +i important_file.conf

注意:不可变位+i只能由超级用户(即root)用户设置或具有sudo权限的用户可以设置。

设置不可变位后,让我们使用命令“lsattr”验证属性。

[root@tecmint tecmint]# lsattr
----i----------- ./demo
----i----------- ./important_file.conf

现在,尝试强制删除、重命名或更改权限,但不允许,提示“不允许操作”。

[root@tecmint tecmint]# rm -rf demo/
rm: cannot remove âdemo/â: Operation not permitted
[root@tecmint tecmint]# mv demo/ demo_alter
mv: cannot move âdemo/â to âdemo_alterâ: Operation not permitted
[root@tecmint tecmint]# chmod 755 important_file.conf
chmod: changing permissions of âimportant_file.confâ: Operation not permitted

2. 如何取消文件属性

在上面的示例中,我们已经了解了如何设置属性以保护文件并防止文件被意外删除,在此示例中,我们将了解如何重置(取消设置属性)权限并允许使用 -i 标志。

[root@tecmint tecmint]# chattr -i demo/ important_file.conf

重置权限后,使用“lsattr”命令验证文件的不可变状态。

[root@tecmint tecmint]# lsattr
---------------- ./demo
---------------- ./important_file.conf

您在上面的结果中看到“-i”标志被删除,这意味着您可以安全地删除howtoing文件夹中的所有文件和文件夹。

[root@tecmint tecmint]# rm -rf *

[root@tecmint tecmint]# ls -l
total 0

3. 如何保护 /etc/passwd 和 /etc/shadow 文件

在文件 /etc/passwd/etc/shadow 上设置不可变属性,可以使它们免受意外删除或篡改,并且还将禁用用户帐户创建。

[root@tecmint tecmint]# chattr +i /etc/passwd
[root@tecmint tecmint]# chattr +i /etc/shadow

现在尝试创建一个新的系统用户,您将收到错误消息,指出“无法打开/etc/passwd”。

[root@tecmint tecmint]# useradd tecmint
useradd: cannot open /etc/passwd

这样您就可以对重要文件或系统配置文件设置不可变的权限以防止删除。

4. 追加数据而不修改文件上的现有数据

假设您只想允许每个人将数据附加到文件上,而不更改或修改已输入的数据,您可以使用“a”属性,如下所示。

[root@tecmint tecmint]# chattr +a example.txt

[root@tecmint tecmint]# lsattr example.txt
-----a---------- example.txt

设置追加模式后,只能以追加模式打开文件写入数据。您可以按如下方式取消设置附加属性。

[root@tecmint tecmint]# chattr -a example.txt

现在尝试替换文件 example.txt 上已有的内容,您将收到错误消息“不允许操作”。

[root@tecmint tecmint]# echo "replace contain on file." > example.txt
-bash: example.txt: Operation not permitted

现在尝试在现有文件 example.txt 上附加新内容并验证它。

[root@tecmint tecmint]# echo "replace contain on file." >> example.txt
[root@tecmint tecmint]# cat example.txt
Here is the example to test 'a' attribute mean append only.
replace contain on file.

5. 如何保护目录

为了保护整个目录及其文件,我们使用“-R”(递归)开关和“+i”标志以及文件夹的完整路径。

[root@tecmint tecmint]# chattr -R +i myfolder

设置递归属性后,尝试删除该文件夹及其文件。

[root@tecmint tecmint]# rm -rf myfolder/
rm: cannot remove 'myfolder/': Operation not permitted

要取消设置权限,我们使用相同的“-R”(递归)开关和“-i”标志以及文件夹的完整路径。

[root@tecmint tecmint]# chattr -R -i myfolder

就是这样!要了解有关 chattr 命令属性、标志和选项的更多信息,请使用手册页。