Проверить работу dhcp linux

Check if a DHCP server existing in my network using bash

using CentOS with static IP, Is there any way to determinate if a DHCP server running on the network using bash ?

6 Answers 6

sudo nmap --script broadcast-dhcp-discover -e eth0 
Starting Nmap 6.40 ( http://nmap.org ) at 2016-08-16 09:25 UTC Pre-scan script results: | broadcast-dhcp-discover: | IP Offered: 192.168.0.67 | DHCP Message Type: DHCPOFFER | Server Identifier: 192.168.0.1 | IP Address Lease Time: 0 days, 0:05:00 | Subnet Mask: 255.255.255.0 | Router: 192.168.0.1 | Domain Name Server: 8.8.8.8 | Domain Name: maas | Broadcast Address: 192.168.0.255 |_ NTP Servers: 91.189.91.157, 91.189.89.199, 91.189.94.4, 91.189.89.198 WARNING: No targets were specified, so 0 hosts scanned. Nmap done: 0 IP addresses (0 hosts up) scanned in 0.27 seconds 

Note: there is a similar script for dhcpv6

sudo nmap --script broadcast-dhcp6-discover -e eth0 

Good command but its worth noting that this just outputs the first DHCP server that responds. If multiple DHCP servers exists, this command won’t find them.

I prefer this variant, but it seems that the machine on which you’re executing this, really gets an offered IP assigned to the queried interface. Any way to prevent that?

If available in the repository there is dhcpdump

SYNOPSIS dhcpdump [-h regular-expression] -i interface DESCRIPTION This command parses the output of tcpdump to display the dhcp-packets for easier checking and debugging. USAGE dhcpdump -i /dev/fxp0 If you want to filter a specific Client Hardware Address (CHADDR), then you can specifiy it as a regular expressions: dhcpdump -i /dev/fxp0 -h ^00:c0:4f This will display only the packets with Client Hardware Addresses which start with 00:c0:4f. 

If you have tcpdump available to you, invoking the program as root with the following parameters might assist you in finding the server:

tcpdump -i [interface id] -nev udp port 68

Unfortunately, due to my network’s layout, I can’t get a full DHCP handshake captured right away. However, I do see a DHCP Request from my iPad:

22:16:44.767371 30:10:e4:8f:02:14 > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 342: (tos 0x0, ttl 255, id 15652, offset 0, flags [none], proto UDP (17), length 328) 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 30:10:e4:8f:02:14, length 300, xid 0x42448eb6, Flags [none] Client-Ethernet-Address 30:10:e4:8f:02:14 Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Request Parameter-Request Option 55, length 6: Subnet-Mask, Default-Gateway, Domain-Name-Server, Domain-Name Option 119, Option 252 MSZ Option 57, length 2: 1500 Client-ID Option 61, length 7: ether 30:10:e4:8f:02:14 Requested-IP Option 50, length 4: 192.168.2.222 Lease-Time Option 51, length 4: 7776000 Hostname Option 12, length 15: "NevinWiamssiPad" 

After letting `tcpdump’ run overnight, I did eventually see this ACK:

07:46:40.049423 a8:39:44:96:fa:b8 > 68:a8:6d:58:5b:f3, ethertype IPv4 (0x0800), length 320: (tos 0x0, ttl 64, id 0, offset 0, flags [none], proto UDP (17), length 306) 192.168.2.1.67 > 192.168.2.22.68: BOOTP/DHCP, Reply, length 278, xid 0x5e7944f, Flags [none] Client-IP 192.168.2.22 Your-IP 192.168.2.22 Client-Ethernet-Address 68:a8:6d:58:5b:f3 Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: ACK Server-ID Option 54, length 4: 192.168.2.1 Lease-Time Option 51, length 4: 86400 Subnet-Mask Option 1, length 4: 255.255.255.0 Default-Gateway Option 3, length 4: 192.168.2.1 Domain-Name-Server Option 6, length 8: 192.168.2.1,142.166.166.166 

If when running that tcpdump command, and you see a BOOTP/DHCP Offer or Ack(Nack), that will be from a DHCP server, and the server’s MAC address will be right after the timestamp on the first line.

Читайте также:  Will acronis backup linux

So the (valid) DHCP server here has MAC address a8:39:44:96:fa:b8`.

Using one of many MAC address lookup tools on the web I see this MAC belongs to A8:39:44 Actiontec Electronics, Inc which is my router.

In order to catch rogue DHCP server packets as they happen, I would have to leave this tcpdump process running in terminal window:

tcpdump -i en0 -nev udp src port 67 and not ether host a8:39:44:96:fa:b8

This will only show me DHCP server responses from hosts other than my valid DHCP server, as long as the process is running in its own window.

The following command will run in the background until 100 packets are captured, appending any rogue DHCP server messages to the file /tmp/rogue . Again, the MAC address of your valid DHCP server has to be used in the appropriate place, as well as the interface descriptor on your system.

tcpdump -U -i en0 -c 100 -nev udp src port 67 and not ether host a8:39:44:96:fa:b8 >> /tmp/rogue 2>&1 &

Источник

10 dhclient (DHCP Client) command examples in Linux

10 dhclient (DHCP Client) command examples in Linux 1

In this article, we will look into 10 dhclient command examples in Linux. dhclient is a free and open source DHCP client tool used for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address. Using dhclient tool, a DHCP client system can request an IP address from a Central DHCP Server which which maintains a list of IP addresses which may be assigned on one or more subnets. Here we will see some of the real time examples of dhclient command in below section.

Читайте также:  Wine linux установка fedora

10 dhclient (DHCP Client) command examples in Linux

10 dhclient (DHCP Client) command examples in Linux

Example 1: How to Check dhclient command version

To check the current version of dhclient tool, you need to run dhclient —version command as shown below.

10 dhclient (DHCP Client) command examples in Linux 2

Example 2: How to Get a DHCP IP on lease

You can simply run sudo dhclient command to request a DHCP IP on lease from the available DHCP Server.

Please note that if you already have a lease IP generated then running sudo dhclient command will show you RTNETLINK answers: File exists on the output.

cyberithub@ubuntu:~$ sudo dhclient

Example 3: How to use verbose mode(-v)

If you want to use verbose mode then you need to use -v option with dhclient command as shown below.

Please note that you need to either have sudo or root access to run dhclient command. Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

cyberithub@ubuntu:~$ sudo dhclient -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s8/08:00:27:5f:48:3a Sending on LPF/enp0s8/08:00:27:5f:48:3a Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.104 on enp0s8 to 255.255.255.255 port 67 (xid=0x4dcd9939) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 67 (xid=0x606bc4b3) DHCPACK of 192.168.0.104 from 192.168.0.1 (xid=0x3999cd4d) .

Example 4: How to release lease IP from an Interface

If you need to release the leased IP and stop the running DHCP client as previously recorded in the PID file then you need to use -r option. In this example we are releasing IP from enp0s3 network interface using sudo dhclient -r -v enp0s3 command as shown below.

10 dhclient (DHCP Client) command examples in Linux 3

Example 5: How to obtain IPV6 address using dhclient command

If you want to use the DHCPv6 protocol to obtain whatever IPv6 addresses are available along with configuration parameters then you need to use -6 option with dhclient command as shown below.

cyberithub@ubuntu:~$ sudo dhclient -6 -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on Socket/enp0s3 Sending on Socket/enp0s3 PRC: Soliciting for leases (INIT). XMT: Forming Solicit, 0 ms elapsed. XMT: X-- IA_NA 27:16:c6:22 XMT: | X-- Request renew in +3600 XMT: | X-- Request rebind in +5400 XMT: Solicit on enp0s3, interval 1070ms. .

Example 6: How to stop the running DHCP client without releasing the current lease

If you need to stop the running DHCP client without releasing the current lease IP then you need to use -x option with dhclient command as shown below.

10 dhclient (DHCP Client) command examples in Linux 4

Example 7: How to Change UDP Listening Port

If you want to change the UDP port number on which the DHCP client should listen and transmit to then you need to use -p option with port number specified as an argument. In this example, we are using UDP Port 556 on which dhclient would listen and transmit to using sudo dhclient -v -p 556 enp0s3 command as shown below.

cyberithub@ubuntu:~$ sudo dhclient -v -p 556 enp0s3 binding to user-specified port 556 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) .

