Linux – Obscure reasons a file is read-only

filesfilesystemslinux

The Linux filesystem seems to have layers and layers of subtlety that I stumble upon case-by-case:

I'm interested in editing this file:

[user@box ~]$ ls -l /a/b/c/foo.bar
-rw-rwxr-x    1 user   user      144529 Jan 26  2018 /a/b/c/foo.bar

It looks like it should be writable by user user – true? But when I try to edit the file in vim, I get a "W10: Warning: Changing a readonly file" warning.

I know that file writability has a dependency on the permissions of its containing folder. I think the containing folder needs to have execute permission – true? I assume the required directory permission also extends all the way up to / – true?

It looks to me like the noted file's containing folder tree has execute permissions:

[user@box ~]$ ls -ld /a/b/c/                                                                                    
drwxrwxrwx    2 user   user       36864 Mar  5 17:50 /a/b/c/
[user@box ~]$ ls -ld /a/b/           
drwxrwxr-x    4 user   user        4096 Sep 22  2017 /a/b/
[user@box ~]$ ls -ld /a/                
drwxrwxr-x    9 user   user        4096 Sep 15  2017 /a/
[user@box ~]$ ls -ld /     
drwxr-xr-x   24 root   root           0 Aug 24 10:48 /
[user@box ~]$ whoami
user

As far as I can tell from the above, every directory in the relevant tree has execute permissions. At first, I was suspicious of whether it had to do with / being owned by root, but it has execute permission for "others". Plus, if there were issues related to / being owned by root, I imagine I'd have trouble writing files anywhere on the filesystem as a user other than root, but that is not the case.

Can anyone think of/identify other reasons why the noted file is considered read-only?

Best Answer

First - you are with immutable flag set

chattr -i yourfilename

If immutable flag is set you cannot change the file. Remember that permissions did not override that behavior!

Second, check if the directory where file is, is mounted in another disk or partition, as read only, for that just type:

mount 

(without arguments)

If you see something like your directory mounted as read-only there is the reason! Remount it as read-write to get lucky!

Third - and most common, you are just running a filesystem with errors, to correct that, you must backup what you can, reboot, then login in single mode and run:

mount #To determine how the partition is mounted
mount -o remount,ro /dev/sd(yourpartition) directory
fsck.ext4 /dev/sd(yourpartition)

(ext4 maybe need to be changed to your partition type)

Good Lucky!