Mac OSX Route Mapping: Use LAN for specific Server and Wifi for other

Mac OSX Route Mapping: Use LAN for specific Server and Wifi for other

Network Jul 14, 2023

When working with internal services through LAN, we have some problem that we cannot connect to the outside world which we normally connect by Wifi.

Scenario — Expect Result

When going to internal server e.g. the ip 10.200.10.20 use LAN

Otherwise go through Wi-fi

See the interface in my machine with ifconfig

en0 Wifi
en5 LAN

We will config this behavior with route command

Removing LAN default Gateway

Open System Preference > Network

Seeing the LAN tab.

  1. Fill the Red if it is required for your internal services.
  2. Remove the default gateway in Green

Adding gateway

sudo route -n add -host <target_host_ip> <gateway_ip>

// Example
sudo route -n add -host 10.200.10.20 10.175.30.40

Add interface

sudo route add -host 10.200.10.20 -interface en5

Seeing route

OSX version of route miss the command to see route.

It use netstat -nr instead.

netstat -nr
...10.200.10.20       10.175.30.40      UGHS           en5
...

Now when going to 10.200.10.20 it goes through correct gateway and correct interface (LAN en5 )

and for other i.e. default goes through Wifi (en0)

default            172.24.200.1       UGSc           en0

Bonus

For those with ip range, we can avoid tedious one by one adding with CIDR block range /16 , /24 etc.

sudo route -n add -net 172.16.0.0/16 192.168.1.49

Hope this help. Cheers !

Tags

TeamCMD

We are CODEMONDAY team and provide a variety of content about Business , technology, and Programming. Let's enjoy it with us.