Press "Enter" to skip to content

mac系统配置静态路由表

背景:

我有一台实验机器,链接了多个网络(有线、无线),我可能需要不一样网段走不同的网络出口。需要进行一个静态的路由规则配置。

操作:

查看路由表

netstat -rn

新增静态路由:

sudo route -n add -net 10.0.0.0/8 10.242.100.1

说明:

我们会发现我们查看路由表命令会有一堆的路由,那隔了很久之后我们还记得哪个是静态路由哪个是动态路由吗?

路由表形如:

➜  ~ netstat -nr
Routing tables

Internet:
Destination        Gateway            Flags        Netif Expire
default            link#14            UCSg          ppp0
default            10.12.16.1        UGScIg         en0
default            10.24.24.1       UGScIg         en1
1.11.71.20       link#14            UHW3Ig        ppp0    121
10.12.16/23       link#4             UCS            en0      !
10.12.16.1/32     link#4             UCS            en0      !

你会看到一个Flags的字段:

Flags字段的解释为:

 1       RTF_PROTO1       Protocol specific routing flag #1
 2       RTF_PROTO2       Protocol specific routing flag #2
 3       RTF_PROTO3       Protocol specific routing flag #3
 B       RTF_BLACKHOLE    Just discard packets (during updates)
 b       RTF_BROADCAST    The route represents a broadcast address
 C       RTF_CLONING      Generate new routes on use
 c       RTF_PRCLONING    Protocol-specified generate new routes on use
 D       RTF_DYNAMIC      Created dynamically (by redirect)
 G       RTF_GATEWAY      Destination requires forwarding by intermediary
 H       RTF_HOST         Host entry (net otherwise)
 I       RTF_IFSCOPE      Route is associated with an interface scope
 i       RTF_IFREF        Route is holding a reference to the interface
 L       RTF_LLINFO       Valid protocol to link address translation
 M       RTF_MODIFIED     Modified dynamically (by redirect)
 m       RTF_MULTICAST    The route represents a multicast address
 R       RTF_REJECT       Host or net unreachable
 r       RTF_ROUTER       Host is a default router
 S       RTF_STATIC       Manually added
 U       RTF_UP           Route usable
 W       RTF_WASCLONED    Route was generated as a result of cloning
 X       RTF_XRESOLVE     External daemon translates proto to link address
 Y       RTF_PROXY        Proxying; cloned routes will not be scoped

比如我看到我们刚才加入的静态路由规则应该是:

Internet:
Destination        Gateway            Flags        Netif Expire
10                 10.242.100.1       UGSc           en1

其中的 USGc的S 表示是手动加上去的。

参考资料:

https://apple.stackexchange.com/questions/336888/whats-the-meanings-of-the-routing-tables-data-in-the-mac-os

https://www.analysisman.com/2020/11/macos-staticroutes.html

https://support.justaddpower.com/kb/article/320-adding-a-static-route-to-macos/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注