How to add a static route in CentOS 7?
To add a static route on CentOS 7, you can follow these steps:
- Open the terminal and log in as the root user.
- Use the following command to view the current system’s network configuration information:
- Show IP addresses.
- Add a static route using the following command:
- Add a route to the destination network with the specified subnet mask via the designated gateway address on the specified network interface.
- For instance, to add a route to the network address 192.168.0.0/24 with a gateway address of 192.168.1.1, network interface eth0, you can use the following command:
- Add a route for the network 192.168.0.0/24 through the interface eth0 with the gateway 192.168.1.1.
- Use the following command to verify if the newly added static route is effective:
- display IP routes
- To permanently save a static route, you can edit the /etc/sysconfig/network-scripts/route-
file. For example, to edit the static route for the eth0 interface, you can use the following command: - Open the file route-eth0 in the directory /etc/sysconfig/network-scripts using the vi editor.
- Add the configuration for static routes in the file, then save and exit.
- Restart the network service to apply the changes.
- Restart the network service using systemctl.
After adding a static route, the system will use this route to forward packets to the destination network address.