Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

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.

share|improve this answer
    
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 yesterday

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.

share|improve this answer
1  
tar will preserve ownership and permissions unless its o option is given. Yes, that's completely logical. –  Michael Kjörling yesterday
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 yesterday
1  
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 yesterday

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
share|improve this answer

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.