4

I've been having a problem where my macbook won't reconnect to the internet after I turn off my work VPN. A simple reboot fixes this, but I was wondering if there was a way I could restart the network configs, or clear cache (not sure exactly what the problem is) from the command line - without having to do a reboot?

I've tried the following, without success

$ ifconfig en0 down && ifconfig en0 up

Is there a way to reset the network config to whatever the default values are after a reboot?

Edit: This doesn't happen every time, I'm not exactly sure what chain of events triggers it.

  • 1
    Is it a DNS or routing problem? Can you ping an IP (4.4.4.4 for example)? – Allan May 10 '18 at 5:18
  • I'm not able to ping an outside address. My suspicion was a DNS problem; however, the DNS addresses appeared to be unchanged after a reboot. I should also note that this problem doesn't happen every time - it's sporadic. – Ben May 10 '18 at 5:21
  • 1
    If you can't ping an IP address, it's not DNS. Try sudo networksetup -setnetworkserviceenabled Ethernet off; sleep 10; networksetup -setnetworkserviceenabled Ethernet on That will disable your Ethernet, wait 10 seconds, then turn it back on. – Allan May 10 '18 at 5:24
  • Okay, thanks. Next time it goes down I'll give it a try and report back! – Ben May 10 '18 at 6:09
  • It could be that the routes provided by the VPN connection aren't being cleared from the tables when you disconnect. I would suggest trying sudo networksetup -setadditionalroutes "Insert Name of Network Service here". The absence of any arguments following the service name clears all additional routes. P.S. I don't think macOS has given such low level access to interfaces that you might expect from using ifconfig, it is basically a wrapper left for compatibility at this point. Take a look at man scutil for real fine grained control. – user3052786 May 10 '18 at 8:29
1

Lack of network access after disconnecting from a VPN can be a symptom of a routing issue. If your VPN client doesn't disconnect cleanly, the gateway that directs your traffic to the virtual network can sometimes be left in place, effectively routing your traffic to a dead end.

I'd suggest bringing your interfaces down, flushing the routing table of gateway entries, and bringing them up again:

  1. Bring your network interfaces down:

    for i in $(ifconfig | egrep -o "^[a-z].+\d{1}:" | sed 's/://'); do sudo ifconfig "$i" down; done
    
  2. Flush the routing table: sudo route -n flush.

  3. Bring your interfaces back up again: (repeat step 1 with up instead of down).

If you want a reusable Bash function for this that you can drop into your .bashrc (or wherever), you could save the following:

resetroute () {
    echo "Flushing routes...";
    for i in $(ifconfig | egrep -o "^[a-z].+\d{1}:" | sed 's/://');
    do
        sudo ifconfig "$i" down;
    done;
    sudo route -n flush;
    for i in $(ifconfig | egrep -o "^[a-z].+\d{1}:" | sed 's/://');
    do
        sudo ifconfig "$i" up;
    done
}
  • Surprisingly, the error hasn't resurfaced. I will definitely try this when it does and accept it if it works. Until then, +1 for the suggestion – Ben May 24 '18 at 5:27

You must log in to answer this question.

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