Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Include the output of lsattr /etc/hosts in your question. –  jordanm Oct 8 '13 at 15:44
    
@jordanm -- just did –  amphibient Oct 8 '13 at 15:47

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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|improve this answer
3  
"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. –  SuperMagic Oct 8 '13 at 16:36
    
Thanks a lot, @SuperMagic. If you post all that in a separate answer, I will upvote and accept it. –  amphibient Oct 8 '13 at 16:43
    
As you request, so it is done. Thanks. –  SuperMagic Oct 8 '13 at 17:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.