TransWikia.com

scp using ssh tunneling

Unix & Linux Asked by Mano Mini on December 4, 2021

Usually, I would just do scp file myuser@server1:~/ (and enter my password)
but now I need to first scp file myuser@server2:~/ and then ssh myuser@server2 from me into server2, and then from server2 do the scp file myuser@server1:~/

I tried to do ssh -L 8888:server1:22 myuer@server2 and it asked me the password just as it does when I do ssh myuser@server2, and it just connected to server2 but not to server1.

How can I make it work so that I could scp from me directly to server1?

4 Answers

I find this this very handy:

scp -J myuser@server2 file myuser@server1:~/

Also, if you keep the user names equal between your laptop and each of the servers, you can simply leave out the user name. If you like, you can also put server2 in your hosts file (/etc/hosts) with a nice and short name for faster typing :

# in file /etc/hosts 
74.125.71.113 s2  # this is server2

Then the command for transfering file to server1 is:

scp -J s2 file server1:~/

Answered by wbloos on December 4, 2021

Put the following in ~/.ssh/config of the host server

XAuthLocation /opt/X11/bin/xauth

Host remote
  User foo
  Hostname remote-server
  ProxyCommand ssh foo@gateway-server -W remote-server:22

then scp as

scp remote:~/file_to_be_copied . 

Answered by sridharn on December 4, 2021

You can very simply configure the ProxyCommand/ProxyJump so you will be able to scp directly to the distant sever. Create a ~/.ssh/config with the following:

Host server1
  ProxyCommand ssh -W %h:%p server2

and then you will be able to scp directly:

scp local.file server1:/path/to/remote.file

If you really insist on port forwarding then after creating a "tunnel" using the first command you provided:

ssh -L 8888:server1:22 server2

you will need to trasnfer the data this way (where the -P indicates the port where the forwarded connection is set up):

scp -P 8888 local.file server1:/path/to/remote.file

Answered by Jakuje on December 4, 2021

scp supports a remote source file as well as a remote destination file. It uses ssh for data transfer, so this should be equivalent to your attempts at tunnelling:

scp file myuser@server2:~/
scp myuser@server2:~/file myuser@server1:~/

By the way, if myuser is the same username as your local login, you can omit it:

scp file server2:~/
scp server2:~/file server1:~/

If you really do want a tunnel (so the file never needs to touch the server2 filesystem) then this is answered well here: https://superuser.com/questions/276533/scp-files-via-intermediate-host

So well in fact, that this might be considered already answered...

Answered by Heath Raftery on December 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP