[macos] How to get default gateway in Mac OSX

I need to retrieve the default gateway on a Mac machine. I know that in Linux route -n will give an output from which I can easily retrieve this information. However this is not working in Mac OSX(Snow Leopard).

I also tried netstat -nr | grep 'default', but I was hoping for a cleaner output like that produced by route -n in Linux/Unix. netstat -nr lists all the interfaces and the default gateway for them.

Any kind of suggestion or a hint in the right direction will be appreciated.

This question is related to macos routes netstat ifconfig

The answer is


I would use something along these lines...

 netstat -rn | grep "default" | awk '{print $2}'

For getting the list of ip addresses associated, you can use netstat command

netstat -rn 

This gives a long list of ip addresses and it is not easy to find the required field. The sample result is as following:

Routing tables
Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.195.1      UGSc           17        0     en2
127                127.0.0.1          UCS             0        0     lo0
127.0.0.1          127.0.0.1          UH              1   254107     lo0
169.254            link#7             UCS             0        0     en2
192.168.195        link#7             UCS             3        0     en2
192.168.195.1      0:27:22:67:35:ee   UHLWIi         22      397     en2   1193
192.168.195.5      127.0.0.1          UHS             0        0     lo0

More result is truncated.......

The ip address of gateway is in the first line; one with default at its first column.

To display only the selected lines of result, we can use grep command along with netstat

netstat -rn | grep 'default'

This command filters and displays those lines of result having default. In this case, you can see result like following:

default            192.168.195.1      UGSc           14        0     en2

If you are interested in finding only the ip address of gateway and nothing else you can further filter the result using awk. The awk command matches pattern in the input result and displays the output. This can be useful when you are using your result directly in some program or batch job.

netstat -rn | grep 'default' | awk '{print $2}'

The awk command tells to match and print the second column of the result in the text. The final result thus looks like this:

192.168.195.1

In this case, netstat displays all result, grep only selects the line with 'default' in it, and awk further matches the pattern to display the second column in the text.

You can similarly use route -n get default command to get the required result. The full command is

route -n get default | grep 'gateway' | awk '{print $2}'

These commands work well in linux as well as unix systems and MAC OS.


The grep utility is not needed. Awk can do it all:

    netstat -rn | awk '/default/ {print $2}'
      192.168.128.1

Note that if you have something like Parallels (or a VPN, or both) running, you may see two or more default routing entries - it will be true if you use the 'grep' suggestion above, too.

    netstat -rn | awk '/default/ {print $2}'
      192.168.128.1
      link#12

and

    netstat -rn | awk '/default/ {print $2}'                             
      utun1
      192.168.128.1
      link#12

To set a variable (_default) for further use (assuming only one entry for 'default') .....

    _default=$( netstat -rn inet | awk '/default/ {print $2}' ) # I prefer $( ... ) over back-ticks

In the case of multiple default routes use:

    netstat -rn | awk '/default/ {if ( index($6, "en") > 0 ){print $2} }'
      192.168.128.1

These examples tested in Mavericks Terminal.app and are specific to OSX only. For example, other *nix versions frequently use 'eth' for ethernet/wireless connections, not 'en'. This is also only tested with ksh. Other shells may need a slightly different syntax.


Using System Preferences:

Step 1: Click the Apple icon (at the top left of the screen) and select System Preferences.

Step 2: Click Network.

Step 3: Select your network connection and then click Advanced.

Step 4: Select the TCP/IP tab and find your gateway IP address listed next to Router.


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to routes

How to open a link in new tab using angular? How to Refresh a Component in Angular laravel Unable to prepare route ... for serialization. Uses Closure How to use router.navigateByUrl and router.navigate in Angular No provider for Router? Difference between [routerLink] and routerLink How to force Laravel Project to use HTTPS for all routes? Change route params without reloading in Angular 2 Angular 2 router no base href set How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template?

Examples related to netstat

How to check if a network port is open on linux? How do I find which application is using up my port? How to get default gateway in Mac OSX Who is listening on a given TCP port on Mac OS X? Port 80 is being used by SYSTEM (PID 4), what is that?

Examples related to ifconfig

How to install "ifconfig" command in my ubuntu docker image? How do I delete virtual interface in Linux? How to get the primary IP address of the local machine on Linux and OS X? How to get default gateway in Mac OSX