12

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.

4
  • If you ifconfig <your_interface> and look at the flags listed, you may be able to find "DHCP" - this may be a little more generic across distros
    – Kinnectus
    Commented Aug 12, 2015 at 8:35
  • 1
    @BigChris ifconfig interface doesn't display anything about the type of connection :/
    – Neetz
    Commented Aug 12, 2015 at 9:21
  • 1
    In that case there's no method universal across all linux distros. There is a syslog on openwrt though which will tell you this though. It's just not a file.
    – qasdfdsaq
    Commented Aug 12, 2015 at 10:26
  • I only did a quick look up and came across this resource: unix.com/unix-for-dummies-questions-and-answers/…
    – Kinnectus
    Commented Aug 12, 2015 at 10:39

6 Answers 6

6

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.

2
  • I get  “Error: ‘con’ command ‘show’ is not valid.” Commented Feb 20, 2020 at 21:50
  • 1
    Use -g instead of -f for batch-friendly.
    – DustWolf
    Commented Sep 17, 2021 at 23:06
6
ip a | grep dynamic

If output is nothing -> your IP address is statically assigned.

If output is a line with dynamic in it -> your IP address is dynamically/dhcp assigned.

If the IP address is assigned by dhcp your line should be something like this:

inet 192.168.1.5/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3

Tested on Debian and Rocky Linux (CentOS)...

2
4

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

3

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.

3

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.

2
  • 3
    What does it mean if the result is iface lo inet loopback?
    – Magick
    Commented Feb 22, 2018 at 1:42
  • cat: /etc/network/interfaces: No such file or directory ON Fedora 29
    – brainLoop
    Commented Nov 1, 2019 at 12:08
0

In my testing in 2023, this works in multiple versions of CentOS/Rocky/RedHat/SuSE and Ubuntu:

# These are inverse of each other:
ip route list default | grep dhcp
ip route list default | grep static

Note: ip command is provided by iproute or iproute2 package, depending on the distribution.

Static IP command output:

$ ip route list default
default via X.X.X.X dev ens160 proto static metric 100 

DHCP command output:

$ ip route list default
default via X.X.X.X dev ens192 proto dhcp src Y.Y.Y.Y metric 100

Bash Example

if ip route list default | grep dhcp > /dev/null ; then 
   Do Something on DHCP machines
fi

credit

Extra credit: Puppet Integration

Puppet fact example:

Facter.add("is_dhcp") do
  setcode do
    route = Puppet::Util::Execution.execute(['ip route list default'], :failonfail => false)
    result = route ? "#{route}".match?(/dhcp/) : false
  end
end
2
  • how did you test this on ubuntu? it outputs dynamic, not dhcp Commented Oct 17, 2023 at 12:27
  • @BЈовић can you clarify which part of my answer you are referring to? I checked that ip route list default includes dhcp on Ubuntu 20.04 and 22.04 (I currently only have LTS available to test)
    – akom
    Commented Oct 18, 2023 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .