Skip to main content
In this lesson, we explore the fundamental concepts of networking, including switching, routing, gateways, and DNS configurations on Linux. These core topics form the building blocks for understanding how devices communicate over a network.
The image lists networking prerequisites: Switching, Routing, Default Gateway, and DNS Configurations on Linux.
Imagine you have two computers (or VMs) that need to communicate. Both systems are connected to a switch, and each host requires an interface—either physical or virtual—to establish that connection. To view the available interfaces on a Linux host, run:
Assuming the network subnet is 192.168.1.0, assign an IP address from this range to each system. For example, on one host, use:
And on another:
Once the IP addresses are configured, the systems can communicate via the switch. Test connectivity with a ping:
A switch, however, only facilitates communication within the same network. Consider introducing a second network with the CIDR 192.168.2.0, where host IPs might be 192.168.2.10 and 192.168.2.11. To enable communication between systems on the 192.168.1.0 network and those on the 192.168.2.0 network, a router becomes essential. A router connects separate networks by using multiple network interfaces. Typically, you assign it IP addresses such as 192.168.1.1 on the first network and 192.168.2.1 on the second. This configuration lets the router forward traffic between the networks.
The image illustrates a network routing diagram with devices A, B, C, and D connected through two subnets, 192.168.1.0 and 192.168.2.0.
When system B (192.168.1.11) needs to reach system C (192.168.2.10), it must use the router as its gateway. To check the current routing table on a Linux system, run:
Without additional configuration, system B can only communicate within its own network. To route traffic destined for the 192.168.2.0 network, add an entry on system B:
After adding this route, system B’s routing table will reflect that traffic to 192.168.2.0 is forwarded through the router. Any system that communicates across networks (for example, system C talking to system B) must have a similar route set up.
Using the default gateway simplifies routing for internet access. Rather than adding separate routes for every external network, you set the router as the default route.
When systems need Internet access—such as reaching Google within the 172.217.104.0 network—the router connected to the Internet must be designated as the default gateway. To set up a default route, use:
This default route catches any packet destined for an unspecified network. Running the routing command might now yield:
Both the “default” keyword and the “0.0.0.0” entry function as catch-all destinations for packets that do not match more specific routes. In environments with multiple routers, such as one handling Internet traffic and another for internal networks, you must create separate routing entries. For example, if traffic destined for network 192.168.1.0 should use a router at 192.168.2.2 while all other traffic uses 192.168.2.1 as the default gateway, set the route as follows:
The routing table would then resemble:
If you experience issues reaching the Internet, reviewing the routing table and checking the default gateway configuration is an excellent troubleshooting step.

Setting Up Linux as a Router

Now, consider configuring a Linux host as a router. Picture three hosts: A, B, and C. Hosts A and B belong to the 192.168.1.0 network, while hosts B and C are part of the 192.168.2.0 network. Host B connects to both networks via two interfaces—eth0 and eth1—with IP addresses 192.168.1.6 and 192.168.2.6 respectively. Host A has IP 192.168.1.5, and host C uses IP 192.168.2.5. Initially, if host A pings host C:
This error occurs because host A does not know to use host B as the gateway to reach the 192.168.2.0 network. To resolve this, add a route on host A:
Similarly, inform host C to use host B as its gateway for the 192.168.1.0 network:
Once these routes are added, host A can reach host C. However, you might still not receive any ping responses. This is because Linux, by default, does not forward packets between interfaces—a security measure to prevent unintended bridging of networks.
To check if IP forwarding is enabled, inspect the file /proc/sys/net/ipv4/ip_forward.
To determine if IP forwarding is enabled, run:
Since the output is 0, packet forwarding is disabled. To enable it temporarily, execute:
After enabling IP forwarding, a subsequent ping from host A should yield replies from host C:
Keep in mind that this change does not persist after a reboot. To enable permanent IP forwarding, modify the configuration in the /etc/sysctl.conf file.

Key Networking Commands

Below is a summary of essential commands covered in this lesson: With these fundamentals, you now have a solid understanding of basic networking, IP addressing, and routing. In the next lesson, we will delve into DNS configurations and further network management techniques.

Watch Video

Practice Lab