Tuesday, 24 February 2015

Using a Linux server to route packets between two private networks

Reference: http://www.tecmint.com/setup-linux-as-router/

We want to route icmp (ping) packets from dev2 to dev4 and the other way around as well (note that both client machines are on different networks). The name of each NIC, along with its corresponding IPv4 address, is given inside square brackets.
Our test environment is as follows:
Client 1: CentOS 7 [enp0s3:] - dev1
Router: Debian Wheezy 7.7 [eth0:, eth1:] - dev2
Client 2: openSUSE 13.2 [enp0s3:] - dev4
Let’s view the routing table in dev1 (CentOS box):
# ip route show
and then modify it in order to use its enp0s3 NIC and the connection to to access hosts in the network:
# ip route add via dev enp0s3
Which essentially reads, “Add a route to the network through the enp0s3 network interface using as gateway”.
Route Network in Linux
Route Network in Linux
Likewise in dev4 (openSUSE box) to ping hosts in the network:
# ip route add via dev enp0s3
Network Routing in Linux
Network Routing in Linux
Finally, we need to enable forwarding in our Debian router:
# echo 1 > /proc/sys/net/ipv4/ip_forward
Now let’s ping:
Check Network Routing
Check Network Routing
Route Ping Status
Route Ping Status
To make these settings persistent across boots, edit /etc/sysctl.conf on the router and make sure the net.ipv4.ip_forward variable is set to true as follows:
net.ipv4.ip_forward = 1
In addition, configure the NICs on both clients (look for the configuration file within /etc/sysconfig/network on openSUSE and /etc/sysconfig/network-scripts on CentOS – in both cases it’s called ifcfg-enp0s3).
Here’s the configuration file from the openSUSE box:

No comments:

Post a Comment