3

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.

2 Answers 2

10

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.

5
  • 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
    – Rahul
    Mar 7, 2016 at 17:30
  • This is wrong: Root can read regular files regardless of read permissions. As the OP's question already explained.
    – alexis
    Mar 8, 2016 at 9:53
  • @alexis Where did you find in my answer that i said root can not read a file having only write bit set?
    – heemayl
    Mar 8, 2016 at 10:38
  • Um, the first paragraph? You are answering a question about doing this as root, you know.
    – alexis
    Mar 8, 2016 at 10:40
  • @alexis Here you excludes root obviously, perhaps that needs to be clarified a bit ..editing..
    – heemayl
    Mar 8, 2016 at 10:45
4

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 ...

3
  • Is it possible to copy / anywhere without the destination being inside of the source?
    – Pace
    Mar 7, 2016 at 15:53
  • @Pace It would have to be to another file system. (Network fs, USB stick etc) Mar 8, 2016 at 8:12
  • @Pace, with cp -x you are safe as long as the destination is on a different file system (logical drive).
    – alexis
    Mar 8, 2016 at 9:58

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.