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.

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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:
share|improve this answer

Your Answer


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.