• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

chattr commands in Linux with examples

801


chattr Commands - To make Important Files IMMUTABLE (Unchangeable) in Linux

A command line utility in Linux is called chattr, which is utilized to set/unset several attributes to a file is explained in this article.

chattr Syntax

# chattr [operator] [flags] [filename]

Flags and Attributes

When a file containing ‘ u‘ attribute is deleted, its information' s are saved, which let the user to ask for its deletion.
A file containing the ‘ j‘ attribute is set, where all of its datas are updated to the ext3 journal.
When a file is changed with ‘ S‘ attribute set, updated modifications take place synchronously on the disk.

Operators

&ndash Deletes the attribute to the existing attribute of the files.
+ Inserts the attribute to the existing attribute of the files.
= Keep the existing attributes of the files.

Addition of attributes on files from deletion

Using ‘ ls -l‘ command, verify whether the existing files have any attributes set.

[root@linuxhelp1 test]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Jun 10 14:30 folder
-rw-r--r--. 1 root root 0 Jun 10 14:31 secure.conf

Utilize the " + " sign for setting up an attribute, and " &ndash " sign along with the chattr command.

For preventing anyone from deleting a file, set immutable bit with +i flags on the files.

[root@linuxhelp1 test]# chattr +i folder/
[root@linuxhelp1 test]# chattr +i secure.conf

The superuser only can have the authority to set the immutable bit +i.

Using the command ‘ lsattr‘ verify the attribute.

[root@linuxhelp1 test]# lsattr 
----i----------- ./folder
----i----------- ./secure.conf
[root@linuxhelp1 test]# rm -rf folder/
rm: cannot remove ‘ folder/’ : Operation not permitted
[root@linuxhelp1 test]# mv folder/ folder1
mv: cannot move ‘ folder/’  to ‘ folder1’ : Operation not permitted
[root@linuxhelp1 test]# chmod 755 secure.conf 
chmod: changing permissions of ‘ secure.conf’ : Operation not permitted

Unsetting attribute on Files

The following is an example to unset attribute or reset permissions

[root@linuxhelp1 test]# chattr -i folder/ secure.conf

Using ‘ lsattr‘ command, verify the immutable file status.

[root@linuxhelp1 test]# lsattr 
---------------- ./folder
---------------- ./secure.conf
[root@linuxhelp1 test]# rm -rf *
[root@linuxhelp1 test]# ls -l
total 0

How to /etc/shadow files and Secure /etc/passwd

Set immutable attribute on /etc/shadow, or files /etc/passwd.

[root@linuxhelp1 test]# chattr +i /etc/passwd
[root@linuxhelp1 test]# chattr +i /etc/shadow

You will receive an error message that ‘ cannot open /etc/passwd‘ , while creating a new system user.

[root@linuxhelp1 test]# useradd user2
useradd: cannot open /etc/passwd

Append data without changing existing File data

Utilize the ‘ a‘ attribute.

[root@linuxhelp1 test]# chattr +a sample.txt 
[root@linuxhelp1 test]# lsattr sample.txt 
-----a---------- sample.txt
you will receive an error that ‘ Operation not permitted‘ , while replacing already existing content on a file example.txt.

[root@linuxhelp1 test]# echo " this is a sample file"  >  sample.txt 
bash: sample.txt: Operation not permitted

Setting up the append attribute

[root@linuxhelp1 test]# chattr -a sample.txt
[root@linuxhelp1 test]# echo " this is a sample file"  > >  sample.txt 
[root@linuxhelp1 test]# cat sample.txt 
this is a sample file
Here is the example to test ' a'  attribute mean append only.
replace contain on file.

Securing the Directories

Utilize ‘ -R‘ switch,‘ +i‘ flag and complete path of the folder, to secure the whole directory and its files.

[root@linuxhelp1 test]# chattr -R +i folder/

Now Remove/delete the folder and its files.

[root@linuxhelp1 test]# rm -rf folder/
rm: cannot remove ‘ folder/’ : Operation not permitted

Once again Utilize ‘ -R’ switch, ‘ -i’ flag and the complete path of the folder for unsetting permission.

[root@linuxhelp1 test]# chattr -R -i folder/
[root@linuxhelp1 test]# rm -rf folder/

Thus the directory is successfully removed.

Tags:
isaac
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

How to install chattr in ubuntu ?

A

Install chattr in ubuntu using the following command

apt-get install e2fsprogs

Q

is chattr tool available for Mac OS ?

A

For using on Mac use chflags command.

Q

How to /etc/shadow files and Secure /etc/passwd using chattr?

A

Set immutable attribute on /etc/shadow, or files /etc/passwd

chattr +i /etc/passwd
chattr +i /etc/shadow

Q

How to Unset attribute on Files using chattr ?

A

The following is an example to unset attribute or reset permissions

chattr -i folder/ secure.conf

Q

How to remove/delete the folder and its files using chattr ?

A

To remove/delete the folder and its files using chattr use the following commands

chattr -R -i folder/
rm -rf folder/

Related Tutorials in chattr commands in Linux with examples

Related Tutorials in chattr commands in Linux with examples

How To Configure Time using timedatectl command
How To Configure Time using timedatectl command
Jun 16, 2016
chattr commands in Linux with examples
chattr commands in Linux with examples
Jun 11, 2016
How to use Special Character in Linux
How to use Special Character in Linux
Jun 17, 2016
How To Use (!) Symbol In Linux
How To Use (!) Symbol In Linux
Jun 18, 2016
How To Play with Word and Character Counts in Linux
How To Play with Word and Character Counts in Linux
Jun 17, 2016

Related Forums in chattr commands in Linux with examples

Related Forums in chattr commands in Linux with examples

Linux
jayce class=
shasum command not found
May 5, 2017
Echo
grayson class=
how to add to last line in echo command
May 12, 2017
Advanced commands
michael class=
how to block creating new user in linux
Feb 8, 2017
Linux
david class=
difference between whereis and which command
May 4, 2017
Advanced commands
caden class=
how to use chatter command
Feb 8, 2017
Advanced commands
isaiah class=
What is the use of SUID & SGID commands
Feb 11, 2017
Linux
landon class=
how to use hwclock command
May 4, 2017
Lolcat
jayce class=
lolcat command usage
May 2, 2017

Related News in chattr commands in Linux with examples

Related News in chattr commands in Linux with examples

Intel Security's Chris Young: Attackers Not Just Compromising Home Devices, They're Weaponizing Them
Intel Security's Chris Young: Attackers Not Just Compromising Home Devices, They're Weaponizing Them
Feb 16, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Isaiah ?
What is the use of SUID & SGID commands

How to set the special permissions to the files and folders using SUID and SGID commands...

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.