Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

My laptop needs to use DHCP at home and a static IP at work. As far as I can tell, there's no way to flip between one and the other quickly - I'll have to manually configure the network connection every time I move the laptop. One alternative would be to create a small Bash script to edit /etc/network/interfaces, but I'd rather have some simpler way, like Network Proxy Preferences → Location.

Edit: Looks like this might be simple using /etc/network/interfaces itself.

share|improve this question

migrated from stackoverflow.com Sep 23 '11 at 7:51

This question came from our site for professional and enthusiast programmers.

3  
you can save multiple network manager connection profiles and switch them easily in the networkmanager applet. (this question is probably better suited on superuser....) –  Gryphius Sep 23 '11 at 7:29
    
@Gryphius: Which application is that? I can't find it in System -> Preferences. Network Connections doesn't seem to support anything of the kind. –  l0b0 Sep 23 '11 at 7:45
1  

1 Answer 1

up vote 3 down vote accepted

This can be easily done with NCD, the network configuration software I'm developing: http://code.google.com/p/badvpn/wiki/NCD

The following NCD program uses either static config or DHCP based on the value of a variable. To switch, you have to edit the NCD program and restart the NCD daemon. (but see below for automatic detection)

process lan {
    # Network device name.
    var("eth0") dev;

    # Use static config or DHCP? Change to "true" for static config.
    var("false") is_home;

    # Wait for device and link.
    net.backend.waitdevice(dev);
    net.up(dev);
    net.backend.waitlink(dev);

    # Get configuration.
    provide("lan_config");
    depend("lan_config_done") config;

    # Assign IP address.
    net.ipv4.addr(dev, config.addr, config.addr_prefix);

    # Add default route.
    net.ipv4.route("0.0.0.0", "0", config.gateway, "20", dev);

    # Configure DNS servers.
    net.dns(config.dns_servers, "20");
}

process lan_config_home {
    depend("lan_config") dep;
    if(dep.is_home);

    # Choose static configuration.
    var("192.168.1.72") addr;
    var("24") addr_prefix;
    var("192.168.1.1") gateway;
    list("192.168.1.1") dns_servers;

    provide("lan_config_done");
}

process lan_config_other {
    depend("lan_config") dep;
    ifnot(dep.is_home);

    # Do DHCP.
    net.ipv4.dhcp(dep.dev) dhcp;

    # Verify address.
    ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    # Alias configuration from DHCP.
    var(dhcp.addr) addr;
    var(dhcp.prefix) addr_prefix;
    var(dhcp.gateway) gateway;
    var(dhcp.dns_servers) dns_servers;

    provide("lan_config_done");
}

The selection can also be automated by checking the MAC address of the DHCP server. The following NCD program always performs DHCP, but then it checks the DHCP server's MAC address against the address of the home router. If they match, it ignores the DHCP-obtained address and assigns a static address, else it assigns the DHCP address. (note: you need the latest NCD from the svn repo for this, I've just implemented the dhcp.server_mac variable)

process lan {
    # Network device name.
    var("eth0") dev;

    # MAC address of home router (6 two-digit caps hexadecimal values separated with colons).
    var("00:AB:CD:EF:51:74") home_mac;

    # Wait for device and link.
    net.backend.waitdevice(dev);
    net.up(dev);
    net.backend.waitlink(dev);

    # Do DHCP.
    net.ipv4.dhcp(dev) dhcp;

    # Are we home?
    strcmp(dhcp.server_mac, home_mac) is_home;

    # Choose configuration.
    provide("lan_config");
    depend("lan_config_done") config;

    # Assign IP address.
    net.ipv4.addr(dev, config.addr, config.addr_prefix);

    # Add default route.
    net.ipv4.route("0.0.0.0", "0", config.gateway, "20", dev);

    # Configure DNS servers.
    net.dns(config.dns_servers, "20");
}

process lan_config_home {
    depend("lan_config") dep;
    if(dep.is_home);

    println("using config: static");

    # Choose static configuration.
    var("192.168.1.72") addr;
    var("24") addr_prefix;
    var("192.168.1.1") gateway;
    list("192.168.1.1") dns_servers;

    provide("lan_config_done");
}

process lan_config_other {
    depend("lan_config") dep;
    ifnot(dep.is_home);

    println("using config: dhcp");

    # Verify address.
    ip_in_network(dep.dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    # Alias configuration from DHCP.
    var(dep.dhcp.addr) addr;
    var(dep.dhcp.prefix) addr_prefix;
    var(dep.dhcp.gateway) gateway;
    var(dep.dhcp.dns_servers) dns_servers;

    provide("lan_config_done");
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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