Linux – Forwarding MySQL connection with iptables and differents network interfaces

iptableslinuxMySQLnat;port-forwarding

I have a PC with Ubuntu as a router. It has a 3G connection with a public IP to the Internet, and there is a private wireless subnet. So it has two active interfaces:

  • ppp0: public IP (WAN)
  • wlan0: private IP (LAN)

With iptables I wannt to forward every MySQL connection (port 3306) to a local machine (10.42.43.10) of the subnet.

I type these iptables commands:

iptables -A PREROUTING -t nat -i ppp0 -p tcp --dport 3306 -j DNAT --to 10.42.43.10:3306
iptables -A FORWARD -p tcp -i ppp0 -o wlan0 -d 10.42.43.10 --dport 3306 -j ACCEPT

But it doesn't work. telnet publicip 3306 fails 🙁

Any help will be appreciated. Thanks!

Best Answer

You can create a ssh tunnel for forwaring the connections. It's much easier and secure than using iptables:

ssh -L YOUR_PUBLIC_IP:3306:10.42.43.10:3306 YOUR_USER@10.42.43.10

You will have to enter the ssh user credentials and the redirection through tunnel will be done. Fast, easy and secure :)