Ruan Bekker's Blog

From a Curious mind to Posts on Github

How to Do Port Forwarding With Iptables

This is a quick post on how to do port forwarding with iptables on linux.

What would we like to achieve

We have a lxc container running a redis server and we would like to do port forwarding so that we can reach the server over the internet

LXC Host

On our host that hosts our lxc containers, we want to forward the host port 5379 to 6379 of the container (, so we can connect on a non-standard redis port:

$ iptables -t nat -I PREROUTING -p tcp --dport 5379 -j DNAT --to-destination

Test over the Internet

Test the connection by connecting to the LXC Host’s IP:

$ redis-cli -h -p 5379 -a "${REDIS_PW}"  get test
"It's working!"

Thank You

Thanks for reading my short post on how to use iptables to do port forwarding.