一. route命令
1) 查看:route -n
2)添加: route add [-net|-host] target [netmask Nm] [gw Gw] [[dev If]
目标:192.168.1.3 网关:172.16.0.1(必须与本地地址在同一网络)
~]# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0(经由本地哪个网卡)
目标:192.168.0.0 网关:172.16.0.1
~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
~]# route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
默认路由,网关:172.16.0.1
~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
~]# route add default gw 172.16.0.1
3)删除:route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
目标:192.168.1.3 网关:172.16.0.1
~]# route del -host 192.168.1.3
目标:192.168.0.0 网关:172.16.0.1
~]# route del -net 192.168.0.0 netmask 255.255.255.0
二. ip route - routing table management
1) 添加路由:ip route add TARGET via GW [dev IFACE] [src SOURCE_IP] (dev表示流出的接口,via:经由下一跳)
例如:ip route add 192.168.1.3 via 172.16.0.1 dev eth0
ip route add 192.168.0.0/24 via 172.16.0.1
注意TARGET中的格式:
主机路由:IP
网络路由:NETWORK/MASK
2) 添加默认网关:ip route add defalt via GW [dev IFACE] ----dev指明由哪条接口出去
3) 删除路由:ip route del TARGET
4) ip route show,或者ip route list查看路由
5) ip route flush-----清空整个路由表
[dev IFACE]---仅清空与某个接口相关联的
[via PREFIX]--仅清空与某个网关相关联的,不常用
三. 修改配置文件永久生效
echo "route add -net 172.31.0.0 netmask 255.255.255.0 gw 172.24.150.1 dev p3p2" >> /etc/rc.local