Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to determine whether my IP has been statically or dynamically assigned in Ubuntu 11.04 from within C. The closest I've come is examining /var/lib/dhcp/dhclient.leases, but that doesn't change if a static IP is assigned (as it caches DHCP leases for future connection attempts).

So... is there a command line utility (for Ubuntu) that will allow me to check if DHCP is enabled or disabled?

Alternatively: the machine is a server dedicated to one application; would it be easier just to maintain an environment variable that keeps track of DHCP status?

share|improve this question

migrated from Jun 9 '11 at 16:38

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

I'm not sure you can... are you trying to check internal or external IPs? If you are looking at internal IPs, you can generally assume they're dynamic. If you are looking at external IPs, sometimes asking your ISP will get you an answer - subsequently, if you're on a residential ISP, assume its dynamic, as only small-business-class ISP plans would be static IPs. – Thomas W. Jun 9 '11 at 19:38

You can put scripts in /etc/network/if-up.d to be executed when an interface is activated (either by ifup or by Network Manager). These scripts receive information about the interface, including the environment variable $METHOD set to static or dhcp. This is documented in the interfaces(5) man page.

## This is /etc/network/if-up.d/zzzz_alex_notify_myserver
case $LOGICAL in
    case $METHOD in
      dhcp) <notify server that it's on DHCP>;;
      static) <notify server that it's on a static IP configuration>;;
share|improve this answer

Another way is to use NetworkManager tools, namely nm-tool and nmcli.

I writed the following script on this:


# get the connection id of the active connection    
get_con_id() {
  nm-tool |
    awk '
      $1 == "-" {
        dev = $3
        id = dev
        if (NF > 4 && match($0, "\\[(.*)\\]", a))
          id = a[1]
      /^ / && $1 == "State:" && $2 == "connected" {
        print id

# get the address type of the active connection
nmcli con list id "$(get_con_id)" |
  awk '
    $1 == "ipv4.method:" {
      if ($2 == "manual")
        print "static"
      else if ($2 == "auto")
        print "dynamic"
        print "unknown"

It probably break in non usual cases, e.g. where you have more than one active connection, but could be a starting point on which to elaborate.

Also, if you modify a connection, the script will see the new settings, even if the connection has not been restarted, so they are not in use yet.

share|improve this answer

I've written this command to get the DHCP configured interfaces in a shell script:

ps -A -o cmd | grep -E '(/| )dhclient .'

Then you can discard interfaces not managed by dhclient.

At the moment I don't know if it works for all GNU/Linux distributions.

share|improve this answer

I guess you're referring to the IP assignment of your ubuntu machine locally and not your external/public IP.

Perhaps the following output could be of some help:

cat /etc/network/interfaces |grep ^iface\ eth0 | awk -F ' ' '{print $4}'

This will print either the word static or dhcp, depending on the current configuration of interface eth0.

You can also make small adjustments, like:

cat /etc/network/interfaces |grep ^iface\ | awk -F ' ' '{print $2, $4}'

in order to list all of the interfaces and their configuration.

share|improve this answer
This do not work with NetworkManager, the default in Ubuntu. – enzotib Aug 6 '11 at 18:37
What do you mean by 'not working with NetworkManager'? It's output is not updated according to the running config? The output is surely working ok... – Pavlos G. Aug 6 '11 at 21:31
When using NetworkManager, /etc/network/interfaces do not contain any information on eth0 or wlan0, or similar – enzotib Aug 6 '11 at 21:33

Its much easier to determine that:

Just disconnect the machine from network. If you can see the ip address in the ifconfig output for that interface then it is statically assigned otherwise dynamically.

regards delaflota

share|improve this answer

Your Answer


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.