How to add routes on Linux?

In Linux systems, routing is one of the key components of network communication. By adding routes, you can specify the transmission path of data packets in the network, thereby enabling network connections and data forwarding. This article details how to add routing on Linux so that you can configure network routing according to your needs and achieve flexible network connectivity.

ipAdd a route using the command

Linux provides ipcommands to manage network interfaces and routing tables. You can use ipcommands to add, delete, and modify routing entries. Here are ipthe steps to add a route using the command:

Step 1: View the current routing table

Before adding a route, you can view the current routing table using the following command:

ip route show

This command will display the routing table information of the current system, including the target network, next-hop gateway, interface, etc.

Step 2: Add new route

Use ipthe command to add a new route. Here is the basic syntax for adding a route:

sudo ip route add <目标网络> via <下一跳网关> dev <接口>

Replace <目标网络>with the destination network address to which you want to add the route, for example 192.168.0.0/24. Replace <下一跳网关>with the gateway address of the next hop, for example 192.168.1.1. Replace with the network interface the packet will <接口>pass through, eth0e.g.

For example, to send 192.168.0.0/24packets from the destination network to an interface through gateway , you can add a route using the following command:192.168.1.1eth0

sudo ip route add 192.168.0.0/24 via 192.168.1.1 dev eth0

Step 3: Verify new routes

After adding the new route, you can use ip route showthe command again to verify whether the route was added successfully.

Persistent routing settings

By default, iproutes added through the command are temporary and will be lost after the system restarts. If you want your routing settings to remain valid across system reboots, you can persist them. Here is a commonly used method:

Use /etc/network/interfacesfile

  1. Open /etc/network/interfacesthe file for editing:

    sudo nano /etc/network/interfaces
    
  2. Find the appropriate network interface configuration in the file and add the following lines to define persistent routes:

    up ip route add <目标网络> via <下一跳网关> dev <接口>
    

    Replace <目标网络>, <下一跳网关>and <接口>with the corresponding values.

  3. Save and close the file. The route will be added automatically every time the network interface is started.

Using NetworkManager

If your system uses NetworkManager to manage network connections, you can use nmclithe command to add persistent routes. Here are the steps to add persistent routing:

  1. Open NetworkManager's connection profile using the following command:

    sudo nano /etc/NetworkManager/system-connections/<连接名称>
    

    Replace <连接名称>with the name of the network connection to which you want to add a route.

  2. Add the following lines to the file's [ipv4]section to define persistent routes:

    route1=dst=<目标网络>,nh=<下一跳网关>,dev=<接口>
    

    Replace <目标网络>, <下一跳网关>and <接口>with the corresponding values.

  3. Save and close the file. The route will be added automatically each time this network connection is initiated.

Please note that the specific method of configuring persistent routing may vary depending on different Linux distributions and network management tools. Please follow the documentation for the system and tools you are using.

Delete route

If you need to delete the added route, you can use ipthe command. Here is the basic syntax for deleting a route:

sudo ip route del <目标网络> via <下一跳网关> dev <接口>

Replace <目标网络>, <下一跳网关>and with <接口>the appropriate values ​​for the route you want to delete.

For example, to delete 192.168.0.0/24a route for a target network, you can use the following command:

sudo ip route del 192.168.0.0/24

in conclusion

By using ipthe command and appropriate configuration files, you can add routes on a Linux system and make them persistent. Adding and managing routes can help you implement flexible network connections and data forwarding to meet specific network needs. Choose the appropriate method based on your specific situation and network management tools, and add, delete, and modify routes as needed to optimize network performance and connection reliability.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131277512