In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the chattr command in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to chattr command
One command we need to use here is chattr, which is available in most Linux distributions, so I won't go into detail about its installation. Its basic usage is as follows:
$chattr operator properties file name
For operators, there are three types:
+: add attributes to the file
-: remove file attributes
=: set only properties of the file
Note that the attributes we are talking about here are not the corresponding system attributes of the file, but the attributes assigned to the file by chattr. There are two attributes to be discussed in this article:
A-allow attaching content to the file
I-protected mode (deletion or modification is not allowed)
However, its properties can be set a lot, and interested partners can take a look at its man manual.
$man chattr
Prevent files from being mistakenly deleted or modified
If we now have a national treasure important document file.txt, now we use chattr to protect it. Here, we add the + I attribute to the file:
$sudo chattr + I file.txt
We can then use the lsattr command to view its properties. A similar output is as follows:
$lsattr file.txt-iMurray Murray-file.txt
Now, let's give it a try and delete that file:
$rm file.txt rm: cannot remove 'file.txt': Operation not permitted
Huh? Deletion is not allowed? Is there not enough authority?
All right, let me sudo!
$sudo rm file.txt rm: cannot remove 'file.txt': Operation not permitted
I X, still can't delete it?
Let's try again and modify the contents of the file.
$echo 'hello worldview' > > file.txt bash: file.txt: Operation not permitted
As you can see, the file still cannot be modified.
And even if you delete the file manually from the GUI interface, you still can't stand it.
So, as you can see, this file is now well protected and can neither be deleted (not in any way) nor modified.
So how do we get rid of this protection? It's simple, just add the-I option.
$sudo chattr-I file.txt
Now that the file is back as it is, we can modify it if we want to, or delete it if we want to delete it.
$echo 'Hello Worldwide' > > file.txt $cat file.txt Hello World! $rm file.txt
Prevent folders from being mistakenly deleted or modified
The above is about protecting files, so how to protect folders?
In fact, the same is true, using the + I option. Suppose we now have a dir1 directory with a file.txt file in it. Let's protect this folder.
$sudo chattr-R + I dir1
Here, we use the-R option to indicate that all files (including subdirectories) in the directory can be applied recursively.
Now, let's also test whether it can be deleted or modified.
$rm-rf dir1 $sudo rm-rf dir1 rm: cannot remove 'dir1/file.txt': Operation not permitted $echo' hello worldview'> > dir1/file.txt bash: file.txt: Operation not permitted
So like files, we successfully protected the folder.
Prevent files / directories from being deleted, but allow appending content
Now we know how to prevent the file / directory from being deleted or modified by mistake, but what if we don't want the contents of the file to be modified, but allow others to append the content at the end of the file?
At this point we need to use the + an option.
For files:
$sudo chattr + a file.txt
For the directory:
$sudo chattr-R + a dir1
Now, let's confirm that the file can be appended.
$echo 'Hello Worldlings' > > file.txt $echo 'Hello Worldlings' > > dir1/file.txt
Let's use the cat command to check the contents again:
$cat file.txt Hello World! $cat dir1/file.txt Hello World!
As you can see, files can be appended.
However, file.txt and dir1/file.txt still cannot be deleted.
If you want to remove the appendable attributes, you can use the-an option.
For files:
$sudo chattr-R-a file.txt
For the directory:
$sudo chattr-R-a dir1/ Thank you for your reading! This is the end of the article on "how to use chattr commands in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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: 280
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.