Determine if it's a static IP or a DHCP IP

I'm trying to find a genric solution across all linux distro to find if the IP address attached to the system is a static or a DHCP ?

On ubuntu , I can find if it's static or DHCP by doing a DHCP grep on /var/log/syslog but it is not generic solution , it might differ on other platforms.

One of the target board is Cortina and I'm using open wrt as a boot up kernel for that. There is no var/log/syslog on Cortina nothing similar to that also.


Solution 1:

If its CentOS, you can check /etc/sysconfig/network-scripts/ifcfg-eth0. Check BOOTPROTO entry says. If its dhcp then its DHCP configured. If its Static or none, then its not DHCP

Solution 2:

The problem is, if you're using NetworkManager, for example, it's going to be requesting an IP and gateway and DNS server. But beyond that, once it's got the information it needs, it sets addressing information essentially statically. Essentially, the rest of your machine doesn't know or care if an address is static or dynamic, just that it has an address.

You can check /var/log/syslog for DHCPACK entries specifically. I believe dhclient and NetworkManager write there.

Solution 3:

You have the command in nmcli.
This should work in all Linux flavors, I believe:

nmcli -f ipv4.method con show eno16780032

If the output is auto, then it is DHCP.
If the output is manual, then it is static.

Solution 4:

Type in terminal

cat /etc/network/interfaces

You should find one of this lines

iface eth0 inet dhcp

that means that IP for interface eth0 is from DHCP

iface eth0 inet static

Above line shows that IP is static. You should also find other parameters.