简体   繁体   English

我们可以通过命令行更改mac中的默认网关和接口吗

[英]can we change default gateway and interface in mac through commandline

We can see default gateway and interface of MAC by using following command我们可以使用以下命令查看 MAC 的默认网关和接口

route -n get default

I know that this is possible in linux and can be achieved by following commands我知道这在 linux 中是可能的,可以通过以下命令实现

route change default -interface $INTF route change 192.168.0.0/16 -interface $INTF But these are not working in MAC. route change default -interface $INTF route change 192.168.0.0/16 -interface $INTF但这些在 MAC 中不起作用。 My objective is to change the Default Gateway and interface.我的目标是更改默认网关和界面。

The networksetup utility should be able to do what you want. networksetup实用程序应该可以执行您想要的操作。

For example, to manually set up the standard Ethernet interface with an IP of 192.168.100.100 , subnet of 255.255.255.0 and gateway of 192.168.100.1 : 例如,要手动设置IP地址为192.168.100.100 ,子网为255.255.255.0和网关为192.168.100.1的标准以太网接口:

networksetup -setmanual "Ethernet" 192.168.100.100 255.255.255.0 192.168.100.1

You might also be interested in the -setadditionalroutes flag. 您可能也对-setadditionalroutes标志感兴趣。

I recently had an instance where someone setup dual default routes我最近有一个实例,有人设置了双默认路由

grant@dd08-mac:~[20220909-15:59][#8625]% netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags           Netif Expire
default            10.17.124.1        UGScg             en0       
default            10.1.78.1          UGScIg        bridge0       
10.1.78/24         link#16            UCS           bridge0      !
10.1.78.1/32       link#16            UCS           bridge0      !
10.1.78.1          link#16            UHLWIir       bridge0      !
10.1.78.24         link#16            UHLWI         bridge0      !
10.1.78.58/32      link#16            UCS           bridge0      !
10.1.78.58         36.6a.e9.47.a5.80  UHLWIi            lo0       
10.1.78.255        ff.ff.ff.ff.ff.ff  UHLWbI        bridge0      !
-snip-

I reset the interface with networksetup - this deleted the extra route我用networksetup重置了接口——这删除了额外的路由

grant@dd08-mac:~[20220909-16:04][#8885]% sudo networksetup -setmanual Ethernet 10.17.124.78 255.255.255.0 0.0.0.0
Password:
grant@dd08-mac:~[20220909-16:11][#8924]

and the extra route is gone额外的路线消失了

grant@dd08-mac:~[20220909-16:11][#8964]% netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags           Netif Expire
default            10.1.78.1          UGScg         bridge0       
10.1.78/24         link#16            UCS           bridge0      !
10.1.78.1/32       link#16            UCS           bridge0      !
10.1.78.1          link#16            UHRLWIir      bridge0     19
10.1.78.58/32      link#16            UCS           bridge0      !
10.1.78.58         36.6a.e9.47.a5.80  UHLWIi            lo0       
10.17.124/24       link#9             UCS               en0      !
10.17.124.5        0:25:90:55:6d:e3   UHLWIi            en0   1191
10.17.124.78/32    link#9             UCS               en0      !
-snip-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM