How to Configure and use Linux as a Router

This tutorial explains how to enable IP forwarding in Linux. Learn how to use a Linux system as the router in detail through the practical example.

Each computer, in the LAN network, uses a unique software address that is known as the IP address. For easier management and several technical reasons, IP addresses are grouped into the IP networks and the IP networks are further categorized into the five IP classes.

By default, an IP address of an IP network can't communicate with the IP address of another IP network. This means, if you have two devices and both use IP addresses from different IP networks, they can't communicate with each other.

Due to any reason, if computers in your network are configured with the IP addresses of the different IP networks, then to connect them, you need a device that supports the IP forwarding. IP forwarding is a feature that allows communication between the devices of different the IP networks.

Router is a special device that not only provides the IP forwarding as the main function but also supports several other IP based features such as; packet filtering, voice over IP, IP firewall, etc.

Router is an expensive device. Configuring it is also a complex task. Not every network, especially the small and home office network, can afford it.

Linux provides a zero-cost solution for the IP forwarding. If you have a Linux system in your network, you can use it for the IP forwarding. The below section explains this process step by step with a practical example.

LAB setup

For demonstration purpose, I will use three systems named as; Linux (RHEL 7.2), PC-A (Windows 7), and PC-B (Ubuntu 18.4). I will use the Linux system as the router and will use the remaining two systems (PC-A and PC-B) to simulate the two different networks.

The following image shows the IP configuration of the PC-A (Windows 7).

ip configuration on windows testing system

The following image shows the IP configuration of the PC-B (Ubuntu 18.04).

ip configuration on ubuntu testing system

Since the process of setting IP address varies in each OS and you can use any OS to simulate the different networks, it's not feasible for me to provide step by step instructions for each OS. However, if you are using the Linux system for testing, you can follow the same steps which I will use to configure the IP address on my Linux system.

Setting IP configuration on Linux system

For this section, I assume that you have two or more PCs configured with different IP networks. If you have multiple PCs in each network, make sure each PC of each network uses the same default gateway IP address.

The following image shows my LAB with IP configuration.

practice lab with ip configuration

Make a list of all gateway IPs which you assigned in your network. For example, in my LAB I have two networks and the default gateway IPs of both are; and

Now, configure all these gateway IP addresses in the Linux system. You can use an individual network interface (LAN) for each gateway IP or can use a single network interface for all gateway IPs. For example, in my LAB, I have two gateway IPs. I can assign both gateway IPs to a single network interface or can assign them to two separate network interfaces individually.

Since Linux allows us to configure multiple IP addresses on a single network interface, the second option would be a waste of resources.

To configure gateway IPs on Linux, use the following steps.

Login from the root account and run the nmtui command.

nmtui command

Select "Edit a connection" option and press the Enter key

edit a connection option

Select available Ethernet (network interface) from the left pane and the Edit option from the right pane and hit the Enter key.

select network inteface for editing

Set IP configuration as explained in the following image.

set ip configuration

When adding IPs, in step2, make sure you add IP with the subnet-mask in slash notation. Slash notation is the other way to write the subnet mask along with the IP address. To know more about the slash notation and how to make or use it, check this tutorial.

Subnetting Tutorial - Subnetting Explained with Examples

Select the Quit option and press Enter key.

quit for editing

Restart the network service.

systemctl restart network

The nmtui tool, which we used to set the IP configuration, automatically applies all changes when quit form it. So, technically we don't need to restart the network service. We use this command to verify that everything is configured properly.

Confirm the IP configuration. To print the IP configuration of all network interfaces, use the "ip addr" command.

ip addr show

At this point, systems from different networks can access the default gateway, but they can't access the system of another network. To verify connectivity between systems and gateway, you can use the ping command.

The following image shows connectivity between PC-A and the default gateway.

testing connectivity between windows and linux

The following image shows connectivity between PC-B and the default gateway.

testing connectivity between ubuntu and linux

Enabling IP forwarding on Linux

Run the following command to enable the IP forwarding.

#echo "1" > /proc/sys/net/ip4/ip_forward

enabling ip forwarding temporary

This command enables IP forwarding in the current session. It does not make a change in that configuration file which Linux reads at the startup. By default, the state of IP forwarding is disabled in the configuration file. It means Linux will automatically disable the IP forwarding again when you will restart the system.

To enable IP forwarding permanently, run the following command.

#echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

enabling ip forwarding permanently

Once the IP forwarding is enabled, Linux works as the router. It forwards all incoming data packets to their correct destination.

To verify this, test connectivity between the PCs of different networks.

The following image confirms connectivity between PC-A and PC-B from the Windows (PC-A) system.

verifying linux as router from windows

The following image verifies connectivity between PC-B and PC-A from the Ubuntu (PC-B) system.

verifying linux working as router from ubuntu

If you are following my LAB setup, make sure you also disable the windows firewall on the Windows system otherwise you will not get the response of the ping command from the Windows system.

That's all for this tutorial. If you like this tutorial, please don't forget to share it with friends through your favorite social channel.

ComputerNetworkingNotes Linux Tutorials How to Configure and use Linux as a Router