Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm not entirely sure where to ask this question, so if this is not the right place just let me know and I'll move it.

Today I was trying to using recursive scp to copy a directory between two Linux servers and made a typo when referring to the destination server. Instead of transferring the directory to the other machine, it began to transfer the file back into itself. It took a while for me to notice what it was doing since the directory was so large, but then I started seeing the same files popping up again and again on the transfer status until I killed it. It confused me that this was allowed and I have a few questions.

I guess my main question I would like to ask you all, though, is if there is ever an instance when a user would need to make a secure copy to himself on the same machine or is it an unnecessary side effect of scp that was just shrugged off?

Edit - Sorry guys, here's the actual command

scp -r /path/to/dir SameUser@sameserver:/path/to/dir

share|improve this question
1  
If you supply local source and destinations, scp just calls cp. –  jsbillings Aug 8 '12 at 17:38
1  
I carried out a simple test and the behaviour with cp is different, as it doesn't allow to copy a directory over itself: $ cp -r /tmp/test /tmp/test (line break) cp: cannot copy a directory, ‘/tmp/test’, into itself, /tmp/test/test’. With the scp line you supplied an infinite recursion takes place. –  Claudio Aug 8 '12 at 18:12
1  
scp -r /tmp/test /tmp/test, without any hostname references does seem to call cp indeed, as the error message is exactly the same as cp -r /tmp/test /tmp/test. –  Claudio Aug 8 '12 at 18:20
1  
If your goal is just making the copy, I can see no reason to go over the network for it :) –  Claudio Aug 8 '12 at 18:27
1  
@degausser: no, if both the source and destination are local, it uses cp, but as far as scp knows, hostname:path is a remote path, which is why you saw what you did. –  jsbillings Aug 8 '12 at 19:49
show 3 more comments

2 Answers 2

up vote 1 down vote accepted

For copying files on the same machine you wouldn't need scp at all. Anyway, if you specify a directory or file as destination instead of a hostname and a path it will copy it for you locally, which seems to be what happened. If you supply the command line you used we can point you what happened exactly.

EDIT: With the supplied command line, what it does is to go over the network interface, connect to the SSHD server on your local machine and then make the copy. There is no good reason for that since you can copy it locally with cp.

share|improve this answer
    
edited to show command –  degausser Aug 8 '12 at 17:49
    
Thank you sir. I need to go learn a little more about ssh protocols I think :) –  degausser Aug 8 '12 at 18:33
1  
You're welcome! Think of scp as a secure ftp (there's an exact equivalent in the form of sftp, which is also part of openssh). What it does is to copy a file to or from a remote server through an encrypted channel. You wouldn't ever need the extra overhead of encryption and network connection if you are doing things locally, where you have complete control over the system. –  Claudio Aug 8 '12 at 18:36
add comment

It's a special case that's rarely useful. But to detect it would be difficult, harmful and useless.

Difficult, because how do you know that it's the same user on the same machine? There are plenty of rare edge cases where even ssh $USER@localhost does not contact the same account, and the scp program isn't always in a position to tell. For example:

  • localhost does not point to the local machine, due to an unconventional but legal DNS or ssh_config alias.
  • ssh is invoked from inside a chroot, but the ssh server is running outside the chroot, or in a different chroot.
  • The ssh port on the local machine bounces connections to another machine.
  • scp is invoked from a session with reduced privileges; on the remote site of scp $USER@localhost, the user's default privileges hold.

Harmful because it would complicate the behavior of scp a lot. Instead of “copy files, using SSH as the transport”, it would become “copy files, using SSH as the transport, except if the source and destination are detected to be the same user on the same machine”. Yuck. Then, if someone writes a script that happens to use scp and takes machine and user names as parameters, the script would have to include a special case: if the source and destination happen to be the same account on the same machine, then call cp instea of scp. The script writer would have to match scp's logic exactly.

Useless because it would only catch one possible typo among many. Why bother? You might have typed the name of a different server, or mistyped the path, or the username, etc.

share|improve this answer
add comment

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.