Unable to run 'sudo chmod +w /etc/hosts'

10,933

Solution 1

The specific attribute in this issue is i, the immutable attribute.

The file was marked immutable.

This means it is unchangeable at all by any user including root. Root can still change the attributes and remove the immutable attribute, but must to so first before making changes to the file, unlike standard no-write permissions to a file which root can simply ignore.

These attributes are only applicable to ext[234] file systems so far as I know.

You can see the man page for chattr,

$man chattr

to see a full list and description of the available attributes.

The only one I've ever actually used is i. But some of the others include:

A: atime remains unmodified when accessed
a: can only be opened for writing in append-only mode
c: compressed automatically
j: all data is written to the journal before being written to the file
s: blocks are zeros when file is deleted
u: contents of file are saved when file is deleted for later undelete

There are other attributes but they are somewhat esoteric and much more info can be found on them in the chattr man page.

Solution 2

I changed the extended attributes to get rid of the i and then I was alright:

>sudo chattr -i /etc/hosts

But would still like an explanation how to read lsattrs output, including the attribute that I changed.

Share:
10,933

Related videos on Youtube

amphibient
Author by

amphibient

Software Engineer with table manners

Updated on September 18, 2022

Comments

  • amphibient
    amphibient almost 2 years

    I am logged into my remote VM (running out of ESXi) as user xyz. I wanted to change my /etc/hosts to add some network names that were not visible by default.

    I first tried to run

    sudo vi /etc/hosts
    

    but when I got into vi, it was still telling me the file was read-only. Here are the privileges:

    >ls -l /etc/hosts
    -rw-r--r-- 1 root root 416 2013-06-19 08:08 /etc/hosts
    

    I also noticed that almost every other file in /etc has a lsattr of -----------------e-, only hosts has ----i------------e-. E.g.:

    >lsattr /etc
    ...
    -----------------e- ./python
    ----i------------e- ./hosts
    ...
    

    Then I tried to chmod and here is what I got:

    >sudo chmod +w /etc/hosts
    chmod: changing permissions of `/etc/hosts': Operation not permitted
    

    I thought that was weird because root (to which I am switched when I sudo) should be able to do anything. My sudoers file looks quite ordinary:

      1 # /etc/sudoers
      2 #
      3 # This file MUST be edited with the 'visudo' command as root.
      4 #
      5 # See the man page for details on how to write a sudoers file.
      6 #
      7 
      8 Defaults        env_reset
      9 
     10 # Host alias specification
     11 
     12 # User alias specification
     13 
     14 # Cmnd alias specification
     15 
     16 # User privilege specification
     17 root    ALL=(ALL) ALL
     18 
     19 # Allow members of group sudo to execute any command after they have
     20 # provided their password
     21 # (Note that later entries override this, so you might need to move
     22 # it further down)
     23 %sudo ALL=(ALL) ALL
     24 #
     25 #includedir /etc/sudoers.d
     26 
     27 # Members of the admin group may gain root privileges
     28 %admin ALL=(ALL) ALL
    

    I am looking for an explanation why this is happening and how to work around it.

    • jordanm
      jordanm over 10 years
      Include the output of lsattr /etc/hosts in your question.
    • amphibient
      amphibient over 10 years
      @jordanm -- just did
  • Sleeping_Giant
    Sleeping_Giant over 10 years
    "man chattr" to see a list of all the attributes. The specific attribute in this issues is the immutable attribute. The file was marked immutable. This means it is unchangeable at all by any user including root. Root can still change the attributes and remove the immutable attribute, but must to so first before making changes to the file, unlike standard no-write permissions to a file which root can simply ignore. These attributes are only applicable to ext[234] file systems so far as I know.