Linux – Use server’s IP via SSH tunnel

linuxPROXYservicesshtunnel

Firslty my setup is an Ubuntu laptop and an Ubuntu server.

I have a program on my local laptop which needs to access a certain web-service, (lets call it http://someserver.com/someservice123). Now this service has a firewall which only allows access from my server's IP.

Is there some type of SSH tunnel I could use between my laptop and server so that when a python script on my laptop sends a request to http://someserver.com/someservice123 that service sees the request coming from my server's IP?

I know it would look something like:

ssh -N -R 80:localhost:80 user@myserver

but I'm not sure exactly.

Best Answer

What you want is not a reverse tunnel but a regular tunnel.

ssh -L 80:someserver.com:80 user@myserver

This will create a listening socket on port 80 of your laptop (localhost) that will go to someserver.com through the SSH server on myserver.

I usually combine tunnels with the options -CfN, -C will enable compression (speeds things up a bit), -f sends the SSH to the background once the authentication is complete (so you still have a chance to enter the password if needed), -N will make sure no command is executed on the SSH server (it's not really safe to have an SSH running in the background that could hypothetically be used send commands to the server, it's a bit of healthy paranoia/a good practice).

If you don't care about having a very secure connection between your laptop and myserver, you can also change the cipher to something fast, like blowfish using -c blowfish, or arcfour128 (which is faster still).

So what I would use is this:

ssh -CfNc arcfour128 -L 80:someserver.com:80 user@myserver

Which will give you nice, fast tunnel that goes straight into the background instead of leaving an open command prompt on your server.

Keep in mind that if you send it to the background, when you want to break the tunnel, you'll have to first find the process id by doing something like ps -ef | grep ssh and kill the correct process id.