- Create a VM or Container instance in cloud with minimal linux based distro. e.g.: Debian or Ubuntu. Create with smallest capacity/resource to reduce the cost.
- Add client's public key of SSH key pair in authorized_keys or while creating VM.
- Enable
net.ipv4.ip_forwardRestart sysctl service or machine. Command to restart sysctlsudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.confEnablement can be check withsudo systemctl restart systemd-sysctl
sysctl -p
- Allow the the ports requried to serve the traffic. e.g.: 80, 443 or 22.
- SSH remote forwarding might not be allow to directly listen on port less than 1024. It requires root access.
- Client can listen on different port and then setup port forwarding on proxy machine.
Commands to do port forwarding
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080 # In client we can map 8080:localhost:80 sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 4443 # # In client we can map 4443:localhost:443
- Clone this repo
git clone https://github.com/parveenchahal/remote-port-forwarding-with-ssh.git
-
cd remote-port-forwarding-with-ssh/client -
chmod +x ./setup.sh
- Install
sudo ./setup.sh --user $USER - Configure
/etc/remote-port-forwarding-with-ssh/remote-port-mapping.confOpen fileAdd port mappingsudo vim /etc/remote-port-forwarding-with-ssh/remote-port-mapping.conf8080=localhost:80 4443=localhost:443 - Configure
/etc/remote-port-forwarding-with-ssh/remote.confOpen fileAdd proxy details for sshsudo vim /etc/remote-port-forwarding-with-ssh/remote.confuser=ubuntu address=<proxy vm IP/Domain> port=22 ssh_key=/home/<your user>/.ssh/id_rsa - SSH manually for first time so that remote host can be added in known_hosts file.
ssh <user>@<IP/Domain> - Enable service in systemctl
sudo systemctl enable remote-port-forwarding-with-ssh.service - Start service
sudo systemctl start remote-port-forwarding-with-ssh.service - With any change in conf file service requires restart.
sudo systemctl restart remote-port-forwarding-with-ssh.service