Using linux as a router

Level: - beginners
Platform: - RED hat Linux 9.0
Keywords: - Linux as router, enabling IP forwarding, creating sub interfaces in linux
Author: - Dinesh Aggarwal

Generally people who have worked mostly on windows find it tough to work on Linux. Linux is a great OS and has got amazing powers. If you want to use Linux and a router it is very simple. Just install two network cards on a pc and install Linux on it. After assigning IP addresses to both the NIC cards you need to enable IP forwarding.

ENABLING FORWARDING BETWEEN TWO NIC CARDS
==============================================

[root@TEST root]# echo "1" >> /proc/sys/net/ipv4/ip_forward

Once you type the above command the default value of 0 changes to 1 as shown below.

[root@TEST root]# more /proc/sys/net/ipv4/ip_forward

1

Now connect two nic cards of Linux to two different networks and the routing between these two networks will be taken care by IP forwarding.


CONFIGUTRING VIRTUAL IP ADDRESSESS

 

If we have only one network card in Linux and would like to create say 8 networks and route between those 8 networks, sub interfaces is the answer. You can create as many sub interfaces as you desire
Let’s create following 8 networks.

172.31.117.0/255.255.255.0
172.31.118.0/255.255.255.0
172.31.119.0/255.255.255.0
172.31.120.0/255.255.255.0
172.31.121.0/255.255.255.0
172.31.122.0/255.255.255.0
172.31.123.0/255.255.255.0
172.31.124.0/255.255.255.0

Give the following commands to create these sub interfaces

ifconfig eth1 172.31.117.1 netmask 255.255.255.0
ifconfig eth1:0 172.31.118.1 netmask 255.255.255.0
ifconfig eth1:1 172.31.119.1 netmask 255.255.255.0
ifconfig eth1:2 172.31.120.1  netmask 255.255.255.0
ifconfig eth1:3 172.31.121.1  netmask 255.255.255.0
ifconfig eth1:4 172.31.122.1  netmask 255.255.255.0
ifconfig eth1:5 172.31.123.1  netmask 255.255.255.0
ifconfig eth1:6 172.31.124.1 netmask 255.255.255.0

give the following commands

cd /etc/sysconfig/network-scripts

vi ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.117.255
IPADDR=172.31.117.1
NETWORK=172.31.117.0

vi ifcfg-eth1:1
DEVICE=eth1:1
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.118.255
IPADDR=172.31.118.1
NETWORK=172.31.118.0

vi ifcfg-eth1:2
DEVICE=eth1:2
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.119.255
IPADDR=172.31.119.1
NETWORK=172.31.119.0

vi ifcfg-eth1:3
DEVICE=eth1:3
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.120.255
IPADDR=172.31.120.1
NETWORK=172.31.120.0

vi ifcfg-eth1:4
DEVICE=eth1:4
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.121.255
IPADDR=172.31.121.1
NETWORK=172.31.121.0

vi ifcfg-eth1:5
DEVICE=eth1:5
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.122.255
IPADDR=172.31.122.1
NETWORK=172.31.122.0

vi ifcfg-eth1:6
DEVICE=eth1:6
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.123.255
IPADDR=172.31.123.1
NETWORK=172.31.123.0

REBOOTING THE SYSTEM
==============================

/sbin/reboot

CHECK AFTER REBOOT
===============================

[root@localhost network-scripts]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:D0:B7:BE:BB:97
inet addr:172.16.100.162  Bcast:172.16.100.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:6622 errors:0 dropped:0 overruns:0 frame:0
TX packets:1100 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:572897 (559.4 Kb)  TX bytes:273660 (267.2 Kb)
Interrupt:11 Base address:0xc000 Memory:e9100000-e9100038

eth1      Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.117.1  Bcast:172.31.117.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:0    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.118.1  Bcast:172.31.118.255 Mask: 255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:1    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.119.1  Bcast:172.31.119.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:2    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.120.1  Bcast:172.31.120.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:3    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.121.1  Bcast:172.31.121.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:4    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.122.1  Bcast:172.31.122.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:5    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.123.1  Bcast:172.31.123.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

eth1:6    Link encap:Ethernet  HWaddr 00:0D:61:B5:C4:60
inet addr:172.31.124.1  Bcast:172.31.124.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b)  TX bytes:240 (240.0 b)
Interrupt:11 Base address:0xd000

lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1732 (1.6 Kb)  TX bytes:1732 (1.6 Kb)

[root@localhost network-scripts]#

If you have any suggestions or want to add more to this article do write us an email articles@knowurtech.com

What Next?


If you liked this article, you can share it with others using the following link:


Related Content :