Setting up Linux machine as a Gateway

Posted: February 4, 2015 in linux
Tags: ,

Linux machine can be setup as gateway to allow the machines which are present in local network to communicate with external machines/internet.

Linux machine should have atleast two network interfaces, say eth0 – connected to internet, eth1 – connected to local LAN.

1. Enable IP forwarding

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

2. Run the following commands ( assuming 192.168.187.0/24 as the subnet of LAN. )

iptables –table nat –append POSTROUTING –out-interface eth0 -j MASQUERADE
iptables –append FORWARD –in-interface eth1 -j ACCEPT
iptables -I FORWARD 1 -s 192.168.187.0/24 -j ACCEPT
iptables -I FORWARD 1 -d 192.168.187.0/24 -j ACCEPT

You are done….

I am using static IP addresses here. If you need to use DHCP, you have to setup DHCP server also.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s