Jingwei's

A breath of fresh air in a busy world!

Adding permanent static routes in CentOS involves several methods

In daily use, servers often have two IP addresses and configurations for two network cards, accessing different network segments, which is a common scenario. However, we need to create additional routing entries to ensure that data packets are forwarded through the correct gateway, allowing the interface to communicate normally. The following has been tested on CentOS 7 and 8: 1. **Using the `route` command to add a temporary route, which will be invalid after a restart** Parameters for the `route` command: - `add`: to add a route - `del`: to delete a route - `-net`: to set a route to a specific network segment - `-host`: to set a route to a specific host - `gw`: the exit gateway IP address - `dev`: the name of the physical device for the exit gateway Example commands: ```bash # Adding a route to a host (sysin) route add -host 192.168.1.123 dev eth0 route add -host 192.168.1.123 gw 192.168.1.1 # Adding a route to a network route add -net 192.168.1.123 netmask 255.255.255.0 eth0 route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 eth1 route add -net 192.168.1.0/24 eth1 # Adding a default gateway route add default gw 192.168.1.1 # Deleting a route route del -host 192.168.1.11 dev eth0 route del -net 192.168.1.123 netmask 255.255.255.0 ``` To view routing information: ```bash ip route route -n ``` 2. **Adding a permanent route in Linux** - **Default Gateway** - (1) Write into the ifcfg file (recommended) Edit `/etc/sysconfig/network-scripts/ifcfg-eth0` and directly write the GATEWAY configuration into the ifcfg file when setting the IP address. The format is: `GATEWAY=gw-ip`. This is suitable for adding a default route. - (2) Add to the end of the `/etc/sysconfig/network` file, for example: ``` GATEWAY=gw-ip or GATEWAY=gw-dev ``` - **Writing to `/etc/rc.local` (not recommended)** (Note: CentOS 7 must execute `chmod +x /etc/rc.d/rc.local` to ensure this script runs at boot.) You can write the aforementioned commands into the `/etc/rc.local` file so that the system will automatically add the relevant routing settings upon startup. However, there is a disadvantage (sysin): Suppose a system service, such as the NFS service, is started after the network service and before running rc.local. If you have set up NFS to mount automatically, the link may not be established, causing the mount to fail. Additionally, if you restart the network server, the route will become invalid, and you will have to load this file again. But what if you are operating remotely? Therefore, this method is not recommended. Method: Edit `/etc/rc.local` and use the `route` command syntax to add: ```bash route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.3.254 route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.100 dev eth0 ``` Example of the modified `/etc/rc.d/rc.local` file: ```bash #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.3.254 route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.100 dev eth0 ``` - **Writing to `/etc/sysconfig/static-routes`** By default, there is no such file in the `/etc/sysconfig` directory, and it needs to be created manually. The invocation of this file is as follows: ```bash cat /etc/init.d/network # Add non interface-specific static-routes. if [-f /etc/sysconfig/static-routes]; then if [-x /sbin/route]; then grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do /sbin/route add -$args done else net_log $"Legacy static-route support not available: /sbin/route not found" fi fi ``` To add: ```bash vi /etc/sysconfig/static-routes any net 192.168.1.0/24 gw 192.168.1.1 any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 any host 10.19.190.11/32 gw 10.19.177.10 any host 10.19.190.12 gw 10.19.177.10 ``` This method is more useful compared to rc.local. For example, with NFS, the route takes effect when the network service starts, and other network-related services start after the network service has successfully started, ensuring network connectivity. Additionally, if you restart the network service, the script is called within the network service startup script. Therefore, it also adds the routes you have set up manually. This method is not valid by default in CentOS 8. In CentOS 8, the default network management tool is `nmcli`. You can install the traditional `network.service` by running `yum install network-scripts` to revert to using this method for configuring static routes. - **Creating `/etc/sysconfig/network-scripts/route-eth0` (recommended)** ```bash # Create a file named route-eth0 in the `/etc/sysconfig/network-scripts/` directory vi /etc/sysconfig/network-scripts/route-eth0 # Add the following format content to this file 192.168.1.0/24 via 192.168.0.1 # Restart the network to verify the effectiveness systemctl restart network ```

Add comment:

◎Welcome to take comment to discuss this post.

«    November , 2024    »
Mon Tue Wed Thu Fri Sat Sun
123
45678910
11121314151617
18192021222324
252627282930
Categories
Search
Latest Comments
    Archives
    Favorite Sites
    Link Exchange

    Powered By Z-BlogPHP 1.7.3

    Copyright 2024-2028 Jingwei's. All Rights Reserved.