I need to show somebody a website running on my local machine tomorrow. Normally I'd accomplish this by port forwarding on my local router but thanks to failing hardware and its replacement being awful, my current router doesn't let me do port forwarding.
So stuck with this delay and not wanting to push the whole thing onto a proper server, I had a crazy idea: Can I just forward my port to an external server over SSH?
I've done port tunnelling before but I usually do it the right way around:
- I connect to a remote box and ask that port 12345 shows up on my local machine at port 12345.
- I start something on P12345 on the remote machine
- I can access it via localhost:12345
What I want to do:
- Connect to a remote PC and ask that that its local P12345 fetch things from my local P12345 (over the tunnel)
- I start something on my local computer on P12345
- Other people can access remote:12345 and see my localhost:12345
The command for forwarding port 80 from your local machine (
localhost
) to the remote host on port 8000 is:This requires an additional tweak on the SSH server, add the lines to
/etc/ssh/sshd_config
:Next, reload the configuration by server executing
sudo reload ssh
.The setting
GatewayPorts yes
causes SSH to bind port 8000 on the wildcard address, so it becomes available to the public address ofremote-machine
(remote-machine:8000
).If you need to have the option for not binding everything on the wildcard address, change
GatewayPorts yes
toGatewayPorts clientspecified
. Becausessh
binds to the loopback address by default, you need to specify an emptybind_address
for binding the wildcard address:The
:
before8000
is mandatory ifGatewayPorts
is set toclientspecified
and you want to allow public access toremote-machine:8000
.Relevant manual excerpts:
ssh(1)
sshd_config(5)
See also:
If the server has
GatewayPorts no
, you can achieve the same result by executingssh -g -L 8001:localhost:8000 oli@remote-machine
on the server once you have executedssh -R
command on the client. This will make loopback port 8000 on the server accessible on all interfaces on port 8001.