Docs/linux基础/路由表维护
2022-10-18 16:59:37 +08:00

45 lines
No EOL
2.1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

一. 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]--仅清空与某个网关相关联的,不常用