在 macOS 中修改路由通常使用route
命令或者 networksetup
工具。以下是一些常见的路由操作
查看当前路由表
使用
netstat -nr
命令可以查看当前的路由表。1
netstat -nr
使用
route get
命令查看特定目标的路由1
2
3
4
5
6
7
8
9
10% route get 59.232/16
route to: 59.232.0.0
destination: 59.232.0.0
mask: 255.255.0.0
gateway: 192.168.2.1
interface: en4
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
0 0 0 0 0 0 1500 0使用
networksetup -listallnetworkservices
查看当前的网络服务名称1
2
3
4
5
6% networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
USB 10/100/1000 LAN
Wi-Fi
Thunderbolt Bridge
设置路由
为指定的网络服务添加静态路由
1
2
3
4sudo networksetup -setadditionalroutes <网络服务> <目标网络> <子网掩码> <网关>
# 示例
sudo networksetup -setadditionalroutes "USB 10/100/1000 LAN" 59.232.0.0 255.255.0.0 192.168.123.1再次查看路由信息
1
2
3
4
5
6
7
8
9% route get 59.232/16
route to: 59.232.0.0
destination: 59.232.0.0
mask: 255.255.0.0
gateway: 192.168.123.1
interface: en4
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
0 0 0 0 0 0 1500 0