You must run the copy command as root as otherwise the owner will be reset to you, and the group may be reset.
sudo cp -a /mnt/d/SRC /home/dog/data/SRC
The full rules are considered in order:
- If you are root then all owner/group and permissions are kept
- If you are a member of the group then the group name and permissions are kept
- Otherwise owner and group are reset to you and your primary group
These rules are honoured using rsync. However, using (GNU) cp a further restriction is applied in that setuid/setgid bits are removed if the owner or group cannot be kept.
Example using a copy from src to dst, and then sdiff to list differences between the two directories.
Initial state for each attempt:
ls -l src
total 36
drwxr-xr-x 2 chris chris 4096 May 6 15:16 chris-dir
-rwxr-xr-x 1 chris chris 0 May 6 15:16 chris-file
drwxr-sr-x 2 chris chris 4096 May 6 15:16 chris-sgid-dir
-rwxr-sr-x 1 chris chris 0 May 6 15:16 chris-sgid-file
drwsr-xr-x 2 chris chris 4096 May 6 15:16 chris-suid-dir
-rwsr-xr-x 1 chris chris 0 May 6 15:16 chris-suid-file
drwxr-xr-x 2 root root 4096 May 6 15:16 root-dir
-rwxr-xr-x 1 root root 0 May 6 15:16 root-file
drwxr-sr-x 2 root root 4096 May 6 15:16 root-sgid-dir
-rwxr-sr-x 1 root root 0 May 6 15:16 root-sgid-file
drwsr-xr-x 2 root root 4096 May 6 15:16 root-suid-dir
-rwsr-xr-x 1 root root 0 May 6 15:16 root-suid-file
drwxr-xr-x 2 test1 test1 4096 May 6 15:16 test1-dir
-rwxr-xr-x 1 test1 test1 0 May 6 15:16 test1-file
drwxr-sr-x 2 test1 test1 4096 May 6 15:16 test1-sgid-dir
-rwxr-sr-x 1 test1 test1 0 May 6 15:16 test1-sgid-file
drwsr-xr-x 2 test1 test1 4096 May 6 15:16 test1-suid-dir
-rwsr-xr-x 1 test1 test1 0 May 6 15:16 test1-suid-file
Using (GNU) cp
cp -a src/. dst/ && sdiff -lw132 <(ls -l src | sort -k9) <(ls -l dst | sort -k9)
total 36 (
drwxr-xr-x 2 chris chris 4096 May 6 15:16 chris-dir (
-rwxr-xr-x 1 chris chris 0 May 6 15:16 chris-file (
drwxr-sr-x 2 chris chris 4096 May 6 15:16 chris-sgid-dir (
-rwxr-sr-x 1 chris chris 0 May 6 15:16 chris-sgid-file (
drwsr-xr-x 2 chris chris 4096 May 6 15:16 chris-suid-dir (
-rwsr-xr-x 1 chris chris 0 May 6 15:16 chris-suid-file (
drwxr-xr-x 2 root root 4096 May 6 15:16 root-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 root-dir
-rwxr-xr-x 1 root root 0 May 6 15:16 root-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 root-file
drwxr-sr-x 2 root root 4096 May 6 15:16 root-sgid-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 root-sgid-dir
-rwxr-sr-x 1 root root 0 May 6 15:16 root-sgid-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 root-sgid-file
drwsr-xr-x 2 root root 4096 May 6 15:16 root-suid-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 root-suid-dir
-rwsr-xr-x 1 root root 0 May 6 15:16 root-suid-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 root-suid-file
drwxr-xr-x 2 test1 test1 4096 May 6 15:16 test1-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 test1-dir
-rwxr-xr-x 1 test1 test1 0 May 6 15:16 test1-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 test1-file
drwxr-sr-x 2 test1 test1 4096 May 6 15:16 test1-sgid-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 test1-sgid-dir
-rwxr-sr-x 1 test1 test1 0 May 6 15:16 test1-sgid-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 test1-sgid-file
drwsr-xr-x 2 test1 test1 4096 May 6 15:16 test1-suid-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:16 test1-suid-dir
-rwsr-xr-x 1 test1 test1 0 May 6 15:16 test1-suid-file | -rwxr-xr-x 1 chris chris 0 May 6 15:16 test1-suid-file
Using rsync:
rsync -a src/ dst && sdiff -lw132 <(ls -l src | sort -k9) <(ls -l dst | sort -k9)
total 36 (
drwxr-xr-x 2 chris chris 4096 May 6 15:17 chris-dir (
-rwxr-xr-x 1 chris chris 0 May 6 15:17 chris-file (
drwxr-sr-x 2 chris chris 4096 May 6 15:17 chris-sgid-dir (
-rwxr-sr-x 1 chris chris 0 May 6 15:17 chris-sgid-file (
drwsr-xr-x 2 chris chris 4096 May 6 15:17 chris-suid-dir (
-rwsr-xr-x 1 chris chris 0 May 6 15:17 chris-suid-file (
drwxr-xr-x 2 root root 4096 May 6 15:17 root-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:17 root-dir
-rwxr-xr-x 1 root root 0 May 6 15:17 root-file | -rwxr-xr-x 1 chris chris 0 May 6 15:17 root-file
drwxr-sr-x 2 root root 4096 May 6 15:17 root-sgid-dir | drwxr-sr-x 2 chris chris 4096 May 6 15:17 root-sgid-dir
-rwxr-sr-x 1 root root 0 May 6 15:17 root-sgid-file | -rwxr-sr-x 1 chris chris 0 May 6 15:17 root-sgid-file
drwsr-xr-x 2 root root 4096 May 6 15:17 root-suid-dir | drwsr-xr-x 2 chris chris 4096 May 6 15:17 root-suid-dir
-rwsr-xr-x 1 root root 0 May 6 15:17 root-suid-file | -rwsr-xr-x 1 chris chris 0 May 6 15:17 root-suid-file
drwxr-xr-x 2 test1 test1 4096 May 6 15:17 test1-dir | drwxr-xr-x 2 chris chris 4096 May 6 15:17 test1-dir
-rwxr-xr-x 1 test1 test1 0 May 6 15:17 test1-file | -rwxr-xr-x 1 chris chris 0 May 6 15:17 test1-file
drwxr-sr-x 2 test1 test1 4096 May 6 15:17 test1-sgid-dir | drwxr-sr-x 2 chris chris 4096 May 6 15:17 test1-sgid-dir
-rwxr-sr-x 1 test1 test1 0 May 6 15:17 test1-sgid-file | -rwxr-sr-x 1 chris chris 0 May 6 15:17 test1-sgid-file
drwsr-xr-x 2 test1 test1 4096 May 6 15:17 test1-suid-dir | drwsr-xr-x 2 chris chris 4096 May 6 15:17 test1-suid-dir
-rwsr-xr-x 1 test1 test1 0 May 6 15:17 test1-suid-file | -rwsr-xr-x 1 chris chris 0 May 6 15:17 test1-suid-file
Note that where rsync has maintained the group it is because the non-root user running the command is a member of that group. Otherwise the group will be reset to the user's primary group.