Tags
copy, nc, network, networking, port, portforwarding, scp, ssh
Let’s say you have physical access to your home computer h.com
and your work computer w.com
and besides that you work on a remote server r.com
. Let’s also assume that the usernames are huser, wuser
and ruser
, respectively.
There may be various accessibility scenarios, but let’s just assume that you can access the remote servers from both home and work computers, but you cannot access home computer from work directly and vice versa. A possible solution to have all the computers accessible is forward a port on the remote machine to the local port 22 (we are interested in ssh connection only, at least for the time being!).
Forwarding remote port:
So at home before you leave for work, forward port 2222 of r.com
to port 22 of local machine h.com
$ ssh ruser@r.com -R 22:localhost:2222
Type in localhost
as it is! It creates a secure socket from remote machine’s port 2222 to local machine’s port 22, meaning the ssh traffic coming to port 2222 of r.com
will be forwarded to port 22 of h.com
.
When you get to work, first ssh to the remote machine from your work computer:
$ ssh ruser@r.com
.
You can then access the files at your home while you are still on h.com
by simply doing
$ ssh huser@r.com -p 2222
(you’ll be asked for the password for your home computer). The flag -p
stands for port.
Now, before you leave your work, forward port 22 of w.com
to another port (say, 2223) of r.com
using the above method. At home, you first ssh to r.com; and the using
$ ssh wuser@r.com -p 2223
(you’ll be asked for the password for the work computer),
you may access files on your work computer.
Forwarding local port:
I don’t know how I can best use it yet, but anyway, here is how to do it along with one “possible” use of it. Let’s say from your home computer you cannot ssh to r2.com
, another remote server, but you can from r.com
. In order to connect to r2.com
form home, the most obvious way is to connect to r.com
from h.com
first, and then to r2.com
. You have to do this for every new ssh connection r2.com
from h.com
. But if you forward an available local port (say, 2224) to port 22 of r2.com
via r.com, it'll be a lot easier. Issue the following command while you are on
h.com
:
$ ssh ruser@r.com -L 2224:r2.com:22
.
This will enable you to connect to r2.com
from h.com
by using
$ ssh r2user@localhost -p 2224
in another terminal on the home computer (r2user
is the username for r2.com
).
File copying between those computers also becomes a breeze:
$ scp -P 2224 file_to_be_copied r2user@localhost:/desired_dir/copied_file
(notice the capital P
).
N.B. (1) In order to check if a port (say, 2225) is available on the locahost, try
$ nc localhost 2225
,
(2) Needless to say, you must have sshd
running on the machines that you want to connect to via ssh.