- So let's talk about routing.
Let's say that this is your server
and this server typically is connected to a network
but maybe your server is connected
to two different networks.
And right here
there's a router
providing access
to the internet.
Now, how does your server normally connect to the internet?
Well, if this is the network
let's say 192.168.11.0 slash 24,
then your server needs to note the IP address of this router
the IP address that it has on the same network.
So that will be 192.168.11.1 for instance.
And this IP address is going to be set
as the default router.
That means that any packet that your server generates
which has an IP address that is not on the local network
will be sent to this IP address.
And this IP address will forward to the internet.
But, the situation is becoming a little bit different.
If at the other side there is some networking as well.
So let's say right here
we have another network.
So maybe your server is connected to 10.0.0.0 slash 24.
And here we have another network, 10.9.0.0 slash 24.
Now the thing is that we need to identify
how to get to this specific network
and we need to identify how to get to this network.
Well, for this network, it's easy.
That's a local route.
For the other network
the 10.9 network, it's not so easy
and we need to create an entry in the routing table.
How are we going to do that?
Well, by identifying the IP address of this router
that is going to bring us there.
So 10.0.0.1 might be the IP address.
And then we need to define the static route.
And the static route will be 10.9.0.0 slash 24
via 10.0.0.1.
That means that all packets that have a destination
on the 10.9 network will be sent to this guy
and this router will make sure that the packet arrives
at its destination on the other network.
Because if you are going to send it to the default route
well, you are sending it in the wrong direction.
So that shouldn't be happening.
And that's what you need to do
in order to configure static routing.
All right,
so let's talk about how we configure these static routes.
All network nodes are configured with a default gateway.
That's a standard as you've just seen.
And the default gateway
specifies which node to use to address external nodes.
So default gateways is simple.
Everything is going to be sent to the default gateway
default gateway is taking care of the rest.
To see the default gateway
you can use the ip route show command.
Static routes can be added to the final route
to a network that is not behind the default gateway
but as we have seen in the drawing behind another gateway.
And if you want to add them in a non-persistent way
you can use the ip route add command.
So that will be a command like IP route add 192.2.0.0
slash 24 via 10.0.0.10.
That means that 10.0.0.10 needs to be on the local network
and that is the host that you are going to address
for all packets that need to go to 192.2.0.0.
But hey, who cares about non-persistent static routes?
What you should care about is the persistent static routes.
So let's talk about those.
On Ubuntu Netplan, static routes are added
as a property of the network card in the etc Netplan,
whatever dot yaml.
And from there, if you apply the configuration, the routes
as we can see here, are applied as well.
So it's pretty simple.
You just enter routes to, which is the destination network
and via, which is the default router
that you are going to use.
On Red Hat, the best way to add a static route
is by using nmcli connection modify.
That will be a command like nmcli con modify ens33.
And then you need to be careful
because you need +ipv4.routes.
The plus is adding,
and if you just use ipv4.routes, nothing is being added.
And then we have the network and the gateway
all between double quotes.
Don't forget the subnet mask behind the network
otherwise it won't work.
And alternatively, you can also use the nmtui utility
to add the static route.
And the result is written
to etc network manager system connections.
Let's check it out.
So let me start with ip route show
where we can see that there's a default route.
And the default route is sending all packets
to 192.168.29.2.
Now, ipa is showing that we have
ens33 as a default network connection.
So based on that,
I can use nmcli connection modify on ens33,
followed by +ipv4.routes
followed by the name of the network
and the default gateway
and the router that we need to use to address that network.
So that's going to be all.
And do we see that? ip route show?
Well, we don't see it because it has just been added
to the network cards, and we need nmcli connection
down on ens33 and up again to activate the changes.
And now ip route show is showing the new route.
That's the one going via 29.50.
Now this configuration is written
to etc Network Manager system-connections.
You should notice that the default location
for these configuration files has changed.
On recent Red Hat, it's etc network manager.
On old Red Hat, it is etc sys-config network dash scripts.
Here we can see the configuration ens33 nm connection.
And if you look at that
then you can see that all the configuration is right there.
As you can see the connection parameters.
And here we have the route that was just added.
So that's all.
That's how you add static routes.