How to bring up a network interface only if it is physically present

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.


Solution 1:

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):

    [connection]
    id=Some name of my connection
    uuid=0d791425-87c5-45e6-948e-01b1863901f7
    type=802-11-wireless
    
    [802-11-wireless]
    ssid=mySSID
    mode=infrastructure
    mac-address=24:77:00:01:02:03
    security=802-11-wireless-security
    
    [802-11-wireless-security]
    key-mgmt=wpa-psk
    psk=mypassword
    
    [ipv4]
    method=auto
    
    [ipv6]
    method=ignore
    
    • 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
    

Solution 2:

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