7

I have a text file named foo.txt with root permission in one Linux distribution. I copied it to another Linux distribution on another computer.

Would file permissions be still maintained for foo.txt?

If yes, how does Unix/Linux linux know, and duplicate the permissions of the file?

Does it add extra bytes (which indicates the permissions) to the file?

12

To add Eric's answer (don't have rep to comment), permissions are not stored in file but file's inode (filesystem's pointer to the file's physical location on disk) as metadata along with owner and timestamps. This means that copying file to non-POSIX filesystem like NTFS or FAT will drop the permission and owner data.

File owner and group is just a pair of numbers, user ID (UID) and group ID (GID) respectively. Root UID is 0 as standard so file will show up as owned by root on (almost) every unix-compliant system. On the other hand, non-root owner will not be saved in meaningful way.

So in short, root ownership will be preserved if tarball'd or copied via extX usbstick or the like. Non-root ownership is unreliable.

  • 1
    For the (clearly somewhat novice) OP you might add that you can think of the file's inode as being maintained inside the directory that contains the file rather than in the file itself. Even if technically this might be a bit less accurate than what you say. In any case the essential point is that a file being copied identically (either within its file system or to a different one) does not rule out that its ownership/permissions might change. – Marc van Leeuwen May 12 '15 at 8:01
4

That would depend on how you copy it. If you put it in a tar ball and copied that, then untarred it, tar will perserve permissions. If you use rsync it will also, depending on flags, perserve permissions. Those applications are responsible for the permissions. If you were to scp it permissions would not be preserved.

The command doing the copying is responsible for managing the permissions on the newly created file.

  • 1
    tar will preserve ownership and permissions unless its o option is given. Yes, that's completely logical. – a CVn May 12 '15 at 9:45
  • 1
    By default tar will store information about both the name and id of the owner. When extracting tar will by default use the name which was stored, but can be instructed to use only the id. – kasperd May 12 '15 at 10:50
  • 2
    It might be worth stressing that tar can only preserve ownership when run as root. There is no way for an ordinary user to create files owned by someone else. – Emil Jeřábek May 12 '15 at 13:17
0

For owner/group, it depends on who does the copy, and how.

  • a regular user: will alway be the owner of the copies by all commands
  • root user too, with cp (except with the --preserve option)
  • "preserve" will be the default for root with tar

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.