{"id":2219,"date":"2015-09-09T16:11:41","date_gmt":"2015-09-09T15:11:41","guid":{"rendered":"https:\/\/stevepedwards.today\/DebianAdmin\/?p=2219"},"modified":"2015-09-09T16:11:41","modified_gmt":"2015-09-09T15:11:41","slug":"using-and-finding-the-attribute-of-i-immutable-assigned-to-a-file","status":"publish","type":"post","link":"https:\/\/stevepedwards.today\/DebianAdmin\/using-and-finding-the-attribute-of-i-immutable-assigned-to-a-file\/","title":{"rendered":"Using and finding the attribute of i assigned to a file with chattr and lsattr"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2219\" class=\"pvc_stats all  \" data-element-id=\"2219\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p><span style=\"color: #ffffff;\">Ever had a situation where you could not recursively remove some directories due to one or more files having a permission that even root could not take control of? Did you ever find out why?<\/span><\/p>\n<p><span style=\"color: #ffffff;\">I have had this scenario on a backup drive, with Windows files having an attribute set from being copied from a compressed Win file system, which would not then allow root to remove it when the external drive was attached to a linux box. <\/span><\/p>\n<p><span style=\"color: #ffffff;\">Infuriating - as it meant I could not delete the whole directory with this file under it. I ended up re-attaching the drive to a Win PC, then taking ownership that way, before I could delete the file with the special attribute, and then returning the drive to a linux system for full control short of just formatting the drive completely in linux and losing everything of course.<\/span><\/p>\n<p><span style=\"color: #ffffff;\">In a similar vein for special file attributes, linux can prevent file deletion to all users, including root, using the chattr command.<\/span><\/p>\n<p><span style=\"color: #0000ff;\">man chattr<\/span><\/p>\n<p><span style=\"color: #cc0000;\">A file with the `i' attribute cannot be modified: it cannot be deleted<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> or renamed, no link can be created to this file and no data can be<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> written to the file. Only the superuser or a process possessing the<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> CAP_LINUX_IMMUTABLE capability can set or clear this attribute.<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> The letters `acdeijstuACDST' select the new attributes for the files:<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> append only (a), compressed (c), no dump (d), extent format (e),<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> immutable (i), data journalling (j), secure deletion (s), no tail-merg<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> ing (t), undeletable (u), no atime updates (A), no copy on write (C),<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> synchronous directory updates (D), synchronous updates (S), and top of<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> directory hierarchy (T).<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> The following attributes are read-only, and may be listed by lsattr(1)<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> but not modified by chattr: huge file (h), compression error (E),<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> <b>indexed directory (I),<\/b> compression raw access (X), and compressed dirty<\/span><\/p>\n<p><span style=\"color: #cc0000;\"> file (Z).<\/span><\/p>\n<p><span style=\"color: #ffffff;\">This is an interesting way to prevent any changes to a file being made except by root.<\/span><\/p>\n<p><span style=\"color: #ffffff;\">Create a testfile:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">touch testfile.txt<\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">ls -als testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">0 -rw-r--r-- 1 root root 0 Sep 9 14:31 testfile.txt<\/span><\/p>\n<p><span style=\"color: #ffffff;\">Now add the i attribute:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">chattr +i testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">ls -als testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">0 -rw-r--r-- 1 root root 0 Sep 9 14:31 testfile.txt<\/span><\/p>\n<p><span style=\"color: #ffffff;\">You can view this attribute with:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee #<span style=\"color: #0000ff;\"> lsattr testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">----i--------e-- testfile.txt<\/span><\/p>\n<p><span style=\"color: #ffffff;\">Now, as root the owner - try to delete the file:<\/span><\/p>\n<p><span style=\"color: #cc0000;\"><span style=\"color: #ffffff;\">MintServer stevee # <\/span><span style=\"color: #0000ff;\">rm -v testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">rm: cannot remove \u02dctestfile.txt,: Operation not permitted<\/span><\/p>\n<p><span style=\"color: #ffffff;\">Only root can remove the attribute before being able to delete the file.<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">chattr -i testfile.txt<\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # l<span style=\"color: #0000ff;\">s -als testfile.txt<\/span> <\/span><\/p>\n<p><span style=\"color: #cc0000;\">0 -rw-r--r-- 1 root root 0 Sep 9 14:31 testfile.txt<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">lsattr testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">-------------e-- testfile.txt<\/span><\/p>\n<p><span style=\"color: #ffffff;\">How do you search for other such files in the system?<\/span><\/p>\n<p><span style=\"color: #ffffff;\">As ls -al does not show this attribute, it can't be used to search for it simply, say in conjunction with grep, as only lsattr shows this character in the 5<sup>th<\/sup> field of the files attributes.<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee #<span style=\"color: #0000ff;\"> lsattr testfile.txt <\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\"><span style=\"color: #cc0000;\"><b>----i<\/b><\/span><span style=\"color: #cc0000;\">--------e-- testfile.txt<\/span> <\/span><\/p>\n<p><span style=\"color: #ffffff;\">Can that be used recursively with grep? Yes, as it has a -R switch, though many directories will not be searchable it seems, due to the way the ioctrl reads files flags, depending on what the file does or is doing within the OS:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">lsattr -R \/ | grep ^----i <\/span><\/span><\/p>\n<p><span style=\"color: #ff0000;\">lsattr: Operation not supported While reading flags on \/dev\/vga_arbiter<\/span><\/p>\n<p><span style=\"color: #ffffff;\"><span style=\"color: #cc0000;\">----i<\/span>--------e-- <span style=\"color: #cc0000;\">\/home\/stevee\/testfile.txt<\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\">It did still find my test file after searching under the root dir. <\/span><\/p>\n<p><span style=\"color: #ffffff;\">If you experiment with this command on Windows files you may find the I indexed type files, or others, with extent format e mentioned above that chattr cannot change <i><b>and may be listed by lsattr(1) but not modified by chattr:<\/b><\/i><\/span><\/p>\n<p><span style=\"color: #ffffff;\">I found some in the 11<sup>th<\/sup> column in my tftpboot directory for Win installs:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">MintServer stevee # <span style=\"color: #0000ff;\">lsattr -R | grep ^----------I<\/span><\/span><\/p>\n<p><span style=\"color: #cc0000;\">----------I--e-- .\/tftpboot\/XP\/I386<\/span><\/p>\n<p><span style=\"color: #cc0000;\">----------I--e-- .\/tftpboot\/XP\/I386\/COMPDATA<\/span><\/p>\n<p><span style=\"color: #cc0000;\">----------I--e-- .\/tftpboot\/Win7Home\/sources\/dlmanifests<\/span><\/p>\n<p><span style=\"color: #cc0000;\">----------I--e-- .\/tftpboot\/WinPE_amd64\/setup\/sources\/dlmanifests<\/span><\/p>\n<p><span style=\"color: #ffffff;\"><span style=\"color: #cc0000;\">----------I--e-- .\/tftpboot\/WinPE_amd64\/setup\/sources\/replacementmanifests<\/span><\/span><\/p>\n<p><span style=\"color: #ffffff;\">This means there are at least 15 further attributes of a file, as well as the listing of ls, of only 10 fields - rwx and file type field - at least for the letters:<\/span><\/p>\n<p><span style=\"color: #ffffff;\">The format of a symbolic mode is +-=<b>[acdeijstuACDST]<\/b><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2219\" class=\"pvc_stats all  \" data-element-id=\"2219\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Ever had a situation where you could not recursively remove some directories due to one or more files having a permission that even root could not take control of? Did you ever find out why? I have had this scenario on a backup drive, with Windows files having an attribute set from being copied from <a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/using-and-finding-the-attribute-of-i-immutable-assigned-to-a-file\/\" class=\"more-link\">...<span class=\"screen-reader-text\">\u00a0 Using and finding the attribute of i assigned to a file with chattr and lsattr<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2219","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"a3_pvc":{"activated":true,"total_views":2,"today_views":0},"_links":{"self":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/2219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/comments?post=2219"}],"version-history":[{"count":0,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/2219\/revisions"}],"wp:attachment":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/media?parent=2219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/categories?post=2219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/tags?post=2219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}