My goal is to be able to change all of the following for a specific device: IP address, netmask, and gateway. So far, I've gathered that I should be able to use ifconfig con0 netmask, but when I try to do ifconfig con0 gateway, it gets pretty upset and tells me that gateway is a bad argument.



The command you're looking for is route.

Check what your current gateway is

To check your current gateway, run the command:

route -n get -ifscope en0 default

This instructs route to return the path it would take using en0 to reach www.google.com.

  • The -n argument turns off domain name resolution in the output (else gateway in the sample output below would be the name of my gateway).

  • The -ifscope argument limits the query to using en0. Without it, to OS will select which interface to use.

  • default tells route that you want to see the default gateway. You can put a URL here instead if you suspect your OS is choosing something other than the default gateway to access a host.

The output will be something along the lines of:

   route to: default
destination: default
       mask: default
  interface: en0
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 

Delete the current gateway

To delete the current gateway from en0, run:

sudo route delete default -ifscope en0

This will delete the default gateway from en0 while leaving any other defined routes intact.

Add the new gateway

To add the new gateway to en0, run:

sudo route add default -ifscope en0

Replace with the IP address of the actual gateway.

For way more gory details, check out the man page for route.

  • So I tried this and it didn't quite work. In the case of trying to do this, my command was # route delete default -ifscope tun0 followed by # route add default -ifscope tun0, the output of both being route: writing to routing socket: Network is unreachable and add net default: gateway x.x.x.1: Network is unreachable in that order. – iamed18 Feb 14 '13 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .