Wednesday, November 17, 2021

basic chattr command in Linux with examples

Chattr stand for "Change the Attribute" is a command  that is used to set certain attributes to a file or directory in Linux system to secure accidental deletion or modification to the files and folders, even root user can't be delete.

Note:- root user only has access to set or unset attributes of files and directories using chattr command.

 Syntax of chattr

$ chattr [operator] [Attributes] [filename]


Attributes

  • i   - Immutable -  Means file can not be rename, can not be create symbolic link, can not be execute, can not be write, only superuser can unset the attribute.
  • a   - Append  - Means the file only opened for writing in append mode. It cannot be deleted or renamed.


Operator

  •    +     Symbol Adds the attribute to the file
  •    -    Symbol Removes the attribute to the file
  •        Symbol Keep the existing attributes that the  files have.


Options 

  •    -R      Recursively change attributes of directories/files
  •    -V      Provide verbose output and display program version.

 

1. Apply attributes on files to secure from deletion

$ sudo chattr -V +i demo/

$ sudo chattr -V +i demo_file.txt

$ sudo chattr -V +a demo_1/

$ sudo chattr -V +a demo_file_1.txt



2. List the attribute of files/directory with command ‘lsattr‘.

$ lsattr 

$ lsattr -R 



3. Now check to delete forcefully, rename or change the permissions, but it will give output  “Operation not permitted“.

$ sudo rm -rf demo/  demo_file.txt

$ mv demo/  Remo/

$ chmod  777 demo/ 


4. Remove attribute on the Files/directory by just use " - symbol "

$ sudo chattr -V -i demo/

$ sudo chattr -V -i demo_file.txt

$ sudo chattr -V -a demo_1/

$ sudo chattr -V -a demo_file_1.txt




No comments:

Post a Comment

testing