'cat': can't open file: Permission denied

10,936

Solution 1

In a regular filesystem e.g. ext4, if you only have w permission in a file, you won't be able to read (cat) it, you need read (r) bit for that. Note that, root can read any file regardless of the permission bits.

Now, /sys is a mount point of special sysfs provided by Linux kernel which is actually a virtual filesystem and works differently than regular filesystems. /sys contains device related info of the system.

Modifying something in /sys would directly alter kernel's internal data structure so it depends on kernel what it would permit or deny.

For /sys/module/nf_conntrack_ipv4/uevent, you have only w bit set for owner (root), even if you add r bit for root (which is a very bad idea), while reading the file you would get I/O error because by design the kernel would not allow anyone to read the uevents for the nf_conntrack_ipv4 module.

Solution 2

To avoid special file systems like /sys and /proc, you should give the following option to cp:

-x, --one-file-system
    stay on this file system

(quoted from the manual page)

If you have several actual file systems, e.g. / and /home, you will then need to give several cp commands.

Be careful that you don't copy from and to the same place. That can lead to infinite loops where you make copies of copies of copies of ...

Share:
10,936

Related videos on Youtube

Rahul
Author by

Rahul

Updated on September 18, 2022

Comments

  • Rahul
    Rahul over 1 year

    I am trying to copy the whole / directory to some other location. When doing cp recursively, it fails on some files like:-

    / # ls -lrt /sys/module/nf_conntrack_ipv4/uevent
    --w-------    1 root     root          4096 Mar  7 06:29 /sys/module/nf_conntrack_ipv4/uevent
    / #
    / # cat /sys/module/nf_conntrack_ipv4/uevent
    cat: can't open '/sys/module/nf_conntrack_ipv4/uevent': Permission denied
    / # cp /sys/module/nf_conntrack_ipv4/uevent /tmp
    cp: can't open '/sys/module/nf_conntrack_ipv4/uevent': Permission denied
    

    If I create some file with only w permissions and try to copy/cat it, I could see no problems there. However, for some files like the one mentioned above, I am unable to copy it or to cat it even though I am trying as root user. Also, the strange thing is that the size of the above mentioned file is mentioned to be as 4096 which is similar to the one we have for directory. Is this something special file?

    I guess I am missing something here and need some input to know more about such files or such behaviors. Kindly do help me out in understanding why I am not allowed to cat contents of such files.

  • Pace
    Pace about 8 years
    Is it possible to copy / anywhere without the destination being inside of the source?
  • Rahul
    Rahul about 8 years
    I tried it on one of my VM's having ext4 filesystem and I was able to cat contents of file which had only w flag set. Here is output: pastebin.com/m1aKAe0b
  • alexis
    alexis about 8 years
    This is wrong: Root can read regular files regardless of read permissions. As the OP's question already explained.
  • alexis
    alexis about 8 years
    @Pace, with cp -x you are safe as long as the destination is on a different file system (logical drive).
  • heemayl
    heemayl about 8 years
    @alexis Where did you find in my answer that i said root can not read a file having only write bit set?
  • alexis
    alexis about 8 years
    Um, the first paragraph? You are answering a question about doing this as root, you know.
  • heemayl
    heemayl about 8 years
    @alexis Here you excludes root obviously, perhaps that needs to be clarified a bit ..editing..