Use Local SSH Forwarding to Reduce the Number of Manual Hops

(This is part three in a series of posts on ssh.)

Local port forwarding is the same as remote port forwarding but works in the opposite direction. An example is the clearest way to explain.

Assuming you've done the steps in the previous posts, then at home you can run ssh -L 4022:localhost:4022 This listens on TCP port 4022 on your home machine. Any connections there will be forwarded through the ssh connection to port 4022 on cloud... which, as we recall, gets forwarded to port 22 (ssh) at work. If you leave this connection open, you can run ssh -p 4022 localhost on your home machine and it will connect to work in just one hop. This means that you can use scp to copy files from home to work or vice versa. For example, scp -P 4022 localhost:/tmp/foo.txt ~/foo.txt will copy a file from work to home. (Note: scp needs capital "-P" to give the port. I got it wrong the first time.)

Posted on 2009-12-02 by brian in ssh .


Suggestion: for scp, you want to use a capital 'P', as in "-P".

kevin clark
2009-12-21 22:54:50

Thanks Kevin. I fixed it and added a note.

Brian St. Pierre
2009-12-22 13:21:14
Comments on this post are closed. If you have something to share, please send me email.