89

Is there a simple ping-like command to test whether a DHCP service is running on a network? ...on Linux

9 Answers 9

17

Even if you don't use a Nagios monitoring server, you can grep the check_dhcp binary or compile it from source and use the check to test a DHCP-Server. E.g.

./check_dhcp -v -s <dhcp_server_address> -r <expected_ip_address> -m <mac_address_touse>
120

Based on this answer, assuming you have installed nmap (sudo apt install nmap):

sudo nmap --script broadcast-dhcp-discover

Add the -e $interface option if you have more than one network interface. (For example: nmap --script broadcast-dhcp-discover -e eth0)

Sample output:

Starting Nmap 7.01 ( https://nmap.org ) at 2017-09-27 17:40 CEST
Pre-scan script results:
| broadcast-dhcp-discover: 
|   Response 1 of 1: 
|     IP Offered: 192.168.81.94
|     DHCP Message Type: DHCPOFFER
|     Server Identifier: 192.168.81.2
|     IP Address Lease Time: 5m00s
|     Subnet Mask: 255.255.255.0
|     Router: 192.168.81.2
|     Domain Name Server: 192.168.81.2
|     Domain Name: example.lan
|     NTP Servers: 192.168.81.10, 192.168.81.2
|     NetBIOS Name Server: 192.168.81.10
|_    NetBIOS Node Type: 8
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.66 seconds

Or this output on a different network, with nmap v. 6 :

Starting Nmap 6.00 ( http://nmap.org ) at 2017-09-27 17:42 CEST
Pre-scan script results:
| broadcast-dhcp-discover: 
|   IP Offered: 192.168.4.101
|   DHCP Message Type: DHCPOFFER
|   Server Identifier: 192.168.4.1
|   IP Address Lease Time: 7 days, 0:00:00
|   Subnet Mask: 255.255.255.0
|   Time Offset: 7200
|   Router: 192.168.4.1
|   Domain Name Server: 208.91.112.53, 208.91.112.52
|   Renewal Time Value: 3 days, 12:00:00
|_  Rebinding Time Value: 6 days, 3:00:00
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 1.15 seconds
4
  • this didn't find my DHCP server.- must be something wrong with my pc... ethernet works, just not that.
    – Jasen
    Nov 22, 2017 at 23:27
  • 4
    Note that this script uses a fixed MAC address (DE:AD:CO:DE:CA:FE) and therefore will not work if your DHCP server is configured with a whitelist.
    – Bob
    Oct 8, 2019 at 5:27
  • 1
    @Bob An option has been added. --script-args mac=XX:XX:XX:XX:XX:XX. It hasn't been released yet though. Aug 14, 2020 at 8:25
  • 1
    For DHCPv6 there's sudo nmap -6 --script broadcast-dhcp6-discover
    – Ken Sharp
    Dec 6, 2023 at 7:04
17

or you can try dhcpcd in test mode:

dhcpcd -T eth0
4
  • 1
    Use dhcpcd -T eth0 -t 2 to timeout after 2 seconds Sep 22, 2017 at 11:14
  • Could you please expand on why should this command detect DHCP servers on your network and what kind of output should we expect when someone will be detected? May 29, 2020 at 19:30
  • a dhcp server needs to see a packet on the network from a host requesting a dhcp configuration message. therefore we need to mimic a dhcp request,, and this tool will do that and print the response, which will be an ip address, netwmask, default router, and optionally dns resolvers, ntp servers etc.
    – Paul M
    Jun 22, 2020 at 14:28
  • not all distros seem to have the dhcpcd program, so you'll have to try the dhclient program in test mode, as discussed elsewhere on this page.
    – Paul M
    Dec 11, 2020 at 14:03
14

dhcpdump is good for sniffing and displaying DHCP packets on the network.

E.g.

# dhcpdump -i eth0
---------------------------------------------------------------------------

  TIME: 2015-11-27 11:41:37.379
    IP: 0.0.0.0 (0:11:b9:5:0:b8) > 255.255.255.255 (ff:ff:ff:ff:ff:ff)
    OP: 1 (BOOTPREQUEST)
 HTYPE: 1 (Ethernet)
  HLEN: 6
  HOPS: 0
   XID: abce9327
  SECS: 0
 FLAGS: 0
CIADDR: 0.0.0.0
YIADDR: 0.0.0.0
SIADDR: 0.0.0.0
GIADDR: 0.0.0.0
CHADDR: 00:11:b9:05:00:b8:00:00:00:00:00:00:00:00:00:00
 SNAME: .
 FNAME: .
OPTION:  53 (  1) DHCP message type         1 (DHCPDISCOVER)
OPTION:  57 (  2) Maximum DHCP message size 576
OPTION:  55 (  4) Parameter Request List      1 (Subnet mask)
                          3 (Routers)
                         28 (Broadcast address)
                          6 (DNS server)

---------------------------------------------------------------------------

You can use it in conjunction with another tool to do DHCP requests, or just use it to watch PCs/devices query DHCP.

4
  • 2
    Also available for macOS thru brew install dhcpdump.
    – the
    Oct 5, 2016 at 17:46
  • 1
    This seems to only only work if there is actual dhcp traffic on the network.
    – mivk
    Sep 27, 2017 at 15:51
  • 3
    @KenSharp Why? Yes. dhcpdump will only show something when a client on the network requests a dhcp address. If you don't want to wait and don't want to send a dhcp request yourself but only see which machine would answer what IF there were a request, then this is not the solution.
    – mivk
    Feb 26, 2018 at 15:49
  • This is what I'd recommend for anyone trying to diagnose TFTP problems. Knowing the thin client wasn't even requesting the field my router UI exposes as "TFTP Server IP/hostname" and just assumes the DHCP and TFTP will be on the same box would have saved me an entire afternoon.
    – ssokolow
    Jan 17 at 2:05
8

Why not just make a DHCP request?

dhclient

For verbose output use:

dhclient -v

5
  • 24
    Probably because that will make your system use the IP assigned by the DHCP server which is not what one wants when e.g. trying to find out if there are rogue DHCP servers in his network. Jun 9, 2012 at 18:54
  • 6
    -n option helps here? Jan 24, 2013 at 22:20
  • 1
    Option -n seems to be broken on ISC DHCP Client v4.2.4 on Ubuntu 14 LTS? Apr 15, 2016 at 14:33
  • 2
    I don't think -n does what you think it does.
    – Jasen
    Nov 22, 2017 at 23:19
  • explainshell.com/explain?cmd=dhclient+-n: "Do not configure any interfaces. This is most likely to be useful in combination with the -w flag."
    – mwfearnley
    Jan 28, 2019 at 12:22
7

I wrote a DHCP test client program a while ago, works on Windows and POSIX:

https://github.com/CyberShadow/dhcptest

1
  • Written in D... It seems possible (with ldc2) to get static binaries with ldc2 -link-defaultlib-shared=false -static -L=-lphobos2-ldc -L=-lz dhcptest.d Oct 22, 2018 at 7:27
5

There is a program called dhcping (with just one p)

0

if you use nagios you could monitor it with check_dhcp

0

On Debian-Ubuntu systems:

dhcpd -f

gives good feedback and tests the configuration.

On RH systems try:

/etc/rc.d/init.d/dhcpd configtest
1
  • That test is run on the dhcp server to check its configuration, it doesn't tell you if a device on the network will actually see a working server
    – Paul M
    May 30, 2020 at 21:44

You must log in to answer this question.

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