33

Is there a way to show my current (client) DHCP lease?

Or even better - retrieve all options/infos sent with DHCPACK?

5
  • 5
    /var/lib/dhcp/dhclient.leases?
    – heemayl
    Commented May 20, 2016 at 22:49
  • @heemayl this file is empty in my case (Ubuntu 15.10), maybe because NetworkManager is acquiring dhcp lease (?)
    – madneon
    Commented May 20, 2016 at 23:03
  • ipconfig getpacket <ifname> or ipconfig getv6packet <ifname> on Mac OS X.
    – thrig
    Commented May 20, 2016 at 23:21
  • @heemayl, Nope. No such file or directory
    – Cerin
    Commented Nov 21, 2017 at 21:46
  • @heemayl was correct for the directory. On Debian Stretch I found infos in "/var/lib/dhcp/dhclient.eth0.leases"
    – Larsen
    Commented Nov 16, 2018 at 11:58

9 Answers 9

11

I'm not aware of a way to query this information on the client. If you are on the server you can see information on the client leases in /var/lib/dhcp/dhclient.leases. From the client the only way i know is:

sudo grep dhclient /var/log/syslog

Which should give you something like:

May 20 18:34:38 [machine_name] dhclient: DHCPREQUEST on eth0 to [DHCP_SERVER_IP] port 67
May 20 18:34:38 [machine_name] dhclient: DHCPACK from [DHCP_SERVER_IP]
May 20 18:34:38 [machine_name] dhclient: bound to [client_dhcp_ip] -- renewal in 1517 seconds.
11

With NetworkManager use nmcli to query the DHCP status for your active connection (assuming one) with

nmcli -f ipv4.method con show "`nmcli -t -f NAME connection `"

For full DHCP information use -f DHCP4. Adjust for ipv6 as necessary.

2
  • 5
    This prints the DHCP information for the currently active connection: nmcli -f DHCP4 con show "$(nmcli -g NAME con show --active)" Commented Sep 9, 2020 at 9:41
  • This is the only method that works on my computer so far.
    – Lei Zhao
    Commented Dec 18, 2023 at 16:57
8

depending on your distro, it should be located in /var/lib/dhcp under dhclient.{interface}.leases or /var/lib/dhclient.leases. you can also specify the path of your dhclient.leases file by passing -lf when starting dhclient.

1
  • @madeon it should be under /var/lib/dhcp/dhclient.{interface}.leases e.g /var/lib/dhcp/dhclient.eth0.leases
    – RDCortez
    Commented May 20, 2016 at 23:08
8

There is one other aspect to this answer to consider to show your DHCP lease time and other DHCP information in the offer. If using Linux systemd-networkd, none of the options above work as they relate to network manager (the Linux solutions at least)

There is a similar question with answer explaining this at How do I check DHCP lease time in systemd-networkd?

Quoting from that answer:

Depending on the OS; Enabling debug isn't always necessary.

systemd-networkd should store the lease info under /run/systemd/netif/leases/

i.e.

cat /run/systemd/netif/leases/2
5

I used dhcpdump when I was testing DHCP. It will dump both sides of the DHCP transaction. If you leave it running it will log the options passed.

You may want to restart your DHCP client to see the initial negotiation. The renewal request should contain all the running configuration.

5

Check Which Lease?!?!

If you thought this couldn't get anymore confused, you'd be wrong ;-)

I found this question unpicking my own DHCP issues on Ubuntu 22.04 Desktop edition, where I discovered that I actually had TWO leases!:

sudo cat /var/lib/dhcp/dhclient.leases

sudo cat /var/lib/NetworkManager/internal-d6b936ad-d73f-4898-a826-edbb61d6155a-eth0.lease

So which is the one being used...?

(2) DHCP Clients:

Unlike Ubuntu's LTS server flavour, their Desktop edition winds-in NetworkManager into the networking gears.

And NetworkManager has its' own DHCP client.

Determine Default DHCP Client

So, there's (2) DHCP clients in this case, but which of these are actually being used?

In Ubuntu Networking, there is a layer of abstraction called netplan which sets the default networking gears in the directive renderer.

A review of /etc/netplan/01-network-manager-all.yaml reveals that NetworkManager is controlling our networking, NOT systemd-networkd.

A review of NetworkManager's logging:

 journalctl -u NetworkManager.service | grep DHCP

reveals that NetworkManager is using its' own DHCP client:

NetworkManager[5117]: <info>  [1655970837.0088] dhcp-init: Using DHCP client 'internal'.

Thus, of our (2) leases, the one being used by the system is:

/var/lib/NetworkManager/internal-d6b936ad-d73f-4898-a826-edbb61d6155a-eth0.lease

Note the keyword in that log message is "using": even if a gazillion DHCP clients are shown in the logging as enabled, only the one specified as "using" is the active DHCP client.

Wow, this was a tangle; hope this saves others going down the same deep, dark rabbit hole...

2

On current Debian you can find the information in /var/lib/dhcp/dhclient.leases which looks like this:

lease {
  interface "eth0";
  fixed-address 10.224.129.116;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 3600;
  option routers 10.224.129.1;
  option dhcp-message-type 5;
  option dhcp-server-identifier 10.224.129.1;
  option domain-name-servers 10.224.129.1;
  option dhcp-renewal-time 1800;
  option dhcp-rebinding-time 3150;
  option broadcast-address 10.224.129.255;
  option host-name "tk-otc-client";
  option domain-name "dd.otc";
  renew 4 2020/12/03 09:56:25;
  rebind 4 2020/12/03 10:26:09;
  expire 4 2020/12/03 10:33:39;
}
2
  • Can you tell me what does the number 4 after renew mean ?
    – Old Markus
    Commented May 27, 2021 at 17:51
  • 1
    4 means Thursday (0-6 -> Sunday-Saturday) as explained in linux.die.net/man/5/dhclient.conf .
    – TNT
    Commented May 27, 2021 at 18:32
0

Know your Unix/Linux :-) There are more DHCP client programmes available.

One, fairly frequent option is to use dhclient. It is usually set to write full "PACK" answer decoded into a file. File is usually somewhere in /var.

Something like find /var -name '*leases*' can give you a hint. Mine is /var/db/dhclient.leases.em2 (OpenBSD).

Or give some detail about SW you are using.

0

I am using arch linux and dhclient.leases can be found inside /var/lib/dhclient/

so you can do cat /var/lib/dhclient/dhclient.leases to view leases.

You must log in to answer this question.

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