46

SCP does not seem to preserve ownership stamps even if used with -p option.

scp -p /mysql/serv/data_summary.* some_server:/mysql/test/

The files are owned by mysql and I want the same ownership to be assigned on the destination server. I need to copy files as root on both servers due to some admin issues. I can not change to mysql@

shantanuo
  • 3,669

3 Answers3

64

Try to use rsync, it has a lot more benefits besides keeping ownership, permissions and incremental copies:

rsync -av source 192.0.2.1:/dest/ination

Besides that, since rsync uses ssh, it should work where scp works.

aseq
  • 4,740
29

That is correct. "-p" does not do that. See the man page:

     -p      Preserves modification times, access times, and modes from the
             original file.

Notice it says times and modes, NOT user/group ownership. You will have better luck with "rsync", as it has various capabilities around preserving permissions when copying between disparate systems. "-p" in rsync, for example.

Nex7
  • 2,055
0

scp doesn't allow that. rsync was suggested earlier, but if it is not available (e.g., not installed by default on a Windows GitHub runner), you may use the chown command to achieve your goal. This is how I do it in my CI/CD:

- name: Change ownership of the folder
  run: |
    ssh ${{ vars.REMOTE_USER }}@${{ vars.REMOTE_HOST }} "chown -R ${{ vars.REMOTE_USER }}:www-data ${{ vars.PUBLISH_TARGET }}"
  continue-on-error: true

Make sure that the user belongs to the desired group; otherwise, you will need to add sudo permissions here.