- DigitalOcean
- 512 MB RAM
- Ubuntu 14.04
SSH allows you to share ports forwarded to the server with other
remote machines, not just the server itself. By default, SSH will
open ports on the loopback address 127.0.0.1 on the server, but
can be configured to use th wildcard address 0.0.0.0 instead, which
means that we will be able to reach the forwarded port from the internet.
On the server, add the following line to /etc/ssh/sshd_config:
GatewayPorts yes
Then restart the ssh daemon
sudo service ssh restart
We only allow the following connections:
- Connections present at the time running the setup
- Port 20 SSH
- Port 80 HTTP
On the server run:
sudo iptables -I INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
sudo iptables -A INPUT -p tcp --dport http -j ACCEPT
sudo iptables -A INPUT -j DROP
sudo apt-get install iptables-persistent
sudo service iptables-persistent start
On your local machine, start a web server to test out forwarding. For example,
cd to a directory and start a simple webserver using Python:
cd ~/lolcats/
python -m SimpleHTTPServer
# Serving HTTP on 0.0.0.0 port 8000 ...
Then set up remote port forwarding via SSH:
ssh root@<your-server-ip> -R 80:localhost:8000
If everything is ok, ssh should now listen on tcp 0.0.0.0:80 on the server and forward requests to port 8000 on your local machine, where the Python webserver is running. You can check whether SSHD uses the correct address on the server:
netstat -tunelp
It should look like this:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 10643 1223/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 11653 1412/0
tcp6 0 0 :::22 :::* LISTEN 0 10645 1223/sshd
tcp6 0 0 :::80 :::* LISTEN 0 11654 1412/0
Now open a web browser, navigate to http://<your-server-ip> and you should
see a directory listing produced by the Python webserver.