Networking Basics: Configuring Switches and Routers with Linux Commands
TECH
4/23/20242 min read
Understanding Networking Basics with Routers and Switches
When diving into the world of computer networking, it's important to grasp the basic concepts of how devices like routers and switches work together to create a robust network infrastructure. In this post, we'll discuss the fundamental roles of switches and routers, and explore some common networking scenarios using commands like `ip link`, `ip addr`, `ip addr add`, `route`, `ip route add`, and `ip_forward`.
Switches and Routers: The Building Blocks of a Network
Switches and routers are key components of any network. Let's break down their roles:
Switches are used to connect multiple devices within a local network, such as computers, servers, and printers. They operate at the data link layer (Layer 2) of the OSI model and use MAC addresses to forward packets between devices.
Routers are responsible for connecting different networks and routing traffic between them. They operate at the network layer (Layer 3) and use IP addresses to determine the best path for data packets.
Switches are typically used within a single subnet, while routers connect multiple subnets or networks.
Setting Up Networking for Multiple Subnets with Routers and Switches
This blog post will walk you through setting up a network with two subnets, focusing on how the router connects them by having an IP address in each subnet. We'll use key networking commands like `ip link`, `ip addr`, `ip addr add`, `ip route add`, and `ip_forward` to illustrate this setup.
Network Scenario with Two Subnets
Our network consists of two subnets connected by a router:
Subnet 1: IP range `192.168.1.0/24`
Subnet 2: IP range `192.168.2.0/24`
Router: Connects Subnet 1 and Subnet 2, with IP addresses in both subnets
Switches: Connect multiple devices within each subnet
The router needs to have an IP address in each subnet to facilitate routing between them. Let's examine the configuration steps for this setup.
Configuring the Router with Multiple IP Addresses
To ensure the router can communicate with both subnets, assign an IP address from each subnet to its respective network interface.
1. Assigning IP Addresses to the Router:
For Subnet 1:
For Subnet 2:
With these commands, the router has an IP address in both subnets, allowing it to route traffic between them.
Configuring Routing on the Router
After setting IP addresses, you'll need to configure routing to ensure the router forwards traffic correctly.
1. Enabling IP Forwarding:
To allow the router to forward traffic between subnets, enable IP forwarding:
2. Adding Routing Rules:
Ensure the router knows how to route traffic between the subnets:
Configuring Devices within the Subnets
With the router configured, you can set up devices within each subnet to communicate with the router.
Subnet 1
Assign an IP address:
Set the default gateway to the router's IP address in Subnet 1
Subnet 2:
Assign an IP address:
Set the default gateway to the router's IP address in Subnet 2:
Conclusion
In this example, we've set up a network with two subnets connected by a router. By assigning an IP address to each of the router's interfaces, we established communication between the subnets.