1. Understand static routing
Static routing is the routing information manually set by the network administrator. Unlike dynamic routing, static routing will not automatically adjust with changes in network state, so it is more stable and reliable. In certain specific scenarios, static routing is particularly important when it is necessary to bypass certain network barriers or optimize specific traffic paths.
2. Temporarily add static routes
Before we start configuring a permanent static route, let's learn how to temporarily add a static route. This step can help us test before formal configuration.
2.1 Using the route command
Open the terminal and use the route command to temporarily add a static route. For example, suppose we need to add a route through the gateway 192.168.1.1 for the subnet 192.168.100.0/24, we can execute the following command:
sudo route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.1.1
2.2 Verify that the route is effective
After adding a route, you can useroute -n
The command checks the current routing table to confirm that the newly added route has taken effect:
route -n
3. Permanently add static routes
Although the above method can temporarily solve the problem, these routing configurations will be lost after restarting the system. In order to achieve persistence of routing configuration, we need to edit the network interface configuration file.
3.1 Edit the network interface configuration file
In CentOS 6, the configuration file for the network interface is located at/etc/sysconfig/network-scripts/
In the directory, the file name is usuallyifcfg-eth0
(Depending on your network interface name). Use a text editor to open the corresponding configuration file:
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
Add the following line at the end of the file to define the static route:
POSTUP="ip route add 192.168.100.0/24 via 192.168.1.1 dev eth0" PREDOWN="ip route del 192.168.100.0/24 via 192.168.1.1 dev eth0"
HerePOSTUP
andPREDOWN
Specifies commands executed when the network interface is started and shut down.
3.2 Restart the network service
After you have finished editing, save and exit the editor. Then restart the network service to make the changes take effect:
sudo service network restart
Or, if you just want to restart a specific network interface, you can use:
sudo ifdown eth0 && sudo ifup eth0
4. Verify permanent static routes
Finally, use againroute -n
The command checks the routing table to confirm that the new static route has been successfully added and still exists after the system restarts.
In CentOS 6 systems, adding permanent static routing often involves modifying the network configuration file to ensure that the routing settings are still valid after the system restarts. Here is an example of a practical application scenario, assuming we need to add a static route through 192.168.1.1 to a specific subnet (for example, 192.168.2.0/24).
Step 1: Edit the network interface configuration file
First, you need to edit the configuration file of the network interface. Assuming your network interface is eth0, you need to edit the /etc/sysconfig/network-scripts/ifcfg-eth0 file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
Add the following line at the end of the file:
POSTUP="ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0" PREDOWN="ip route del 192.168.2.0/24 via 192.168.1.1 dev eth0"
Step 2: Edit the routing configuration file
Another way is to edit the routing configuration file directly. For each network interface, a file named route-<interface> can be created in the directory /etc/sysconfig/network-scripts/. For example, for eth0, create a route-eth0 file:
sudo vi /etc/sysconfig/network-scripts/route-eth0
Add the following to the file:
192.168.2.0/24 via 192.168.1.1 dev eth0
Step 3: Restart the network service
After saving the file and exiting the editor, restart the network service to apply the new routing configuration:
sudo service network restart
Verify the route
You can useip route
Command to verify that the newly added route takes effect:
ip route show
You should be able to see outputs similar to the following:
192.168.2.0/24 via 192.168.1.1 dev eth0
Remark
- If you have multiple network interfaces or multiple static routes that need to be configured, you can configure each interface and route separately according to the above method.
- Make sure to use the correct network interface name and IP address when editing the configuration file.
- If you operate in a production environment, please back up the relevant configuration files first to prevent unexpected situations.
Through the above steps, you can successfully add permanent static routes in CentOS 6 systems. In CentOS 6 Linux systems, adding permanent static routing often involves editing network configuration files or using command line tools to ensure that routing settings are still valid after restart. Here are two common methods:
Method 1: Edit the network interface configuration file
-
Open the network interface configuration file: Opens the configuration file related to the network interface you want to add static routes. These files are usually located in
/etc/sysconfig/network-scripts/
In the directory, the file name is usuallyifcfg-eth0
、ifcfg-eth1
wait.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
-
Add static routes: Add in the file
POSTUP
andPREDOWN
Directives to define routes. For example, if you want to subnet192.168.2.0/24
Add a pass gateway192.168.1.1
The static route can be written like this:
POSTUP="ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0" PREDOWN="ip route del 192.168.2.0/24 via 192.168.1.1 dev eth0"
- Save and exit: Save the file and exit the editor.
- Restart the network service: In order for the change to take effect, the network service needs to be restarted.
sudo service network restart
Method 2: By editingroute-<interface>
File
-
Create or edit routing files: exist
/etc/sysconfig/network-scripts/
Create or edit a directory calledroute-<interface>
file, in which<interface>
is your network interface name (for exampleeth0
)。
sudo vi /etc/sysconfig/network-scripts/route-eth0
- Add static routes: Add routing entries directly to the file. The format is as follows:
192.168.2.0/24 via 192.168.1.1 dev eth0
- Save and exit: Save the file and exit the editor.
- Restart the network service: In order for the change to take effect, the network service needs to be restarted.
sudo service network restart
Verify the route
No matter which method is used, you can verify that the route has been successfully added with the following command:
ip route show
Or use traditionalnetstat
Command:
netstat -rn
Both methods ensure that the static route remains present after the system restarts. Which method to choose depends on your specific needs and preferences.
The above is the detailed content of the method of adding permanent static routing in CentOS 6 Linux system. For more information about CentOS 6 Linux permanent static routing, please pay attention to my other related articles!