Skip to content

November 1, 2009


Awk scripts for reading and editing Ubuntu /etc/network/interfaces file (Part 2/2)

by Joe Kuan

ubuntulogoFor modifying the /etc/network/interfaces file, I use another script and it is slightly more complicated.

[ Modified 28/Nov/14 ] – Created a new github repository for this script

[ Modified 22/May/14 ] – Added feature to remove an interface definition

Edit the interfaces file

Here is the awk script, changeInterface.awk for modifying the interfaces file.

function writeStatic(addr, nw, nm, gw) {
    if (length(addr))
        print "    address ", addr
    if (length(nw))
        print "    network ", nw
    if (length(nm))
        print "    netmask ", nm
    if (length(gw))
        print "    gateway ", gw
function usage() {
    print "awk -f changeInterfaces.awk <interfaces file> device=<eth device> \n" \
        "       [address=<ip addr>] [gateway=<ip addr>] [netmask=<ip addr>]\n" \
        "       [network=<ip addr>] [mode=dhcp|static] [arg=debug]"
BEGIN { start = 0;
    if (ARGC < 3 || ARGC > 9) {
        exit 1;
    for (i = 2; i < ARGC; i++) {
        split(ARGV[i], pair, "=");
        if (pair[1] == "address")
            address = pair[2];
        else if (pair[1] == "gateway")
            gateway = pair[2];
        else if (pair[1] == "network")
            network = pair[2];
        else if (pair[1] == "netmask")
            netmask = pair[2];
        else if (pair[1] == "device")
            device = pair[2];
        else if (pair[1] == "arg" && pair[2] == "debug")
            debug = 1;
        else if (pair[1] == "mode" && pair[2] == "dhcp")
            dhcp = 1;
        else if (pair[1] == "mode" && pair[2] == "static")
            static = 1;
        else if (pair[1] == "mode" && pair[2] == "remove")
            remove = 1;
        else {
            exit 1;
    # Sort out the logic of argument
    if (dhcp && (length(network) || length(gateway) || length(address) || length(netmask))) {
        print "Both DHCP and static properties are defined";
        exit 1;
    if ($1 == "auto" && remove) {
        gsub(device, "");

    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static
    if ($1 == "iface")  {
        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {
            if (debug)
                print $0;
            # If remove is defined, switch on delete mode 
            if (remove) {

            # It's a DHCP interface, if defined any static properties
            # change it to static
            else if (match($0, / dhcp/)) {
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
            # It's a static network interface
            else if (match ($0, / static/)) {
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ static/, " dhcp");
                    print $0;
        # If it is other inteface line, switch it off
        else {
            definedStatic = 0;
            definedDhcp = 0;
            definedRemove = 0;
        if (!definedRemove) {
            print $0;
    # Reaches here - means non iface lines
    # Change the static content
    if (definedStatic) {
        # Already defined static, just changing the properties
        # Otherwise omit everything until the iface section is
        # finished
        if (!dhcp) {
            if (debug)
                print "static - ", $0, $1;
            if ($1 == "address" && length(address))
                print "    address ", address
            else if ($1 == "netmask" && length(netmask))
                print "    netmask ", netmask;
            else if ($1 == "gateway" && length(gateway))
                print "    gateway ", gateway;
            else if ($1 == "network" && length(network))
                print "    network ", network;
                print $0;
    # If already defined dhcp, then dump the network properties
    if (definedDhcp) {
        writeStatic(address, network, netmask, gateway);
        definedDhcp = 0;
    if (!definedRemove) {
        print $0;
    # This bit is useful at the condition when the last line is
    # iface dhcp
    if (definedDhcp)
        writeStatic(address, network, netmask, gateway);

Suppose we have the following interfaces file setup

auto lo
iface lo inet loopback

auto eth0 eth1 eth2 eth3

iface eth0 inet static

iface eth1 inet manual
    up ifconfig $IFACE up
    down ifconfig $IFACE down

iface eth2 inet static

iface eth3 inet dhcp

Script Usage
The script can

  1. change a static address properties from an interface defined as static
  2. convert an interface with static/manual properties to DHCP and vice versa
  3. remove an existing interface definition

Usage 1. Change network static properties

You can define partial or the full set of static properties.

awk -f changeInterface.awk /etc/network/interfaces device=<device> [address=<ip>]  [gateway=<ip>] [netmask=<ip>] [network=<ip>]

Here is a screenshot of an usage example

script usage 1

Usage 2A. Change static/manual interface to DHCP

You can use the script to change static/manual interface to DHCP, simple with mode=dhcp on the target device. The following is a screenshot of the usage

script usage 2A

Usage 2B. Change DHCP interface to static address

The script also works for changing a DHCP network interface into a permanent static address setting, simple specify the static properties.

script usage 2b

Usage 3. Remove an interface definition



Hopefully, some of you may find this script useful. If you find the script not working probably for your Ubuntu interfaces file, drop me a note with the interface file and I will do my best to fix the script. Thanks.

I work for iTrinegy and here are my other Ubuntu blogs

15 Comments Post a comment
  1. Vagelis Koutsomitros
    Dec 29 2009

    I was searching for partial examples with sed and awk, to start from..
    I couldn’t find anything..

    Your script is just awesome!
    Works great on debian!
    Splendid work!

  2. Joe Kuan
    Dec 29 2009

    Thanks. I am surprise that it also works on Debian.

  3. Vagelis Koutsomitros
    Dec 29 2009

    Don’t be! It is a very basic file.

    Its syntax though is very difficult to parse.

    I have parsed other conf files with basic knowledge of awk and sed. But parsing this one requires deeper knowledge.

    You saved me a lot of time!

  4. Sri
    Jan 2 2012

    The output of Usage 2A. Change static/manual interface to DHCP when redirected to file and then eth1 changed to stat with address, netmask and gateway mentioned does not display correctly, only the dhcp is changed to static.

    But when a new line is introduced after line iface eth1 inet dhcp then things start working properly

  5. May 28 2013

    looks very good … i made a few changes to also accept dns-nameservers and dns-search settings ;)

    Also noticed that to script it, the best way is probably to add the following bit to the end of the command to make the change to the actual file!
    “> /etc/network/interfaces.tmp && mv /etc/network/interfaces.tmp /etc/network/interfaces”

  6. Maksim
    May 22 2014

    Very nice script, but how to remove interface with this script?

    • Joe Kuan
      May 22 2014

      I have updated the script to support that feature. Check the blog.

      • Maksim
        May 23 2014

        Thanks for changes. Another question about ADD intreface to config file can it add new ip as alias for given interface? Or just add ip as primary?

      • Maksim
        May 27 2014

        Seems like script completely ignore ip if interface not exist in config file which mean impossible add any alias IP.
        What I mean if set device=eth0 mode=add address= create
        iface eth0:0
        And if put device=eth0 mode=add address= it does
        iface eth0:1
        Is this possible?

  7. Oct 28 2014

    Hi Joe, Have you put this on any public repo – like GitHub etc? its good to upload it so that its maintained and evolves.

    • Oct 29 2014

      [missed this] Also, put a liberal license (like BSD)

      • Joe Kuan
        Nov 1 2014

        Thank you for your interest. I will keep that in mind and put it on github once I have a moment.

  8. Jack
    Oct 31 2014

    Hello, your script is very useful, but what if one needs to setup dns servers too?

    • Joe Kuan
      Nov 1 2014

      Currently, it doesn’t do that. I don’t use that script for quite sometime, so I haven’t put much improvement to it. Will consider it when I have a moment.

      • Jun 3 2015

        Please do. Although I could just as well write something to edit resolve.conf which is much easier to parse.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: