Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»Chattr Command in Linux with Examples

    Chattr Command in Linux with Examples

    By RahulJuly 10, 20213 Mins Read

    The “chattr”, short for change attribute, is a command-line utility in Linux used to change attributes of a file e.g a, i. This command is primarily used to make various files immutable and undeletable for regular users.

    Advertisement

    File management is a complicated process in Linux as it is a multi-user operating system. The administrators can change the attributes of a file using the “chattr” command so it cannot be accessed and changed by anyone except the superuser. This saves the important files from accidental deletion.

    In this write-up, we will focus on how to modify the attributes of a file by using the “chattr” command. We will also learn about different flags that can be used along with the “chattr” command. But first, let’s discuss the syntax of the “chattr” command:

    chattr [OPERATOR][Flags] FILE
    

    Flags

    Here is a list of the most common flags and attributes:

    • 'a' With this attribute a file can only be opened in append mode.
    • 'i' To make a file immutable
    • 'S' Files with this attribute are synchronously updated on the disk
    • 'u' To save contents of a file when it is deleted
    • 't' To restrict tail merging
    • 'j' The data of files with this attribute is updated to ext3 journal before the file itself

    Operators

    • '+' This operator is used to add additional attributes.
    • '-' This operator is used to remove attributes of a file.
    • '=' This operator is used to make the specified attributes, the only attributes of the file.

    How to use ‘i’ attribute to make a file immutable

    The “chattr” command is often used to make files immutable. Immutable means that the file cannot be moved, renamed, or deleted.

    Here we will give the ‘i’ flag to a file named “test-file.txt” as an example:

    sudo chattr +i test-file.txt 
    

    Chattr Command in Linux 001

    You can use the “lsattr” to check the file’s attributes.

    As you can see in the screenshot above the ‘i’ attribute has been set and the file has become immutable.

    The ‘i’ attribute can also be used to make directories immutable.

    How to remove the ‘i’ attribute from the file

    Once the ‘i’ attribute has been set the file can only be changed or deleted once the attribute is removed by the root user. Use the ‘-’ operator with the option to remove the attribute:

    sudo chattr -i test-file.txt 
    

    Chattr Command in Linux 002

    How to use the ‘a’ attribute to open file in append mode

    We can use the ‘a’ attribute to open the file in the append mode. In append mode, users can only append Data on a file without changing the data that is already present in the file.

    sudo chattr +a test-file.txt 
    

    Chattr Command in Linux 003

    Now, as you can see in the screenshot below when I try to add more data into the text file by using the echo command the terminal gives me an error:

    Chattr Command in Linux 005

    But we can append data into the file by using “>>” instead of “>” operator:

    Chattr Command in Linux 005

    How to add ‘j’ attribute to update data of the file to ext3 journal

    By using the ‘j’ attribute, the data of the files attribute will be updated to the ext3 journal before the file itself:

    sudo chattr +j test-file.txt 
    

    Chattr Command in Linux 006

    Conclusion

    The “chattr” command is a very useful tool for administrators. It enables them to modify file permissions which helps in the protection of important files and prevents them from being altered.

    In this write-up, we discussed what the ‘chattr’ command is and how to use it. Moreover, we also discussed some important flags that are used along with the ‘chattr’ command.

    chattr command file lsattr permission security
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Firewalld: Common Firewall Rules and Commands

    Top 10 JQ Commands Every Linux Developer Should Know

    How to Check if a Program Exists in Linux

    View 1 Comment

    1 Comment

    1. Shashi on December 11, 2014 10:18 am

      Good article but with lots of spelling/grammatical mistakes. You should proofread your articles before you hit the publish button. It’s in your own interest. Thanks anyway!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.