I have these files:-rw-rw-r-- 1 root   adm    0 Jun 22 11:25 a-rw-rw-r-- 1 wilmes wilmes 0 Jun 22 11:23 bWhen I cp b over to a as user wilmes why does it keep the original owner of a (root)?I use this command (no aliases): cp b aWhy does it fail when I use -p in command cp -p b a ?cp: preserving times for 'a': Operation not permittedI noticed this on Ubuntu 17.04 with ext4. User wilmes is member of group adm and the containing directory looks like this:drwxrwxr-x 2 wilmes wilmes 4,0K Jun 22 12:09 ../user/And most important: Where is this documented?
Why does cp not set source attributes and fail with -p?
cp
It fails because you need to be the target (a) file owner, cp will not override basic unix security. A user can preserve their own files, wilmes -> wilmes, but cannot do so for other users, and certainly not for root. I am assuming that you are not logged in as root, nor as wilmes.In this case, one file is owned by root, the other owned by wilmes. To preserve, you should try running:sudo cp -p b aRelevant links:Why doesn't cp -p  copy  and preserve ownership?cp -p will preserve times for some files but not all?Preserve permission switch not working with cp?Example:Create a file for root, a, and a file for a standard user (joe), b.joe@testbed:~/tmp2$ sudo touch ajoe@testbed:~/tmp2$ touch bVerify that the permissions are correct.joe@testbed:~/tmp2$ ls -ltotal 0-rw-r--r-- 1 root    root    0 Jun 22 13:51 a-rw-r--r-- 1 joe     joe     0 Jun 22 13:51 bAttempt to copy user owned file, b, over root owned file (which would violate permissions):joe@testbed:~/tmp2$ cp -p b acp: cannot create regular file 'a': Permission deniedRepeat copy as root, permissions preserved:joe@testbed:~/tmp2$ sudo cp -p b ajoe@testbed:~/tmp2$ ls -ltotal 0-rw-r--r-- 1 joe joe 0 Jun 22 13:51 a-rw-r--r-- 1 joe joe 0 Jun 22 13:51 bNote: Permission denied, not Operation not permitted. I imagine that you see not permitted because the user wilmes DOES have access to the file, by means of a group, however, they are not the owner, so they cannot entirely overwrite it. Logically, you're replacing a file that belongs to another user (root) and asking the system to also make you the owner of that file, that would be a catastrophic security flaw. For example, what if I came a long as a normal user, malicious.username01 and wanted to replace the crontab for something like ntp which belongs to root? If what you are trying to do worked, I could simply write my own malicious version of it and then simply cp -p my-malicious-script /etc/cron.daily/ntp. This would be terrible. Really really terrible.