To add a route to the routing table on CentOS, you can use the ip
command with the route add
subcommand.
For example, to add a route to the 10.0.0.0/8 network via the 192.168.1.1 gateway, you can use the following command:
sudo ip route add 10.0.0.0/8 via 192.168.1.1
This will add a route to the routing table that directs traffic to the 10.0.0.0/8 network to be sent through the 192.168.1.1 gateway.
You can also specify a device (e.g. an interface) that should be used for the route. For example, to add a route using the eth0
interface:
sudo ip route add 10.0.0.0/8 via 192.168.1.1 dev eth0
If you want the route to be automatically added when the system boots, you can add it to the /etc/sysconfig/network-scripts/route-eth0
file (replace eth0
with the appropriate interface name). The route will be automatically added when the interface is brought up.
For example, to add the route to the /etc/sysconfig/network-scripts/route-eth0
file:
echo "10.0.0.0/8 via 192.168.1.1" | sudo tee -a /etc/sysconfig/network-scripts/route-eth0
Note: The
ip
command is part of theiproute2
package, which is installed by default on most CentOS systems. If theip
command is not available on your system, you can install theiproute2
package using theyum
ordnf
package manager.
sudo yum install iproute2
or
sudo dnf install iproute2