🦁

DHCPサーバーでリース情報を確認する方法(Tips)

2023/05/13に公開

ISC DHCPサーバーは/var/lib/dhcp/dhcpd.leases(ディレクトリは環境に依る)にIPアドレスのリース情報を書き出しているので、リース情報を確認したい場合、このファイルを調べていました。しかし、以下の例(アドレスは加工しています)のように長いリストなのでリース状態全体を確認するのは大変でした。

root@dhcp:~# head -20 /var/lib/dhcp/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.4.1

# authoring-byte-order entry is generated, DO NOT DELETE
authoring-byte-order little-endian;

lease 192.168.0.30 {
  starts 5 2023/02/24 21:05:03;
  ends 5 2023/02/24 23:05:03;
  tstp 5 2023/02/24 23:05:03;
  cltt 5 2023/02/24 21:05:03;
  binding state free;
  hardware ethernet 66:ef:dc:xx:xx:xx;
  uid "\001f\357\334h\xxx\xxx";
  set ClientIP = "192.168.0.30";
}
lease 192.168.0.34 {
  starts 5 2023/02/24 23:45:01;
  ends 6 2023/02/25 01:45:01;
  tstp 6 2023/02/25 01:45:01;

Ubuntu 20.04のisc-dhcp-serverパッケージにはリース状態一覧を表示してくれる便利なdhcp-lease-listコマンドが含まれています。

root@dhcp:~# dpkg -S /usr/sbin/dhcp-lease-list
isc-dhcp-server: /usr/sbin/dhcp-lease-list
root@dhcp:~# apt-cache show isc-dhcp-server |head -3
Package: isc-dhcp-server
Architecture: amd64
Version: 4.4.1-2.1ubuntu5.20.04.5

以下のようにMACアドレスとホスト名、IPアドレスを一行毎にまとめたリストで表示してくれます。

root@dhcp:~# dhcp-lease-list --help
Prints active DHCP leases.

Usage: /usr/sbin/dhcp-lease-list [options]
 --help      shows this help
 --parsable  machine readable output with full dates
 --last      prints the last (even if end<now) entry for every MAC
 --all       prints all entries i.e. more than one per MAC
 --lease     uses the next argument as the name of the lease file
             the default is to try /var/db/dhcpd.leases then
             /var/lib/dhcp/dhcpd.leases then
             /var/lib/dhcp3/dhcpd.leases

root@dhcp:~# dhcp-lease-list |head -5
To get manufacturer names please download http://standards.ieee.org/regauth/oui/oui.txt to /usr/local/etc/oui.txt
Reading leases from /var/lib/dhcp/dhcpd.leases
MAC                IP              hostname       valid until         manufacturer
===============================================================================================
00:22:4d:xx:xx:xx  192.168.0.20   host_abc       2023-05-12 22:29:00 -NA-
64:27:37:xx:xx:xx  192.168.0.25   BRW64273XXXXXX 2023-05-12 22:27:32 -NA-

Discussion