cancel
Showing results for 
Search instead for 
Did you mean: 

scp and preserving uid

scp and preserving uid

I am trying to copy a directory from one server to another using scp but I can't get the file ownership to transfer w/ the files. The -p only preserves the timestamp. Does anyone know of a way to accomplish this?
11 REPLIES
Court Campbell
Honored Contributor

Re: scp and preserving uid

tar the directory, transfer it, then extract.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
A. Clay Stephenson
Acclaimed Contributor

Re: scp and preserving uid

-p preserves the mode and timestamps but the UID and the GID are determined by the effective UID and GID of the process. You could follow up each scp operation with an ssh'ed chown command but it would be easier to first tar or cpio the files into an archive; scp the archive; and finally ssh a tar or cpio command on the remote host to unarchive the files and remove the archive.
If it ain't broke, I can fix that.

Re: scp and preserving uid

Thanks, but I should have been more explicit with my question. Does anyone know of way to copy files preserving the ownership without making an archive of the files and/or directory, i.e. like the cp command with -pr option. And this has to be done across servers enabled w/ ssh. Is there any gnu s/w out there?
A. Clay Stephenson
Acclaimed Contributor

Re: scp and preserving uid

First, you would have to make sure that the UID's and GID's are unified across servers or even the archive methods will fail. If you must work in an SSH environent then the most robust/flexible approach that could handle name/group lookups would be a Perl script using Net::SFTP. Rdist or rsync could do what you want but they are not SSH-based.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: scp and preserving uid

>copy a directory from one server... file ownership to transfer

Of course one way to do this is with NFS if you don't want to use tar/cpio/pax.
Armin Kunaschik
Esteemed Contributor

Re: scp and preserving uid

tar on the fly over the network:
# cd source_dir; tar -cf - .|ssh user@targethost "cd target_dir; tar -xvf -)

If done as root, all permissions are preserved... even if the users don't exist on
the target host.

My 2 cents,
Armin
And now for something completely different...
Armin Kunaschik
Esteemed Contributor

Re: scp and preserving uid

have to correct myself:
# cd source_dir; tar -cf - .|ssh user@targethost "cd target_dir; tar -xvf -"

My 2 cents,
Armin
And now for something completely different...
Mohamad Ridha
Occasional Visitor

Re: scp and preserving uid

have to correct myself:
# cd source_dir; tar -cf - .|ssh user@targethost "cd target_dir; tar -xvf -"

Its such a wonderful single line command. I didnt quite understand on whats the "" purpose? Do the double quote function is the same as ` (backtick)?
Dennis Handly
Acclaimed Contributor

Re: scp and preserving uid

>Mohamad: ssh user@targethost "cd target_dir; tar -xvf -"
>I didn't quite understand what's the "" purpose? Do the double quote function as the same as `?

The purpose of the double quote is to pass that whole string to ssh to execute. (Single quotes would work too.) Otherwise the semicolon separates the ssh command from the final tar.

You shouldn't be using the archaic ``, replace them by $().
Mohamad Ridha
Occasional Visitor

Re: scp and preserving uid

The purpose of the double quote is to pass that whole string to ssh to execute. (Single quotes would work too.) Otherwise the semicolon separates the ssh command from the final tar.

You shouldn't be using the archaic ``, replace them by $().

-----------

Thanks for the reply. Ok got it. Just need to test first using the $() thing.

I'm a bit confused on which protocol that the command do. It looks like it doesnt use any ftp, sftp, scp command; just sending a tar archive over the SSH tunnel. Am i wrong?
Dennis Handly
Acclaimed Contributor

Re: scp and preserving uid

>Mohamad: Thanks for the reply.

(If you are going to be asking more questions, you should create your own thread so you can assign points.)

>Just need to test first using the $() thing.

This example doesn't use $().

>just sending a tar archive over the SSH tunnel.

Yes, it sends it to the stdin of ssh, then to tar(1).