Redirect Network Traffic using Raspberry Pi in 10 min

HI all. I would like to describe to you my process on how to Redirect Network Traffic using Raspberry Pi in 10 min. 

Why redirect?

Do you need to switch from local server to cloud solution for your IoT device? IoT devices needs to talk to static IP address that is assigned to your local server to function correctly and when we need to  changing to cloud service, like Amazon’s AWS, sometimes you cannot transfer the static IP address. So what then?

Where to redirect?

Where is the best way to redirect this traffic, form local server to cloud server? The best place to do this is at the local router. But what if you have only a basic router that comes with your internet plan and doesn’t support redirect functionality, and you have one spare Raspberry Pi? Can you achieve that same? Let’s try.

Redirect traffic with Raspberry Pi

I will use iptables for this example, but first we need to check is port forwarding enabled on our raspberry pi, and we can do that with next command  as root:

cat /proc/sys/net/ipv4/ip_forward

If the return value is 1, we are ready to go, but if return value is 0, we need to set that value to 1 with next command:

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

Now we can start configuring iptables. We would like to forward specific traffic to cloud server and that traffic is defined on a couple of ports. If we have a website on our server we need to forward port 80 (for HTTP) and if we want to have an option to connect remotely to our server with ssh connection we need to forward port 22.  Also, we need to forward port that is your server specific, in my case we need to forward 1500 that is defined for our MqTT server. We can do this following commands:

iptables -t nat -A PREROUTING -p tcp –dport {Source_Port_Number} -j DNAT –to-destination {Destination_IP_address}:{Destination_Port_Number}

Example:

iptables -t nat -A PREROUTING -p tcp –dport 80 -j DNAT –to-destination 66.249.75.126:80

iptables -t nat -A PREROUTING -p tcp –dport 80 -j DNAT –to-destination 66.249.75.126:80

And now we need to save our iptables with next command:

iptables -t nat -A POSTROUTING -j MASQUERADE

And now we are done. Raspberry Pi will forward that messages on selected ports to the cloud server.

Create Startup Script

Now we need the startup script. First we need to create a simple bash script. I created a BLN_Redirect.sh that is shown below. 

The easiest way to start this script at boot up is to edit rc.local file. We need to edit this file, that is located in /etc/, with the line below, ant that is everything that we need. 

sudo bash /home/pi/BLN_Redirect.sh & 

And we are done. That was short document on how to Redirect Network Traffic using Raspberry Pi. This was a project that needed to be done and this was the easiest way that I found out. Do you have any better way? Did you manage to replicate this solution. Please, look at my other posts at https://bln364.com/, and see you in the next one. 

1 thought on “Redirect Network Traffic using Raspberry Pi in 10 min”

Leave a Comment