3

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!

1

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 :)

1

The rules seem correct. I think you have not enabled IP forwarding in kernel. There are two ways to enable IP forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward

This will get reset on reboot. Add the following line to /etc/sysctl.conf:

net.ipv4.ip_forward = 1

And you should be able to get it working.

1

Wasn't able to comment on the answer Nilesh posted, so i added this for anyone who might be having problems following that step

in regards to

echo 1 > /proc/sys/net/ipv4/ip_forward

this will only work if you are the root user.

you should do it this way

echo -n 1 | sudo tee /proc/sys/net/ipv4/ip_forward

echo -n is so there isn't a trailing newline character

sudo tee is so that you can write to the file with increased permissions

sudo echo > /file doesn't work if /file needs to be accessed as a root user, because sudo is only operating on the echo function and not the > /file

  • Welcome to Server Fault. I’m upvoting this answer because it’s useful for users who aren’t running a shell with superuser privileges. I’d also add that echo does not behave consistently across different Unix-like systems; printf is much more robust/reliable. – Anthony Geoghegan Nov 7 '16 at 10:20

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.