Configuring a New Routing Rule
Adding a Static Routing Rule
To add a new static routing rule in Linux, you can use the ip route add
command. The basic syntax is as follows:
ip route add DESTINATION_PREFIX via GATEWAY_ADDRESS dev INTERFACE
Here's an example of adding a routing rule to send traffic destined for the 192.168.2.0/24
network through the gateway 10.0.0.1
on the eth1
interface:
sudo ip route add 192.168.2.0/24 via 10.0.0.1 dev eth1
Verifying the New Routing Rule
After adding the new routing rule, you can use the ip route show
command to verify that the rule has been added correctly:
$ ip route show
default via 10.0.0.1 dev eth0
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.10
192.168.2.0/24 via 10.0.0.1 dev eth1
In the output, you can see the new routing rule for the 192.168.2.0/24
network.
Configuring a Default Route
In addition to adding specific routing rules, you can also configure a default route, which is used when no other matching route is found in the routing table. To add a default route, use the following command:
ip route add default via GATEWAY_ADDRESS dev INTERFACE
For example, to set the default gateway to 10.0.0.1
on the eth0
interface:
sudo ip route add default via 10.0.0.1 dev eth0
Removing a Routing Rule
If you need to remove a routing rule, you can use the ip route del
command. The syntax is similar to the ip route add
command:
ip route del DESTINATION_PREFIX via GATEWAY_ADDRESS dev INTERFACE
For example, to remove the routing rule for the 192.168.2.0/24
network:
sudo ip route del 192.168.2.0/24 via 10.0.0.1 dev eth1
By understanding how to add, verify, and remove routing rules, you can effectively configure the network connectivity of your Linux systems.