Is there a way to show my current (client) DHCP lease?
Or even better - retrieve all options/infos sent with DHCPACK
?
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.
nmcli -f DHCP4 con show "$(nmcli -g NAME con show --active)"
Commented
Sep 9, 2020 at 9:41
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.
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.
/var/lib/dhcp/dhclient.{interface}.leases e.g /var/lib/dhcp/dhclient.eth0.leases
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
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.
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...?
Unlike Ubuntu's LTS server flavour, their Desktop edition winds-in NetworkManager into the networking gears.
And NetworkManager has its' own 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...
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;
}
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.
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.
/var/lib/dhcp/dhclient.leases
?ipconfig getpacket <ifname>
oripconfig getv6packet <ifname>
on Mac OS X.No such file or directory