Linux Networking: Switching, Routing and Gateway

Claire Lee
5 min readApr 5, 2023

--

Switching happens at Layer 2 of the OSI model using switches in LANs to forward packets based on MAC addresses, while routing occurs at Layer 3 using routers to direct traffic between different networks based on IP addresses. Gateways, typically routers, connect networks and enable traffic to flow in and out. The default gateway is used by the system to forward packets to destinations outside of the local network.

Linux Networking: Switching, Routing and Gateway
Table of Contents

· Switching
Commands
· Routing
Commands
· Gateway
Default gateway

Switching

Switching is a process that occurs at the data link layer (Layer 2) of the OSI model. In the context of a local area network (LAN), a switch acts as a central point that connects multiple devices, such as computers, servers, and printers. It forwards network packets between devices within the same network (or broadcast domain) based on their MAC (Media Access Control) addresses. Switches are responsible for efficiently and accurately delivering packets to their intended destinations within the same LAN.

For example, let’s say you have a small office LAN with multiple computers connected to a switch. When computer A wants to send a network packet to computer C, it sends the packet to the switch. The switch examines the MAC address of the packet, determines which port computer C is connected to, and forwards the packet only to that port. This allows for efficient communication between devices within the same LAN without flooding unnecessary packets to all devices.

switching

Commands

  1. Display the configuration of all network interfaces
$ ip link show

2. View the IP addresses of all network interfaces

$ ip addr show

3. Add an IP address to a network interface

$ ip addr add <ip_address>/<subnet_mask> dev <network_interface>

<ip_address>: The IP address that you want to assign to the network interface.

<subnet_mask>: The subnet mask that defines the network's address range. It is expressed in CIDR (Classless Inter-Domain Routing) notation, where the number after the forward slash ("/") specifies the number of bits in the subnet mask. For example, "/24" corresponds to a subnet mask of 255.255.255.0, which means that the network has 256 IP addresses available (2^8 - 2).

<network_interface>: The name of the network interface (e.g., eth0, eth1, enp0s1, etc.) to which you want to assign the IP address.

ip addr add

Routing

Routing is the process of directing network traffic from one network to another. It involves the use of routers, which are network devices that make decisions about how to forward data packets across different networks based on their IP addresses. Routers operate at the network layer (Layer 3) of the OSI model and use routing tables to determine the most efficient path for packet delivery.

For example, consider a scenario where a computer A in a local area network (LAN) wants to send data to a computer F in another LAN.

  • computer A: IP address 192.168.1.1
  • computer F: IP address 192.168.2.3
  • switch1 with IP address 192.168.1.0
  • switch2 with IP address 192.168.2.0
  • router IP address to reach switch1: 192.168.1.5
  • router IP address to reach switch2: 192.168.2.5

Configure the gateway for computer A:

$ ip route add 192.168.2.0/24 via 192.168.1.5 dev eth0

This command adds a route to the 192.168.2.0/24 network via the IP address of the router to reach Switch1 (192.168.1.5) through the “eth0” network interface of computer A.

Configure the gateway for computer F:

$ ip route add 192.168.1.0/24 via 192.168.2.5 dev eth0

This command adds a route to the 192.168.1.0/24 network via the IP address of the router to reach Switch2 (192.168.2.5) through the “eth0” network interface of computer F.

Now, computer A can communicate with computer F, and the packet will be routed through the routers connected to Switch1 and Switch2, enabling inter-network communication between the two computers.

Routing

Commands

  1. Add a routing entry to the IP routing table. It allows you to define how network traffic should be directed for a specific destination network.
$ ip route add <destination_network_ip>/<subnet_mask> via <gateway_ip> dev <network_interface>

<destination_network_ip>: This specifies the IP address of the destination network you want to add to the routing table.

<subnet_mask>: This specifies the subnet mask for the destination network, which determines the range of IP addresses that belong to the network.

<gateway_ip>: This specifies the IP address of the gateway (router) that should be used to reach the destination network.

<network_interface>: This specifies the network interface through which the traffic should be routed to reach the destination network.

2. View the IP routing table

$ route

Gateway

A gateway is a network device, often a router, that connects a local network (e.g., LAN or VLAN) to other networks such as the Internet. It acts as a “doorway” for traffic to flow in and out of the local network, enabling communication between devices in different networks.

Default gateway

A default gateway, also known as a default route, is a special type of gateway that is used in computer networking to provide a default path for network traffic that is destined for a network outside of the local network. In other words, it is the IP address of the router that is used as the exit point for network traffic that does not have a specific route in the routing table.

When a device, such as a computer or a network switch, needs to send data to a destination network that is not part of its local network, it checks its routing table to determine the appropriate path. If there is no specific route for that destination network, the device forwards the traffic to the default gateway. The default gateway then forwards the traffic to the appropriate destination network based on its own routing table.

$ route
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

The default gateway is typically displayed as the destination “0.0.0.0” or deafult with a netmask of “0.0.0.0”.

--

--

Claire Lee
Claire Lee

Responses (2)