Linux路由配置
Linux 路由配置
文章目录
- Linux 路由配置
- 1. route命令介绍
- 1.1 嵌入式设备上执行route命令结果
- 1.2 网络/主机地址计算
- 2. 命令使用
- 2.1 添加主机路由
- 2.2 添加网络路由
- 2.3 添加默认路由
- 2.4 删除主机路由
- 2.5 删除网络路由
- 2.6 删除默认路由
- 2.7 添加屏蔽路由
- 2.8 删除屏蔽路由
- 2.9 配置路由优先级
1. route命令介绍
🚀Linux系统的route
命令用于显示和操作IP路由表(show/manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。🚩 在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问internet,那么就需要将网关地址设置为该Linux机器的默认路由。⚠️需要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。
1.1 嵌入式设备上执行route命令结果
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default * 0.0.0.0 U 0 0 0 eth_x
10.0.0.0 * 255.0.0.0 U 0 0 0 usb0
192.0.0.0 * 255.0.0.0 U 0 0 0 eth_x
#
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth_x
10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 usb0
192.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth_x
字段名称 | 含义 |
---|---|
Destination | 目标网络(network)或者目标主机(host)。 |
Gateway | 网关地址,*** **表示并未设置网关地址。 |
Genmask | 网络掩码。其中’255.255.255’用于指示单一目标主机,’0.0.0.0’用于指示默认路由。 |
Flags | 标记,含义参考表格后面的解释。 |
Metric | 路由距离,到达指定网络所需的中转数。当前Linux内核并未使用。 |
Ref | 路由项引用次数,当前Linux内核并未使用。 |
Use | 此路由项被路由软件查找的次数。 |
Iface | 当前路由会使用哪个网卡来发送数据。 |
1. Flags含义:
U (route is up): 路由是活动的。
H (target is a host): 目标是一个主机而非网络。
G (use gateway):需要经过网关。
R (reinstate route for dynamic routing): 使用动态路由时,对动态路由进行复位设置的标志。
D (dynamically installed by daemon or redirect): 由后台程序或转发程序动态安装的路由。
M (modified from routing daemon or redirect): 由后台程序或转发程序修改的路由。
A (installed by addrconf): 由addrconf安装的路由。
C (cache entry): 缓存的路由信息。
! (reject route): 这个路由将不会被接受(主要用来抵御不安全的网络)。
2. 细节详细
1. 0.0.0.0表示所有地址,例如默认路由0.0.0.0 192.168.79.2 0.0.0.0表示所有地址通过192.168.79.2转发;
4. 255.255.255.255是受限广播地址,但在实际使用中,会看到用255.255.255.255作为子网掩码,此时它表示单一主机IP地址;
1.2 网络/主机地址计算
网络地址: IP & 子网掩码
主机地址:IP & (~子网掩码)
2. 命令使用
2.1 添加主机路由
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
# route add -host 8.8.8.8 dev eth_x
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 eth_x
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=111 time=2173.787 ms
64 bytes from 8.8.8.8: seq=1 ttl=111 time=1253.267 ms
64 bytes from 8.8.8.8: seq=2 ttl=111 time=332.242 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 332.242/1253.098/2173.787 ms
2.2 添加网络路由
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
# ping 36.155.132.3 -c 3
PING 36.155.132.3 (36.155.132.3): 56 data bytes
ping: sendto: Network unreachable
# route add -net 36.155.132.0 netmask 255.255.255.0 dev eth_x
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
36.155.132.0 0.0.0.0 255.255.255.0 U 0 0 0 eth_x
# ping 36.155.132.3 -c 3
PING 36.155.132.3 (36.155.132.3): 56 data bytes
64 bytes from 36.155.132.3: seq=0 ttl=49 time=4999.354 ms
64 bytes from 36.155.132.3: seq=1 ttl=49 time=4110.518 ms
64 bytes from 36.155.132.3: seq=2 ttl=49 time=3176.215 ms
--- 36.155.132.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 3176.215/4095.362/4999.354 ms
2.3 添加默认路由
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: Network unreachable
# route add default dev eth_x
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth_x
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=111 time=903.207 ms
64 bytes from 8.8.8.8: seq=1 ttl=111 time=343.256 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 2 packets received, 33% packet loss
round-trip min/avg/max = 343.256/623.231/903.207 ms
2.4 删除主机路由
route del -host 8.8.8.8 dev eth_x
2.5 删除网络路由
route del -net 36.155.132.0 netmask 255.255.255.0 dev eth_x
2.6 删除默认路由
route del default dev eth_x
2.7 添加屏蔽路由
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: Network unreachable
# route add -host 8.8.8.8 dev eth_x
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 eth_x
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=111 time=808.356 ms
64 bytes from 8.8.8.8: seq=1 ttl=111 time=232.418 ms
64 bytes from 8.8.8.8: seq=2 ttl=111 time=265.754 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 232.418/435.509/808.356 ms
# route add -host 8.8.8.8 dev eth_x reject
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
8.8.8.8 0.0.0.0 255.255.255.255 !H 0 0 0 *
8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 eth_x
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: Host is unreachable
2.8 删除屏蔽路由
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
8.8.8.8 0.0.0.0 255.255.255.255 !H 0 0 0 *
8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 eth_x
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: Host is unreachable
# route del -host 8.8.8.8 dev eth_x reject
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 eth_x
# ping 8.8.8.8 -c 3
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=111 time=1822.399 ms
64 bytes from 8.8.8.8: seq=1 ttl=111 time=821.905 ms
64 bytes from 8.8.8.8: seq=2 ttl=111 time=253.332 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 253.332/965.878/1822.399 ms
2.9 配置路由优先级
♥️Metric决定路由的优先级,Metric越小优先级越高,越大优先级越低。
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
# route add default dev br0 metric 0
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 br0
# route add default dev rmnet0 metric 100
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 br0
0.0.0.0 0.0.0.0 0.0.0.0 U 100 0 0 rmnet0
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.100.1 icmp_seq=1 Destination Host Unreachable
From 192.168.100.1 icmp_seq=2 Destination Host Unreachable
From 192.168.100.1 icmp_seq=3 Destination Host Unreachable
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 4007ms
♥️基于br0的路由优先级高,通过ping外网时,发现路由走了br0网卡。