Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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.

share|improve this answer
    
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 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 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 at 10:38
    
Um, the first paragraph? You are answering a question about doing this as root, you know. – alexis Mar 8 at 10:40
    
@alexis Here you excludes root obviously, perhaps that needs to be clarified a bit ..editing.. – heemayl Mar 8 at 10:45

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|improve this answer
    
Is it possible to copy / anywhere without the destination being inside of the source? – Pace Mar 7 at 15:53
    
@Pace It would have to be to another file system. (Network fs, USB stick etc) – Stig Hemmer Mar 8 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 at 9:58

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.