A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/chattr-command-in-linux-with-examples/ below:

chattr and lsattr commands in Linux with examples

In the world of Linux, managing file permissions and attributes is crucial for maintaining a secure and organized system. Two powerful commands that help control file and directory attributes are 'chattr' and 'lsattr'. These commands are essential for administrators and advanced users who need to prevent unauthorized changes, protect critical files, and ensure the integrity of the system.

What is the 'lsattr' Command in Linux?

The lsattr command is a Unix/Linux command used to list the attributes of files or directories on a file system that supports extended attributes. Extended attributes are additional metadata associated with a file or directory beyond the traditional metadata, like permissions, ownership, and modification time.

lsattr [options] [files/directories]
What is the 'chattr' Command in Linux?

The 'chattr' (change attribute) command is a file system utility in Linux that allows users to change the attributes of files and directories. This command is particularly useful in a multi-user environment, where it's necessary to restrict modifications to critical files. By using 'chattr', you can make files immutable (undeletable or unmodifiable), restrict them to append-only mode, and apply other specific behaviors that enhance file security.

chattr [ -RVf ] [ -v version ] [ mode ] files...

Below are the different options for the chattr command: 

At the beginning of a mode string, one of the following operators must appear: 

The format of symbolic mode is: 

{+|-|=}[aAcCdDeijsStTu]

Following are the list of common attributes and associated flags can be set/unset using the chattr command: 

All the commands above are however not qualified to files and can be used on directories (Folders) as well to secure a directory from deletion or any other analogous accidents. However, while securing a directory the flag -R’ is suggested to be used in order to recursively secure all the content in the specified directory. 

Use of chattr Command: The chattr’ can be used to preserve some system files that are very important and needs to remain in the host PC no matter what. Also to make a directory undeletable or unmodifiable for users other than superuser, this is necessary. The common use of 'chattr' is as below:- 

  1. Making the file immutable: The command here made the file named file.txt immutable, hence now no operations are possible on this file until the attributes of the file are changed again.
  2. Opening the file only in append mode: The flag a’ is used to open the file only in append mode. Consequently, it can only be appended and the previous data can’t be modified.
  3. Making directories secured: The flag +i’ can be used for a directory(as shown below) to make the directory immutable. Also, the flag -R’ is used here, which makes the call recursive and all the subfiles and directories are made immutable as well.

Note: lsattr command is used to see the attributes of files in a directory. Here, it should be noted that the e flag in the file is previously set and it means that the file is using extents for mapping blocks on the disk. The extents are filesystem dependent. They are seldom removable.

lsattr Output

The output of the lsattr command typically displays a list of attributes for the specified files or directories. The attributes are represented by letters, and their meanings can vary depending on the file system and the attributes set. Here's an example of what the output might look like:

----i----------- example.txt

In this example, each character represents a specific attribute:

Attributes may be toggled on or off, represented by the presence or absence of the corresponding letter. In the example above, the i attribute is set (immutable).

The meaning of each attribute can vary between different file systems and implementations. You can refer to the manual page for lsattr (man lsattr) or the documentation for your specific file system for more details on what each attribute signifies.

lsattr Options

The lsattr command has several options that allow you to customize its behavior when listing file and directory attributes. Here is a summary of the most commonly used options:

Viewing Attributes of Files and Directories Using lsattr to View Attributes

To view the attributes of files and directories, you use the lsattr command.

Example 1: View attributes of a file

lsattr file.txt

Example 2: View attributes of a directory

lsattr -d mydirectory

The attribute string --------------e----- consists of several flags, each represented by a character in a specific position. If a flag is not set, a hyphen (-) is shown.

  1. - (position 1): No special attributes (immutable, append-only, etc.) are set.
  2. - (position 2): The file is not compressed.
  3. - (position 3): The file is not synchronous.
  4. - (position 4): The file is not immutable.
  5. - (position 5): The file is not append-only.
  6. - (position 6): The file is not being exclusively deleted.
  7. - (position 7): The file is not being marked for secure deletion.
  8. - (position 8): The file is not being marked for synchronous updates.
  9. - (position 9): The file is not indexed.
  10. - (position 10): The file is not a journal file.
  11. - (position 11): The file is not a directory with a hash tree.
  12. - (position 12): The file does not have a direct access node.
  13. e (position 13): This flag means the file is using extents for mapping the blocks on disk. Extents are a way to improve large file performance by reducing fragmentation.
  14. - (position 14): No special attribute is set.
  15. - (position 15): No special attribute is set.

Example 3: View attributes of all files and directories recursively

lsattr -R
Changing Attributes with chattr

The chattr command is used to change file attributes. You need superuser (root) permissions to change some attributes.

chattr [options] [attributes] [files/directories]
Making Files Immutable

An immutable file cannot be modified, deleted, or renamed, and no data can be appended to it.

Example 1: Make a file immutable

sudo chattr +i example.txt

Example 2 : Make a directory immutable

sudo chattr +i -R mydirectory

To remove the immutable attribute, use:

sudo chattr -i example.txt
Making Files Append-Only

An append-only file allows only adding data to the end of the file; it cannot be deleted or overwritten.

Example 1: Make a file append-only

sudo chattr +a example.txt

To remove the append-only attribute, use:

sudo chattr -a example.txt


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4