Running Ubuntu Server 12.04 (no GUI). What would be the best way to make kernel bring a network interface up only if it is physically plugged in? So, if it doesn't exist, just move on with initializing other interfaces (if any) and continue to the login screen, without "waiting for network configuration" delay.

E.g, I have a wireless USB key wlan9 (in /etc/network/interfaces):

auto wlan9
iface wlan9 inet dhcp
        wpa-ssid myssd
        wpa-psk mykey
        wpa-proto RSN
        wpa-pairwise CCMP
        wpa-group CCMP

I tried allow-hotplug instead of auto, in which case the interface doesn't get initialized automatically during the boot, and I have to do it manually with ifup wlan9. This is not exactly what I'm looking for.

Thank you.


Also for server: use NetworkManager

  1. Install it:

    sudo apt-get install network-manager

    Unfortunately, this will pull in a lot of dependencies.

  2. Bring down the interface currently configured the traditional way.

    ifdown wlan9
  3. Disable any manual settings in /etc/network/interfaces by removing all lines concerning that interface.

  4. Add a connection setting for your wireless connection in /etc/NetworkManager/system-connections/ (make up a name):

    id=Some name of my connection
    • For uuid use the command uuidgen to generate a random one.
    • For mac-address use the MAC address of your wireless adapter (use ifconfig -a wlan9 to find out). This binds this configuration file to only this adapter - if it's not present it would not use it for another adapter, nor would it wait for the adapter to be present.
    • Other fields speak for themselves I hope. :)
  5. Restart NetworkManager or reboot.

    restart network-manager
  • Thank you, the Network Manager option feels a bit heavy, but I'll turn to it if don't find something lighter. ifplugd almosted worked, wlan9 goes up but it doesn't obtain a DHCP lease. – noseratio Jul 13 '13 at 17:20
  • @Noseratio Yes, I agree this is not really a lightweight solution. If I knew a more lightweight network manager without the GUI dependencies I would have used that in my answer. :) – gertvdijk Jul 13 '13 at 17:21
  • @gertvdijk Can you tell me what happens if I remove mac-address line? Will it work for all adapters or will it crash? – DangeMask Sep 11 '15 at 7:36
  • @DangeMask It should just work for all adapters I assume. Just try and find out? – gertvdijk Sep 11 '15 at 9:00
  • @gertvdijk I've tried just now. If I delete the "device MAC address" (deleted in GUI, disappears in file too), Network manager will not connect automatically even after one manual connect and reboot. I tried even setting ifupdown>managed to true and false without success. – DangeMask Sep 11 '15 at 9:06

and continue to the login screen

Suggests that you're on a GUI-enabled installation (non-server) - so I'm providing this answer based on that.

Just use NetworkManager

  1. Bring down the interface.

    ifdown wlan9
  2. Disable any manual settings in /etc/network/interfaces by removing all lines concerning that interface.

  3. Go to network settings, and add/edit the wireless network connection.

  4. Configure it as follows (see the screenshot below):

    1. Have it connect automatically.

    2. Apply it to the right wireless adapter by selecting the right MAC address. If the adapter isn't present, then these connection settings in this dialogue will not apply, so this "binds" them to this specific adapter (because every adapter has another MAC address). And no, this is not the MAC address of the WiFi AP - that's BSSID.

    3. Make it available to all users (this makes it automatically connect at login screen already). Note that this settings is greyed out until you provided the necessary details.

    And of course also enter the other details about security, SSID, etc.

    enter image description here

  • Sorry, I forgot to specify this is Ubuntu Server. Thank you anyway, I saved this hint for my desktop needs. – noseratio Jul 13 '13 at 15:55
  • 1
    @Noseratio Ah... too bad. Now writing an answer for server installation. Please be more specific and complete in your question next time. – gertvdijk Jul 13 '13 at 16:01
  • I will. Please forgive a noob :) – noseratio Jul 13 '13 at 16:04

I've come up with the following solution which doesn't depend on any extra packages:

auto wlan9
iface wlan9 inet manual
        wpa-ssid MYSSD
        wpa-psk MYKEY
        wpa-proto RSN
        wpa-pairwise CCMP
        wpa-group CCMP
        wireless-power off
        pre-up if [ -f /sys/class/net/wlan9/operstate ]; then ifconfig wlan9 up; fi
        up if [ -f /sys/class/net/wlan9/operstate ]; then dhclient wlan9; fi

While it may not be elegant, it does what I want: skips wlan9 initialization without boot delay if the wireless dongle is not plugged in, otherwise brings it up.

  • Nice. But how does this skip the initialization? You don't cancel anything in the pre-up directive. – gertvdijk Jul 14 '13 at 9:31
  • 1
    It think this is a product of two factors: 1) inet manual - so it really relies upon pre-up action to do anything: if there's no pre-up or up, kernel just skips this stanza – noseratio Jul 14 '13 at 11:34
  • Yeah, could be, but according to the manpage all other settings including wpa-* directives don't apply to the manual method. Looks like a sort of undocumented behaviour. – gertvdijk Jul 14 '13 at 11:38
  • 1
    I messed up with the comment, here's what I meant to say: It think this is a product of two factors: 1) inet manual - so it really relies upon pre-up action to do anything: if there's no pre-up or up, kernel would just skips this stanza 2) If the interfaces doesn't exist, if [ -f /sys/class/net/wlan9/operstate ] would return 1 and the pre-up would fail, so no further initialization or delay. – noseratio Jul 14 '13 at 11:44
  • The pre-up behavior is actually documented, but it may change in the the future, manpages.ubuntu.com/manpages/precise/man5/interfaces.5. pre-up Run command before bringing the interface up. If this command fails then ifup aborts, refraining from marking the interface as configured, prints an error message, and exits with status 0. This behavior may change in the future. – noseratio Jul 14 '13 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.