ip route add/del


Linux 添加静态路由规则详解

在 Linux 系统中,可以使用 ip route 或过时的 route 命令来添加静态路由。推荐使用 ip route,因为它是较新的标准工具。


一、使用 ip route 命令(推荐)

命令格式:

ip route add <目标网络>/<子网掩码长度> dev <接口名称>

示例:

ip route add 10.10.180.0/24 dev eth2

参数说明:

  • 10.10.180.0/24:目标网
#!/bin/sh # Set the name of the primary network interface primary_interface="eth0" # Set the name of the secondary network interface secondary_interface="wlan0" # Set the IP address range of the local network local_network="192.168.1.0/24" primary_interface_table="eth0_table" # Keep running the script indefinitely while true; do # Check if the primary interface is up and connected to the local network if ip addr show $primary_interface up | grep -q $local_network; then # Add a new routing table for the primary interface echo "200 $primary_interface_table" >> /etc/iproute2/rt_tables # Add default route for primary interface to the new routing table primary_gateway=$(ip route show | grep "default" | grep "$primary_interface" | awk '{print $3}') ip route add default via $primary_gateway dev $primary_interface table $primary_interface_table # Add a rule to route all traffic from primary interface through the new routing table primary_ip=$(ip addr show $primary_interface | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) ip rule add from $primary_ip table $primary_interface_table # Remove any existing default route for the secondary interface ip route del default dev $secondary_interface else # Remove any existing routing table for the primary interface existing_table=$(grep -n " $primary_interface_table" /etc/iproute2/rt_tables | cut -f1 -d:) if [ ! -z "$existing_table" ]; then sed -i "${existing_table}d" /etc/iproute2/rt_tables ip route flush table $primary_interface_table ip rule del table $primary_interface_table fi # Add default route for the secondary interface secondary_gateway=$(ip route show | grep "default" | grep "$secondary_interface" | awk '{print $3}') ip route add default via $secondary_gateway dev $secondary_interface fi # Wait for 1 second before checking the network interfaces again sleep 1 done 运行上述脚本,提示ip: RTNETLINK answers: File exists, 请问如何修正
05-31
### 如何使用 `ip route add` 配置路由 在 Linux 系统中,`ip route add` 命令用于添加静态路由。通过指定目标网络、子网掩码和下一跳网关,可以确保数据包被正确转发到目标网络。以下是该命令的详细用法和示例: #### 命令格式 ```bash ip route add <network_ip>/<cidr> via <gateway_ip> dev <interface> ``` - `<network_ip>`:目标网络IP 地址。 - `<cidr>`:目标网络的 CIDR 表示法(如 24 表示 /255.255.255.0)。 - `<gateway_ip>`:下一跳网关的 IP 地址。 - `<interface>`:可选参数,指定数据包发送的网络接口。 #### 示例配置 假设需要将所有发往 `192.168.102.0/24` 网络的数据包通过网关 `192.168.204.111` 转发,可以执行以下命令: ```bash ip route add 192.168.102.0/24 via 192.168.204.111 ``` 如果需要明确指定网络接口,例如 `eth0`,可以添加 `dev` 参数: ```bash ip route add 192.168.102.0/24 via 192.168.204.111 dev eth0 ``` #### 验证路由是否成功添加 可以通过以下命令查看当前路由表: ```bash ip route show ``` 这将显示所有已配置的路由条目,包括刚刚添加的静态路由。 #### 删除路由 如果需要删除已添加的路由,可以使用以下命令: ```bash ip route del 192.168.102.0/24 via 192.168.204.111 ``` #### 注意事项 - 添加的静态路由仅在当前会话有效。若需永久生效,需将其写入系统的启动脚本或网络配置文件中[^1]。 - 如果系统中已存在匹配的路由条目,新添加的路由可能不会生效,除非使用 `replace` 参数替换原有条目[^3]。 ```bash ip route replace 192.168.102.0/24 via 192.168.204.111 ``` ### 路由匹配原则 路由匹配遵循最长前缀匹配原则。当有多个路由条目与目标网络匹配时,系统会选择前缀长度最长的路由[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值