Ubuntu Linux rsync and cp command not keeping ownership

I am not a Linux expert and mostly a developer but something that should work is not.

I have created a script that after a general install of Ubuntu 20.04 Linux configures my .NET Core website using NGINX as a proxy.

I download my setup script to the Ubuntu Server into a directory called /setup.

When I run this script it all works except for one small issue.

I have created a new nginx.conf file that I just want to copy over the original /etc/nginx/nginx.conf file. When I do this, I want the copied over file to keep the same rights and permissions as the original.

In my script I am using (I Googled):

sudo rsync -HDgop /setup/nginx/nginx.conf /etc/nginx/nginx.conf

When I do this (the script I run) I am logged in as a ‘sudo’ user let’s say is called ‘scarlett’.

If I change directory to /etc/nginx/nginx.conf and do a ‘ls -la’ the nginx.conf is now owned by scarlett:scarlett rather than the original root:root.

What am I doing wrong here?

Note: the script is written in PowerShell not Bash for reasons out of my control. Though it does work well bar this.

UPDATE

I have now tried this only at the command line and no matter what I do it simply doesn't work.

/etc/nginx/nginx.conf Initially has root:root ownership.

After:

sudo rsync -HDgop /setup/nginx/nginx.conf /etc/nginx/nginx.conf

sudo rsync -avz /setup/nginx/nginx.conf /etc/nginx/nginx.conf

sudo cp -p /setup/nginx/nginx.conf /etc/nginx/nginx.conf

/etc/nginx/nginx.conf always ends up with MySudoGroupUser:MySudoGroupUser ownership instead of root:root.

This is also same file system same machine.

I could manually re-own this file but doesn't that sort of defeat the point of the switches.



Read more here: https://stackoverflow.com/questions/68478058/ubuntu-linux-rsync-and-cp-command-not-keeping-ownership

Content Attribution

This content was originally published by Scarlett Hanson at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: