I have one big file on server one and I want to copy it to server two using scp. I have the keys setup properly and I can ssh/scp to both servers from my desktop.
The file I need to copy is larger then the free space on my workstation’s hdd, so I wanted to do:
scp one:/opt/bigfile.tar.gz two:/opt/bigfile.tar.gz
but I got:
ssh: Could not resolve hostname one: Name or service not known
We do not have DNS here (don’t ask me why), so I have this in my ~/.ssh/config:
Host one
Hostname <IP address of server one>
User jspurny
Host two
Hostname <IP address of server two>
User jspurny
If I try with a smaller file and transfer it from one to my workstation and then to two, it works fine:
scp one:/opt/smallerfile.tar.gz . scp smallerfile.tar.gz two:/opt/
When using IP addresses directly as suggested in comment, I got:
$ scp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600a131015120e1920">[email protected]</a><one's IP>:bigfile.tar.gz <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="345e474441465a4d74">[email protected]</a><two's ip>:bigfile.tar.gz Host key verification failed. lost connection
Not an issue:
The size is not an issue here – it was only a “trigger” to this problem as there was no way to store bigfile.tar.gz on my workstation.
The problem occurs regardless of the file size.
Question:
Why does the command:
scp oneremote:file secondremote:file
throws an error regardless if using .ssh/config aliases or directly using ip addresses?
Solved – sort of – still looking for explanation – I’ve split the bigfile into smaller files and transfered them one by one through my workstation. I’m still wondering why it didn’t work. So I would still appreciate some explanation of what was wrong ..
Found a reason why it fails: It seems I was being foolish. I thought that the command
scp one:file two:file
was creating two connections to each server and then receive data from one and immediately send them to two and thus acting like a relay.
This is clearly not the case, because a simple -v option revealed that it in fact just connects to one and from one it tries to connect to two. Which obviously isn’t possible because server one is not supposed to connect to two.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Full credit for this answer goes to https://superuser.com/a/602436/142948
You need the -3 option for scp:
scp -3 one:/opt/bigfile.tar.gz two:/opt/bigfile.tar.gz
-3: Copies between two remote hosts are transferred through the
local host. Without this option the data is copied directly between
the two remote hosts.
http://www.openbsd.org/cgi-bin/man.cgi?query=scp&sektion=1
Otherwise the 2nd alias “two” is being resolved on host “one”, which may not exist.
Method 2
Simple pipe
Try this:
ssh one 'cat file' | ssh two 'cat > file'
The first one should send the file content to your machine, while the second should send that on to second machine. I would compute a checksum at both ends after the transfer, to ensure that nothing got lost or garbled along the way.
Elaborate tunnels
For more elaborate applications, you could use ssh tunnels. For example, you could try something like this:
ssh -R 5001:127.0.0.1:5002 one ssh -L 5002:127.0.0.1:22 two
Then you can open a connection on one to machine localhost port 5001 and it will be forwarded twice and end up as a connection on two to localhost port 22. This is the ssh port, so you can use this for yet another scp, or for rsync, or whatever. You could also start an rsync server on two, and forward port 873 instead of 22. Or you could use nc on both sides to transfer raw data, using an arbitrary port number.
The main benefit between the above approach is that you have a two-directional tcp connection between the two machines, instead of a one-directional pipe only. That way, the two parties can exchange information, which is particularly important in the rsync case.
Method 3
Since you have user access to the source server (one) why not login and run your scp command on that server directly… If you worry that it will take too long, then start the command inside a screen then detach from the screen with Ctrl+a d and let it run.
However, if you must do this from your workstation, and SSH keys from source to destination server are working OK, then send the scp command as the parameter of an ssh command, like:
ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a7a1b7a092a1bda7a0b1b7">[email protected]</a> 'scp /path/to/file <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d0d6c0d7e5c1c0d6d1cccbc4d1cccacb">[email protected]</a>:/path/to/file'
Method 4
Searching for the error message: “Host key verification failed.” would seem to be the most straightforward thing to do here. I found this Q&A on askubuntu titled: SSH connection problem with “Host key verification failed…” error.
One of the answers to that Q&A suggested that the issue lied with a conflicting entry in your ~/.ssh/known_hosts file. You can either delete the troublesome entries from that file using any text editor or you can use this command to remove entries:
$ ssh-keygen -R hostname
Where hostname would be either the IP address or the name of the server you’re attempting to connect from. All the above would be on host two by the way.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0