Routing Operation on macOS

Overview

Routing on macOS allows you to control how network traffic is directed. You can view the routing table, add or delete routes, and manage network interfaces. Note that routes added via the route command are temporary and do not persist across reboots.

List Current Routing Table

netstat -nr

This displays the routing table, showing destinations, gateways, flags, and interfaces.

List Current IP Addresses

ifconfig

This shows network interface configurations, including IP addresses. On newer macOS versions, ifconfig is deprecated; consider using ipconfig or networksetup instead.

Add a New Route

sudo route add -net 11.11.11.11/32 192.168.0.1
  • 11.11.11.11/32: The destination network (host route in this case).
  • 192.168.0.1: The gateway IP address, which must be reachable on your network.

Note: The gateway must be on a directly connected network. Use sudo for administrative privileges.

Delete a Route

sudo route delete -net 11.11.11.11/32 192.168.0.1

This removes the specified route from the routing table.

Making Routes Persistent

Routes added with route are not saved after reboot. To make them persistent:

  1. Use networksetup to configure routes for specific interfaces:

    sudo networksetup -setadditionalroutes "Wi-Fi" 11.11.11.11 255.255.255.255 192.168.0.1
    

    Replace “Wi-Fi” with your interface name (e.g., “Ethernet”).

  2. Alternatively, create a launch daemon in /Library/LaunchDaemons/ to run a script that adds routes on boot.

Troubleshooting

  • Verify Route Addition: After adding, run netstat -nr to confirm.
  • Test Connectivity: Use ping 11.11.11.11 to test if the route works.
  • Check Gateway Reachability: Ensure the gateway is accessible: ping 192.168.0.1.
  • Interface Names: Use networksetup -listallhardwareports to find interface names.
  • Flush Routes: To clear all routes: sudo route flush (use cautiously).

Additional Commands

  • Show Network Interfaces: networksetup -listallhardwareports
  • Get Interface Details: networksetup -getinfo "Wi-Fi"
  • Set Manual IP: sudo networksetup -setmanual "Wi-Fi" 192.168.1.100 255.255.255.0 192.168.1.1

References