6

I have a domain that i bought from DynDNS. I pointed the domain at my ip adress so i can run servers. The problem I have is that I don't live near the server computer... Can I use an ssh tunnel? As I understand it, this will let me have access to my servers. I want the remote computer to direct traffic from port 8080 over the ssh tunnel to the ssh client, being my laptop's port 80. Is this possible?

3 Answers 3

8

This is actually pretty easy to accomplish, even though it's somewhat buried in the ssh documentation. Assuming OpenSSH, the basic syntax is as follows:

ssh -R 8080:localhost:80 -N [email protected]

This will open a listening socket on port 8080 of your-server.dyndns.org, and any connections that are made onto your-server.dyndns.org:8080 will be forwarded over the SSH tunnel to the computer which has opened that SSH connection, and from there will be directed to localhost:80.

The -N option instructs SSH not to open a shell or whatever, just to establish the port forwarding, so you can send it into the background and leave it running.

Putty uses pretty much the same syntax, but wrapped into some sort of GUI. The principle is the same though.

But be careful in what you do. Since you're essentially funneling external traffic into your network, you are pushing a hole in your network's firewall. If it is not your network, your admin may object to this and take you responsible—usually there is a reason why you are not allowed certain kinds of traffic.

12
  • 1
    no its just my home network but and im using ssh on a mac which i think is an implimentation of openssh code but going to mysite.com:8080 is giving an error of a nonexistant server, i check with telnet and it says there nothing Dec 10, 2012 at 2:06
  • but i just checked on the remote computer and it says this, tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 0 14875755 - Dec 10, 2012 at 2:07
  • How exactly does your commandline look like? The localhost:80 part is the crucial thing here—it has to exist from the point of view of your local computer, not of the remote one, otherwise you'll get errors like the above. Dec 10, 2012 at 2:15
  • yea i wrote in 127.0.0.1 instead, ill try localhost... Dec 10, 2012 at 2:25
  • 1
    It seems as if the remote side only binds on localhost instead of to all interfaces. Try doing it this way: ssh -R *:8080:localhost:80 -N ..., that way you're telling it to listen on port 8080 on all network interfaces that are in reach. The line above worked on my PC, but maybe the Mac version of ssh works slightly differently. Dec 10, 2012 at 2:28
2

To by able to forwarded your local port 80 not only to the loopback interface (127.0.0.1) you have to configure GatewayPorts clientspecified in /etc/ssh/sshd_config on the remote machine first.

Then forward your port with:

ssh -R 0.0.0.0:8080:localhost:80 -N [email protected]
0

Here is how you forward your webserver localhost:80 to a remote port 8080:

$ ssh -N -R 0.0.0.0:8080:localhost:80

You need to do 0.0.0.0:8080 to specify that you need a publicly accessible port: ssh would bind to "localhost" by default!

But by default, ssh is not allowed to open ports on the remote machine, so this is the sshd config you need to add:

AllowTcpForwarding yes
GatewayPorts yes

This normally goes into /etc/ssh/sshd_config or better, create a file in /etc/ssh/sshd_config.d/.

If it's scary to restart the SSH server, ask it to reload your config. On Amazon AWS, this is how you do it:

$ sudo /etc/init.d/ssh reload

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .