2

Suppose I have host A from which I ssh to host B, where I sudo -U some_role and from under it ssh to host C. My goal is an interactive shell on C.

Assume that from C I cannot ssh back to A.

What is the best way to copy a file from A to C using the connection built above? What preparations / changes should I introduce into the chain to make infrequent, simple file copying possible?

Of course I can run cat > target_file inside the interactive shell and copy-paste via the terminal, but for large binary files this is not exactly convenient.

7

I take it host B is e.g. a gateway in an intranet and can connect to host A and C, e.g. like this:

                                                                       -----------------------------------------------------------
                                                                      |                                                           |
                                                                      |                                                           |
                       ---------                                -----------                    --------------                     |
                      |A        |------------------------------|   B       |------------------|              |                    |
                       ---------                               |           |                  | C            |                    |
                                                                -----------                    --------------                     |
                                                                      |                                                           |
                                                                       -----------------------------------------------------------

In this case the best solution is ssh port forwarding. I describe it at http://www.linuxintro.org/wiki/Tunneling_with_OpenSSH

First, on A, you tunnel B's port 2222 to C's port 22 with the command:

ssh -L 2222:C:22 B

then you copy to C's port 22, by connecting to port 2222 on A (localhost) with the command:

scp -P 2222 file root@localhost:
0

The quickest way to transfer files is to launch a temporary web server in the directory containing the files you want to copy. So run the following command on host A:

python -m SimpleHTTPServer 4000

Then on host C:

wget http://hostA.local:4000/myfile.txt

or with IP address:

wget http://192.168.1.135:4000/myfile.txt

You can also see the files in a browser if the target host has a GUI.

To stop the web server on host A, use CTRL-C.

If you use this solution often, you may want to use gnu screen (https://ss64.com/bash/screen.html) so that you can continue to work while the web server is running.

  • This is nice exactly in cases when you do not have the complex SSH tunneling setup, but can directly contact one machine from another. Keeping a web server accessible via the ssh tunnel using port forwarding could be convenient, though, if pulling (not pushing) files is the goal. – 9000 Jun 2 '17 at 14:26
  • The idea is to access the web server directly via HTTP but not via SSH tunneling. It is true that this solution is convenient only for pulling files though it is possible to write a simple web page that will also allow pushing. – nico Jun 2 '17 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.