5

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?

  • Is any part of the directory tree /a/b/c/ a separate partition than / ? lsblk would tell you if it is. – Thegs Aug 24 '18 at 18:53
  • Did you start vim with the -R option or do you have readonly/ro set through either your .vimrc file or by a modeline in the file itself? – Kusalananda Aug 24 '18 at 18:54
  • @Thegs - it looks like lsblk is not a supported command on the box on which I'm working - is there any other command that'd give equivalent information (I'm not familiar with partitions, so I've no clue what investigative commands are available) – StoneThrow Aug 24 '18 at 18:55
  • @Kusalananda - I can confirm that vim was not started with -R and I'm pretty sure vim is not sourcing any file that specifies readonly/ro because I am able to edit other, unrelated, files. I'm not sure what you mean by "modeline" but it sounds like a string embedded within the file I want to edit...? If so, a case-insensitive grep on the file for "modeline" came back empty. – StoneThrow Aug 24 '18 at 18:58
  • 1
    @StoneThrow A "modeline" is a line such as # vim: ro in the file. It may be used to set vim options specific to a file (like tab lengths etc., but also the ro option). The line is usually at the start or end of the file. – Kusalananda Aug 24 '18 at 19:01
15

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!

  • I think you identified my problem: chattr -i /a/b/c/foo.bar came back with "chattr: setting flags on /a/b/c/foo.bar: Read-only file system" I then did mount and the output had the line "/dev/sda1 on /a type ext4 (ro,noatime,barrier=1,data=ordered)" -- does this mean that the entire /a/ directory tree is effectively read-only? – StoneThrow Aug 24 '18 at 19:08
  • 2
    Yes it means exactly that. ro "(ro," read-only. The causes may vary, if the partition is explicity mounted read-only by you for example, or falled-back to read-only due to filesystem errors. You can check if the partition has errors with dmesg | grep /dev/sda1 maybe you will see something like Uh! Oh! remounted read-only or something like that. To do the fsck you must use fsck.ext4 /dev/sda1. To just remount read-write if you dont have any errors mount -o remount,rw none /a – Luciano Andress Martini Aug 24 '18 at 19:35
  • 1
    See my bash script github.com/waltinator/pathlld – waltinator Aug 25 '18 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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