Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Copying file from other user's directory with '-p' switch should reserve the permission as per the man page.

I am copying /etc/passwd to my home directory with -p switch but the permissions aren't preserved as follows:-

ijoin@stream:~$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1813 Sep 25 08:58 /etc/passwd
ijoin@stream:~$ cp --preserve /etc/passwd .
ijoin@stream:~$ ls -l passwd 
-rw-r--r-- 1 ijoin ijoin 1813 Sep 25 08:58 passwd

I am doing something wrong?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You're logged in as ijohn and copying a file owned by root. cp command can not work around Linux's security machinery - your user is not allowed to create files owned by root or change ownersip of files to root. Doing so would be a security problem.

If you need to copy files owned by other users, do so using sudo:

sudo cp --preserve /etc/passwd

Here's a good explanation I found on linuxquestions.org:

the -p argument works for all users in the constraint of that users abilities on the system. generally non-root users will be able to preserve ownership of their files that they own and are owned by a group that the specific non-root user belongs to. they cannot preserve ownership of files owned by other users (albeit, there may be ways around this depending on the suid, guid, and sticky bits). root can preserve the ownership of files owned by different users, because root has full system priveledges. but joe_schmoe can not preserve the ownership of files owned by root or files owned by billy, etc. depending on where the file will be copied to, joe_schmoe can only preserve file ownership of files owned by joe_schmoe and owned by a group joe_schmoe belongs to.

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.