Configure Linux as a Router
Routing is forwarding data traffic between two or more computer networks according to given routing rules. Router perform this action which is connected to two or more data lines from different networks. When a data packet comes in on one of the lines, the router reads the network address information in the packet to determine the ultimate destination. Then, using information in its routing table or routing policy, it directs the packet to the next network on its journey.
To reach internet for the PC or Laptop in above network, Linux box has to be configured as a router. It should route internal traffic to ISP router and internet traffic back to local network. We have two different methods to do this.
Method 1. NAT local network from Linux box
This can be easily done with just a simple configuration on your Linux box. First you need to enable ip forwarding in your kernel parameters and then adding a NAT rule to iptables as follows.
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
you can add bellow line to /etc/sysctl.conf to make your nat rule permanent after reboot
net.ipv4.ip_forward = 1
You don't need to add any aditional routing entry to your routing table in this method because your internal network trafic will be NATed at the Linux router and go outside with the source ip of interface eth0. As we don't have specify the ip address when applying NAT rule, you can even use dynamic ip for outside interface(eth0) as target MASQUERADE used.
Method 2. Add Static route to ISP router
In this method you have to add a static route to your ISP router/firewall so that return traffic to 192.168.10.0/24 will be routed via the eth0 interface of Linux box.
To reach internet for the PC or Laptop in above network, Linux box has to be configured as a router. It should route internal traffic to ISP router and internet traffic back to local network. We have two different methods to do this.
Method 1. NAT local network from Linux box
This can be easily done with just a simple configuration on your Linux box. First you need to enable ip forwarding in your kernel parameters and then adding a NAT rule to iptables as follows.
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
you can add bellow line to /etc/sysctl.conf to make your nat rule permanent after reboot
net.ipv4.ip_forward = 1
You don't need to add any aditional routing entry to your routing table in this method because your internal network trafic will be NATed at the Linux router and go outside with the source ip of interface eth0. As we don't have specify the ip address when applying NAT rule, you can even use dynamic ip for outside interface(eth0) as target MASQUERADE used.
Method 2. Add Static route to ISP router
In this method you have to add a static route to your ISP router/firewall so that return traffic to 192.168.10.0/24 will be routed via the eth0 interface of Linux box.
Comments
Post a Comment