Example 8: How to Specify Server IP Address

If you want to change the DHCP Server address from where the IP address needs to be leased out then you need to use -s option and specify the destination server IP address or its fully qualified domain name as shown below. For example, here we are using destination server address as 192.168.0.106 .

cyberithub@ubuntu:~$ sudo dhclient -v -s 192.168.0.106 enp0s3 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) .

Example 9: How to Check all the options available with dhclient tool

To check all the options available with dhclient command you can use dhclient -h or dhclient —help as shown below.

Читайте также:  User is currently logged in linux

cyberithub@ubuntu:~$ dhclient -h Usage: dhclient [-4|-6] [-SNTPRI1dvrxi] [-nw] [-p ] [-D LL|LLT] [—dad-wait-time ] [—prefix-len-hint ] [—decline-wait-time ] [—address-prefix-len ] [-s server-addr] [-cf config-file] [-df duid-file] [-lf lease-file] [-pf pid-file] [—no-pid] [-e VAR=val] [-sf script-file] [interface]* dhclient

Example 10: How to Check Man page of dhclient tool

To check the man page of dhclient tool, you need to use man dhclient command as shown below.

cyberithub@ubuntu:~$ man dhclient dhclient(8) System Manager's Manual dhclient(8) NAME dhclient - Dynamic Host Configuration Protocol Client SYNOPSIS dhclient [ -4 | -6 ] [ -S ] [ -N [ -N. ] ] [ -T [ -T. ] ] [ -P [ -P. ] ] -R ] [ -i ] [ -I ] [ -4o6 port ] [ -D LL|LLT ] [ -p port- number ] [ -d ] [ -df duid-lease-file ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ] [ -lf lease-file ] [ -pf pid-file ] [ --no-pid ] [ -cf con‐ fig-file ] [ -sf script-file ] [ -s server-addr ] [ -g relay ] [ -n ] [ -nw ] [ -w ] [ --dad-wait-time seconds ] [ --prefix-len-hint length ] [ --decline-wait-time seconds ] [ -v ] [ --version ] [ if0 [ . ifN ] ] DESCRIPTION The Internet Systems Consortium DHCP Client, dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address.

Источник

Оцените статью
Adblock
detector