Route command-line interface for macOS


Sometimes you Mac is connected with one Ethernet interface and one WiFi interface, you want to force some route to specific gateways with the macOS Command Line Interface.

Here is our setup:

  • My local network is: 192.168.1.0/24
  • We want to use the gateway: 192.168.1.252
  • My network target is: 172.16.1.10/24

Temporary route

You want add a static route temporarily for a specific ip range with the route command:

moore@my-mac ~ % sudo route -n add -net 172.16.1.10/24 192.168.1.252

You can verify the route you have added:

moore@my-mac ~ % netstat -rn | grep 172.16.1.
CLI route command for Mac OS
CLI route command for Mac OS

Permanent route

To add a static route permanently you will have to use networksetup.

First list your devices:

moore@my-mac ~ % networksetup -listnetworkserviceorder

An asterisk (*) denotes that a network service is disabled.
(1) Belkin USB-C LAN 2
(Hardware Port: Belkin USB-C LAN, Device: en11)

(2) Belkin USB-C LAN
(Hardware Port: Belkin USB-C LAN, Device: en5)

(3) Linux for Tegra
(Hardware Port: Linux for Tegra, Device: en10)

(4) USB Controls
(Hardware Port: USB Controls, Device: usbmodem904NTVJD14201)

(5) Somfy Set&Go io
(Hardware Port: Somfy Set&Go io, Device: usbmodem6104402002)

(6) HERO10 Black
(Hardware Port: HERO10 Black, Device: en15)

(7) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(8) iPhone USB
(Hardware Port: iPhone USB, Device: en16)

(9) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en7)

(10) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)

Add the route with the networksetup command:

moore@my-mac ~ % sudo networksetup -setadditionalroutes "Belkin USB-C LAN 2" 172.16.1.10 255.255.255.0 192.168.1.252

Verify the route you added:

moore@my-mac ~ % netstat -rn | grep 172.16.1.
CLI route command for Mac OS
CLI route command for Mac OS


A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

EN | FR | ES

Search