Basics of Networking: Switching and Routing
On our way to develop applications it’s important to understand how packages flow in a network. Sometimes you will need to setup your system to communicate with a switch or a router.
You will learn in this article the basic principles of networking, understand what is a switch, a router and how to set up so you can have a fully operating network in which your application can communicate freely.
Switching
You have a PC, Tablet, phone, we can define it as a system. You want a system to communicate with each other. What you do? You use a switch, a switch creates a network containing those to systems.
But to connect both systems to the switch you need an interface (physical or virtual) by which the systems know where to send packages.
For that you can check the existing interfaces by using:
ip link
To add an interface we run (assign an IP address to a system):
ip addr add 192.168.1.10/24 dev eth0
ip addr add 192.168.1.11/24 dev eth0
To test we can perform a ping from system A:
ping 192.168.1.11
Routing
Now imagine you have 2 different networks and you want to communicate from one system to another. Something like the following image:
For that instance you will need what is called a router. In the router there will be 2 IP addresses (one for each network). To send a package requires a gateway in the system to guide the package from the system to the router. Let’s see how it works.
You can use the following command to show the routing tables:
route
To add a new route to the routing table you perform the following command:
ip route add 192.168.2.0/24 via 192.168.1.1
ip route add 192.168.1.0/24 via 192.168.2.1
Here you can see a representation:
But you have to do this to reach all networks. What about those you don’t know the network but you need to access the internet?
For that you can set a default route in your routing table as this:
ip route add default via 192.168.2.2
Notice that we use another _._.2.X because it is a good practice to have a specific route to internal and to external networks due to security reasons.
One last detail is that it is necessary that you add a route to the routing table but the package still doesn’t reach it’s point, this happens because ip_forward is normally not enabled by default thus you need to access “/proc/system/ipv4/ip_forward” to 1.
Conclusion
You’ve learn now the basics of switching and routing, which commands to use in order to setup paths, check the routing tables and see which packages can pass the network whether it’s local or external.