Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to prevent files and directories from being accidentally deleted or modified in the Linux system

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to prevent files and directories from being accidentally deleted or modified in the Linux system, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Prevent files and directories from being accidentally deleted and modified in Linux

By default, the chattr command is available in most modern Linux operating systems.

The default syntax is:

Chattr [operator] [switch] [file]

Chattr has the following operators:

Operator + to append the specified attribute to the existing attribute of the file

Operator -, delete the specified attribute

Operator =, directly set the file property to the specified property

Chattr provides different properties, namely aAcCdDeijsStTu. Each character represents a specific file attribute.

A-data can only be added to the file

A-access time for files or directories that are not updated

C-compress files or directories and store them

C-does not apply replication on write mechanism (CoW)

D-setting files cannot be backup targets for dump programs

D-synchronize directory updates

E-extend format storage

I-the file or directory cannot be changed

J-set this parameter so that when a file system is mounted through the mount parameter: data=ordered or data=writeback, the file is first recorded in the log when it is written.

P-project hierarchical structure

S-safely delete files or directories

S-instantly update files or directories

T-No tail merge

T-top-level directory hierarchy

U-cannot be deleted

In this tutorial, we will discuss the use of two attributes, an and I, which can be used to prevent files and directories from being deleted. That's our theme today, isn't it? Let's get started!

Prevent files from being accidentally deleted and modified

I first create a file.txt file in my current directory.

$touch file.txt

Now, I will apply the I attribute to the file to make it unchangeable. This means that you cannot delete or modify the file, even if you are the owner of the file and a root user.

$sudo chattr + I file.txt

Use the lsattr command to check the existing attributes of the file:

$lsattr file.txt

Output:

-I, file.txt.

Now, try to delete the file with a normal user:

$rm file.txt

Output:

# cannot delete files. Illegal operation rm: cannot remove 'file.txt': Operation not permitted

Let me try the sudo privilege:

$sudo rm file.txt

Output:

# cannot delete files. Illegal operation rm: cannot remove 'file.txt': Operation not permitted

Let's try appending the content to this text file:

$echo 'Hello Worldlings' > > file.txt

Output:

# illegal operation bash: file.txt: Operation not permitted

Try the sudo privilege:

$sudo echo 'Hello Worldlings' > > file.txt

Output:

# illegal operation bash: file.txt: Operation not permitted

You should have noticed that we cannot delete or modify this file, not even root users or file owners.

To undo the property, use-I.

$sudo chattr-I file.txt

Now, this immutable attribute has been deleted. You can delete or modify this file now.

$rm file.txt

Similarly, you can restrict directories from being accidentally deleted or modified, as described in the following section.

Prevent directories from being accidentally deleted and modified

Create a dir1 directory and put the file file.txt.

$mkdir dir1 & & touch dir1/file.txt

Now, make the directory and its contents (file.txt files) immutable:

$sudo chattr-R + I dir1

In command

-R-Recursive makes the dir1 directory and its contents unmodifiable

+ I-make the directory unmodifiable

Now, try deleting this directory, either with a normal user or with sudo privileges.

$rm-fr dir1 $sudo rm-fr dir1

You will see the following output:

# cannot delete 'dir1/file.txt': illegal operation rm: cannot remove' dir1/file.txt': Operation not permitted

Have you succeeded in trying to append content to the file with the echo command? Of course, you can't.

Undo this property and enter:

$sudo chattr-R-I dir1

Now you can delete or modify the contents of this directory as usual.

Prevents files and directories from being accidentally deleted, but allows append operations

We now know how to prevent files and directories from being accidentally deleted and modified. Next, we will prevent the file from being deleted but only allow the file to be appended. It means that you cannot edit or modify the existing data of the file, or rename the file or delete the file, you can only open the file using append mode.

To set append properties to a file or directory, we do this as follows:

For files:

$sudo chattr + a file.txt

For directories:

$sudo chattr-R + a dir1

A file or directory with the a property set can only be opened for writing in append mode.

Add some content to this file to test whether it works.

$echo 'Hello Worldwide' > > file.txt$ echo 'Hello Worldwide' > > dir1/file.txt

View the contents of a file using the cat command

$cat file.txt$ cat dir1/file.txt

Output:

Hello World!

You will see that you can now add content. It means that we can modify the file or directory.

Now let's try to delete this file or directory.

$rm file.txt

Output:

# cannot delete file 'file.txt': illegal operation rm: cannot remove' file.txt': Operation not permitted

Let's try to delete this directory:

$rm-fr dir1/

Output:

# cannot delete file 'dir1/file.txt': illegal operation rm: cannot remove' dir1/file.txt': Operation not permitted

Delete this property and execute the following command:

For files:

$sudo chattr-R-a file.txt

For directories:

$sudo chattr-R-a dir1/

Now you can delete or modify this file and directory as usual.

For more details, see the man page.

Man chattr thank you for reading this article carefully. I hope the article "how to prevent files and directories from being accidentally deleted or modified in the Linux system" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report