chattr: command not found

The chattr command is used to change the attributes of a file or directory. The following table describes some of the options of the chattr command.

Option Description
-R Recursively change the attributes of directories and their contents.
-v {version} Set the version number of a file.
+i Mark the file as read-only, or immutable. Requires superuser privileges.
-i Remove the read-only, or immutable, attribute of the file. Requires superuser privileges.

The syntax of the chattr command is:

# chattr [-R] [-v {version}] [+-{attributes}] {file/directory names}

If you encounter the below error while running the chattr command:

chattr: command not found

you may install below package as per your choice of distribution:

Distribution Command
OS X brew install e2fsprogs
Debian apt-get install e2fsprogs
Ubuntu apt-get install e2fsprogs
Alpine apk add e2fsprogs
Arch Linux pacman -S e2fsprogs
Kali Linux apt-get install e2fsprogs
CentOS yum install e2fsprogs
Fedora dnf install e2fsprogs
Raspbian apt-get install e2fsprogs

Removing the -i (immutable) attribute from the files

To Remove “i” attribute use below command.

# chattr -i geek.txt    # Unset "i" attribute

After removing the attribute you will see the permission section will become blank.

# lsattr geek.txt     
--------------- geek.txt

Making a directory immutable using chattr

Now let’s try to Secure a directory by changing it’s attribute recursively using chattr command. Here I have a directory named data and everyone have full access to that directory recursively. Refer the sample output below.

# mkdir data
# chmod -R 777 data/
# ls -l
total 4
drwxrwxrwx. 2 root root 4096 Apr 24 04:25 data

Now set attribute to that directory.

# chattr +i data/
# lsattr 
----i--------e- ./data

You can also set attribute Recursively using -R option with chattr.

# chattr -R +i data/

After setting the attribute to the directory now try to delete, move or create a file, I am sure you will not allowed to do any one of that. Refer the sample output below.

# rmdir data/        # Deleting the Directory
rmdir: failed to remove `data/': Operation not permitted
# rm -rf data/        # Deletiing the Directory Forcefully
rm: cannot remove `data': Operation not permitted
# mv data/ mydata       # Moving the Directory
mv: cannot move `data/' to `mydata': Operation not permitted
# cd data/
# cat > test.txt       # Creating a File in the directory
bash: test.txt: Permission denied
Related Post