{"id":5566,"date":"2018-07-18T17:00:05","date_gmt":"2018-07-18T13:00:05","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-chattr-command\/"},"modified":"2018-07-18T17:00:05","modified_gmt":"2018-07-18T13:00:05","slug":"linux-chattr-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-chattr-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux chattr Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Many a time, a Linux machine is used by different users. So there are chances that these users access a common set of files. This opens up door to problems like accidental deletion or editing of important files, something which you &#8211; as an administrator &#8211; would definitely not want to happen.<\/p>\n<p>Thankfully, there exists a command &#8211; dubbed <strong>chattr<\/strong> &#8211; that&#8217;s developed to help you in specifically these kinds of scenarios. In this tutorial, we will discuss this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on Ubuntu 16.04 LTS.<\/p>\n<h2 id=\"linux-chattr-command\">Linux chattr command<\/h2>\n<p>Basically, the chattr command is used to change file attributes on a Linux file system. Following is its syntax:<\/p>\n<p class=\"command\">chattr [ -RVf ] [ -v version ] [ mode ] files&#8230;<\/p>\n<p>And here&#8217;s what the man page says about it:<\/p>\n<pre readability=\"43\"> chattr changes the file attributes on a Linux file system.<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The format of a symbolic mode is +-=[aAcCdDeijsStTu].<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The\u00a0 operator\u00a0 '+'\u00a0 causes\u00a0 the\u00a0 selected attributes to be added to the<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 existing attributes of the files; '-' causes them to\u00a0 be\u00a0 removed;\u00a0 and<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 '=' causes them to be the only attributes that the files have.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The\u00a0 letters\u00a0 'aAcCdDeijsStTu' select the new attributes for the files:<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 append only (a), no atime updates (A), compressed (c), no copy on write<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (C), no dump (d), synchronous directory updates (D), extent format (e),<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 immutable (i), data journalling (j), secure deletion\u00a0 (s),\u00a0 synchronous<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 updates\u00a0 (S),\u00a0 no tail-merging (t), top of directory hierarchy (T), and<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 undeletable (u).<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The following attributes are read-only, and may be listed by\u00a0 lsattr(1)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 but\u00a0 not\u00a0 modified\u00a0 by\u00a0 chattr:\u00a0 compression\u00a0 error (E), huge file (h),<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 indexed directory (I), inline data (N), compression raw access (X), and<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 compressed dirty file (Z).<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Not\u00a0 all\u00a0 flags\u00a0 are supported or utilized by all filesystems; refer to<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filesystem-specific man pages such as btrfs(5), ext4(5), and xfs(5) for<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 more filesystem-specific details.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the chattr command works.<\/p>\n<h2 id=\"q-how-to-use-chattr-command\">Q1. How to use chattr command?<\/h2>\n<p>Suppose you want to make a file read-only. So all you have to do is to run the chattr command with +i option and the name of the file as input.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">chattr -i test.txt<\/p>\n<p>Following screenshot shows no other operation was successful on the file once it became read-only using chattr.<\/p>\n<p><a class=\"fancybox\" id=\"img-chattr-read-only\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/chattr-read-only.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples.png\" alt=\"How to use chattr command\" width=\"493\" height=\"161\" title=\"\"><\/a><\/p>\n<p><strong>Note<\/strong>: As you would have already observed, you need to have root privileges to use the chattr command.<\/p>\n<h2 id=\"q-how-to-remove-readonly-restriction-imposed-by-chattr\">Q2. How to remove read-only restriction imposed by chattr?<\/h2>\n<p>This is simple &#8211; all you have to do is to use the -i option instead of +i. For example:<\/p>\n<p class=\"system command\">chattr -i test.txt<\/p>\n<p><a class=\"fancybox\" id=\"img-chattr-i-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/chattr-i-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to remove read-only restriction imposed by chattr\" width=\"366\" height=\"74\" title=\"\"><\/a><\/p>\n<p>So you can see the read-only factor got removed with the -i option.<\/p>\n<h2 id=\"q-how-to-provide-appendonly-permission-to-a-file\">Q3. How to provide append-only permission to a file?<\/h2>\n<p>Sometimes, you may not want complete restriction on a file. What I mean is, you may want to provide users append-only access to a file, so that new info can be added, but existing info cannot be deleted or edited. This is also possible using chattr through the +a option.<\/p>\n<p class=\"command\">chattr +a test.txt<\/p>\n<p><a class=\"fancybox\" id=\"img-chattr-a-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/chattr-a-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-2.png\" alt=\"How to provide append-only permission to a file\" width=\"485\" height=\"140\" title=\"\"><\/a><\/p>\n<p>So you can see that we could append to the file now, but could not edit existing information in the file as well as delete the file. To reverse this behavior, just use the -a option.<\/p>\n<p class=\"command\">chattr -a test.txt<\/p>\n<h2 id=\"q-how-to-apply-a-restriction-using-chattr-to-all-files-in-a-directory\">Q4. How to apply a restriction using chattr to all files in a directory?<\/h2>\n<p>This can be done using the flag -R, which lets you recursively change attributes of directories and their contents. For example, if you want to make all files inside the <em>test-dir<\/em> directory as read-only, use the chattr command in the following way:<\/p>\n<p class=\"command\">chattr -R +i .\/test-dir\/<\/p>\n<p>The following screenshot shows the read-only restriction was successfully applied to all files inside the directory.<\/p>\n<p><a class=\"fancybox\" id=\"img-chattr-recursive\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/chattr-recursive.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-3.png\" alt=\"How to apply a restriction using chattr to all files in a directory\" width=\"494\" height=\"105\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-check-chattr-attributes-applied-on-files\">Q5. How to check chattr attributes applied on files?<\/h2>\n<p>Until now, to check if a chattr attribute was successfully applied, we tried performing operations like editing the file or deleting it. But there&#8217;s a separate command that lets you easily see if the attributes were applied or not. The command in question is <strong>lsattr<\/strong>.<\/p>\n<p class=\"command\">lsattr [FILENAME]\n<p>For example, the following screenshot shows lsattr output clearly suggesting the &#8216;i&#8217; attribute was applied to all files in the directory.<\/p>\n<p><a class=\"fancybox\" id=\"img-lsattr\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/lsattr.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-4.png\" alt=\"How to check chattr attributes applied on files\" width=\"308\" height=\"71\" title=\"\"><\/a><\/p>\n<p>Just to reconfirm, here&#8217;s the output after the -i option was used.<\/p>\n<p><a class=\"fancybox\" id=\"img-chattr-recursive-confirm-new\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/chattr-recursive-confirm-new.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-5.png\" alt=\"output after the -i option was used\" width=\"500\" height=\"107\" title=\"\"><\/a><\/p>\n<p>So you can see in the screenshot above, the read-only attribute was removed from all files.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>You will likely agree that chattr is must-know command line tool if you are a system admin, or manage users on a Linux machine in general. Effectively using the command can save you from a lot of hassle. This article should be enough to get you started with the command. Once you&#8217;ve practiced the examples we&#8217;ve discussed here, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/chattr\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-chattr-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-chattr-command%2F&amp;text=Linux+chattr+Command+Tutorial+for+Beginners+%285+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-7.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-chattr-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-chattr-command-tutorial-for-beginners-5-examples-9.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Many a time, a Linux machine is used by different users. So there are chances that these users access a common set of files. This opens up door to problems like accidental deletion or editing of important files, something which you &#8211; as an administrator &#8211; would definitely not want to happen. Thankfully, there exists &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-5566","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=5566"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5566\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=5566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=5566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=5566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}