1

My goal is to terminate all ethernet traffic through en0 until I bring it back up with ifconfig en0 up.

When I run ifconfig en0 down through terminal, and then confirm ifconfig and all interfaces are inactive.

However, the next day, I find that en0 is back up automatically. How can I find out what brought it up, and is there a better way to ensure it stays down?

  • Out of curiosity, does sudo networksetup -setnetworkserviceenabled Ethernet off behave differently? – MacManager Oct 13 '16 at 18:15
  • Thank you @MacManager, I had not tried 'sudo networksetup -setnetworkserviceenabled Ethernet off'. I will tonight. I hope it works! – Harry Potter Oct 13 '16 at 18:39
  • @klanomath, Yes, all are inactive. Its strange, because you would think 'sudo ifconfig en0 down' would stay down. I always check ifconfig after and confirm it went to inactive. But, I find the next morning something brought en0 back up. – Harry Potter Oct 13 '16 at 18:41
  • Sure thing. To get the name of the network service I used networksetup -listnetworkserviceorder and looked for the en0 entry. – MacManager Oct 13 '16 at 18:42
  • @HarryPotter I meant: if you just disable en0 with ifconfig, en1/en2 shouldn't be down/inactive if they have been active previously. – klanomath Oct 13 '16 at 18:44
1

Use the networksetup command instead. On my system en0 is Wi-Fi, so I use networksetup -setairportpower en0 off. I actually use a little AppleScript that sits in my Dock and toggles the state. See the networksetup manual page for additional information using networksetup.

Save the AppleScript code as an application named Toggle Wi-Fi and then you can run it as needed/wanted. If you run it and Wi-Fi is on, it turns it off, and if it's off, it turns it on.

on run
    set interface to "en0"
    set status to do shell script "ifconfig " & interface & " | awk '/status:/{print $2}'"

    if status is "inactive" then
        do shell script "networksetup -setairportpower " & interface & "  on"
        display dialog "The Wi-Fi Network Adapter is turned: ON" with title "Wi-Fi Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    else
        do shell script "networksetup -setairportpower " & interface & "  off"
        display dialog "The Wi-Fi Network Adapter is turned: OFF" with title "Wi-Fi Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    end if
end run

Updated to address Ethernet Adapter usage:

on run
    set interfaceNumber to "en0"
    set interfaceName to "Display Ethernet"
    set status to do shell script "ifconfig " & interfaceNumber & " | awk '/status:/{print $2}'"

    if status is "inactive" then
        do shell script "networksetup -setnetworkserviceenabled " & quoted form of interfaceName & " on with administrator privileges"
        display dialog "Network Adapter " & interfaceNumber & " is turned: ON" with title interfaceName & " Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    else
        do shell script "networksetup -setnetworkserviceenabled " & quoted form of interfaceName & " off with administrator privileges"
        display dialog "Network Adapter " & interfaceNumber & " is turned: OFF" with title interfaceName & " Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    end if
end run

Note: In the code above the name of my Ethernet Adapter is "Display Ethernet" and you'll need to change it to whatever the name of your Ethernet Adapter is. You can ascertain it in System Preferences > Network.

  • Interesting, on my system en0 and en1 are wired interfaces. en2 is wireless. I actually turn off wireless through network settings itself. Your implementation of toggling, is what I am looking to do for the wired interface. Could I modify your apple script to do that? – Harry Potter Oct 13 '16 at 17:35
  • @Harry Potter, I've updated my answer with an example using an Ethernet Adapter. – user3439894 Oct 13 '16 at 18:45
  • thanks for that! It seems very clever and it looks like it serves to what I want it to do. But, I am curious though, why go to this much effort - is it because the terminal commands are not persistent? That is what is perplexing me. – Harry Potter Oct 13 '16 at 18:59
  • @Harry Potter, The original AppleScript code works with Wi-Fi without administrator privileges and I made it into an app because I toggle the state of the Wi-Fi Network Adapter daily and sometimes several times daily. Yes I could do it in Terminal or use Preferences > Network however it's always about what makes repetitive actions the easiest. Had your en0 been Wi-Fi vs. Ethernet this would have just been as simple as using what I originally supplied. For Ethernet it's a bit different. That said, use Terminal if you prefer! :) They'll both do the same thing. – user3439894 Oct 13 '16 at 19:10
  • thank you! I will be doing this repetitively also. Call me paranoid, but if I am away from my machines I have always disabled all internet access. With your script you could have one toggle for wireless and another for wired. I will probably implement both. Thanks for sharing, most appreciated. – Harry Potter Oct 13 '16 at 19:16
0

@user3439894 has answered the part of the question about how to disable Ethernet and have it stay down; I'd like to address the question of why ifconfig en0 down doesn't do it.

The basic reason is that macOS has two different levels of network configuration: the live state (which ifconfig en0 down changes) and the "configuration database" which corresponds to the settings created in the Network pane of System Preferences and/or the networksetup command. And there's a "configuration daemon" (configd) whose job it is to adjust the live network state to match what the database says it should be.

The reason it works this way is that the config database describes the network settings in a more flexible and coherent way than the live state does. For example, the config database can have multiple groups of settings ("locations") and easily switch between them, adjusting everything -- interface state, IP settings, routing, DNS, etc -- in a single operation.

Anyway, when you use ifconfig en0 down, you've created an inconsistency between the live state and what the config database says the state should be. configd won't notice this immediately, because it isn't the sort of event configd looks for (ethernet being plugged in or unplugged, WiFi network being joined or dropped, etc). But sometime later, something will wake configd up and it may (or may not) reset the interface state back to what the config database says it should be.

So, the solution is to use Network Preferences or networksetup. These both make changes to the config database, then wake up configd so it'll apply the changes to the live state.

You must log in to answer this question.

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