Skip to main content
Version: current

DHCP Client

Sub-menu: /ip/dhcp-client

The DHCP client gets an IPv4 address and network settings for an interface from a DHCP server. It is typically used on the interface that connects the router to an ISP or another upstream network, and it can be enabled on any Ethernet-like interface. For how the DHCP exchange, leases and renewal work, see DHCP concepts.

When the client gets a lease, it applies the received settings to the router:

  • The IP address and netmask are added to the interface as a dynamic address.
  • A dynamic default route through the received gateway, or the received classless static routes (option 121), are added to the routing table. The add-default-route property controls this.
  • The received DNS servers are added to the dynamic servers of the DNS resolver (use-peer-dns), and the received NTP servers are added to the servers of the NTP client (use-peer-ntp).

The client renews the lease before it expires. When the lease is lost or the client is disabled, the dynamic address and routes are removed.

If another default route already exists, the route with the lower distance is used. When the distances are equal, both routes are active and traffic is shared between them (ECMP). Use default-route-distance to control which route is preferred.

Configuration Examples

Simple DHCP client

Add a DHCP client on the ether1 interface:

/ip/dhcp-client/add interface=ether1 disabled=no

After the interface is added, you can use the "print" or "print detail" command to see what parameters the DHCP client acquired:

[admin@MikroTik] /ip/dhcp-client> print detail
Flags: X - disabled, I - invalid
0 interface=ether1 add-default-route=yes use-peer-dns=yes use-peer-ntp=yes
status=bound address=192.168.0.65/24 gateway=192.168.0.1
dhcp-server=192.168.0.1 primary-dns=192.168.0.1 primary-ntp=192.168.0.1
expires-after=9m44s
[admin@MikroTik] /ip/dhcp-client>
warning

If the interface used by the DHCP client is part of the VRF configuration, then the default route and other received routes from the DHCP server will be added to the VRF routing table.

DHCP client status can be checked with:

/ip/dhcp-client/print detail

DHCP Options

The client asks the DHCP server for the following options:

  • Option 1 - Subnet Mask.
  • Option 3 - Gateway Addresses.
  • Option 6 - DNS Server Addresses.
  • Option 15 - Domain Name.
  • Option 33 - Static Routes.
  • Option 42 - NTP Server Addresses.
  • Option 43 - Vendor Specific Information.
  • Option 121 - Classless Static Routes.
  • Option 138 - CAPWAP Access Controller Addresses.

The DHCP client has the possibility to set up options that are sent to the DHCP server, for example, hostname and MAC address. The syntax is the same as for DHCP server options.

Currently, there are three variables that can be used in options:

  • HOSTNAME.
  • CLIENT_MAC - client interface MAC address.
  • CLIENT_DUID - client DUID of the router, same as used for the DHCPv6 client. In conformance with RFC4361.

DHCP client default options include these default options:

Namecodevalue
clientid_duid610xff$(CLIENT_DUID)
clientid610x01$(CLIENT_MAC)
hostname12$(HOSTNAME)

Properties

PropertyDescription
add-default-route (yes | no | special-classless; Default: yes)Whether to install a default route in the routing table received from DHCP server. By default, the RouterOS client complies with RFC and ignores option 3 if classless option 121 is received. To force the client not to ignore option 3 set special-classless. This parameter is available in v6rc12+
  • yes - adds a classless route if received, if not then adds a default route (old behavior)
  • special-classless - adds both classless routes if received and a default route (MS style)
allow-reconfigure (yes | no; Default: no)Allows receiving Reconfigure (forcerenew) messages from the DHCP server. For changes in an existing dhcp-client, renew the lease.
check-gateway (none | arp | bfd | ping; Default: none)Method on how to check route gateway reachability.
client-id (string; Default: )Corresponds to the settings suggested by the network administrator or ISP. If not specified, the client's MAC address will be sent
comment (string; Default: )Short description of the client
default-route-tables (table:distance; Default: default)List of routing tables to which default route must be added. Table name can be preceded with ":x" where x would be the distance for the route to be installed with.
default-route-distance (integer:0..255; Default: 1)Default route distance.
dhcp-options (string; Default: hostname,clientid)Names of the options from /ip/dhcp-client/option that the client sends to the DHCP server. The predefined clientid and clientid_duid options both use code 61. To identify the client by its DUID (RFC 4361) instead of its MAC address, replace clientid with clientid_duid.
disabled (yes | no; Default: no)Whether the client is disabled.
dscp (integer:0..63; Default: 0)Sets the DSCP (Differentiated Services Code Point) value for outgoing DHCP client packets. This value is part of the IP header and is used to indicate the desired Quality of Service (QoS) level for network traffic.
host-name (string; Default: )The hostname of the client is sent to a DHCP server. If not specified, the client's system identity will be used.
interface (string; Default: )The interface on which the DHCP client will be running.
name (string; Default: )Name of the DHCP client, generated by RouterOS if not specified.
script (script; Default: )Execute script when DHCP client obtains a new lease or loses an existing one, received gateway address or DNS server list is changed. Variables that are accessible for the event script:
  • bound - 1 - lease is added/changed; 0 - lease is removed
  • server-address - server address
  • lease-address - lease address provided by a server
  • interface - name of the interface on which the client is configured
  • gateway-address - gateway address provided by a server
  • vendor-specific - stores value of option 43 received from DHCP server
  • lease-options - array of recieved options from server
See the lease script example.
use-broadcast (always | both | never; Default: both)Whether to set the broadcast bit in DHCPDISCOVER and DHCPREQUEST messages.
always - broadcastbit is set always
both - broadcast bit is set only for the first 15 seconds
never - broadcastbit is not set
use-peer-dns (yes | no; Default: yes)Whether to accept the DNS settings advertised by DHCP Server. (Will override the settings put in the /ip/dns submenu.
use-peer-ntp (yes | no; Default: yes)Whether to accept the NTP settings advertised by DHCP Server. (Will override the settings put in the /system/ntp/client submenu)
vlan-priority (integer:0..7; Default: 0)If the DHCP client is running on a VLAN interface (/interface/vlan), you can specify the Priority Code Point (PCP) value. PCP is a 3-bit field in the VLAN header used to mark the priority of packets within a VLAN, allowing traffic to be prioritized accordingly. This setting applies only to VLAN interfaces and affects the priority of outgoing DHCP client packets.

Read-only properties

PropertyDescription
address (IP/Netmask)IP address and netmask, which is assigned to DHCP Client from the Server
dhcp-server (IP)The IP address of the DHCP server.
expires-after (time)A time when the lease expires (specified by the DHCP server).
gateway (IP)The IP address of the gateway which is assigned by the DHCP server
invalid (yes | no)Shows whether a configuration is invalid.
netmask (IP)The subnet mask received from the DHCP server.
primary-dns (IP)The IP address of the first DNS resolver, which was assigned by the DHCP server
primary-ntp (IP)The IP address of the primary NTP server, assigned by the DHCP server
secondary-dns (IP)The IP address of the second DNS resolver, assigned by the DHCP server
secondary-ntp (IP)The IP address of the secondary NTP server, assigned by the DHCP server
status (bound | error | rebinding... | requesting... | searching... | stopped)Shows the status of the DHCP Client
reconfigure-key (string)Reconfiguration authentication key
reconfigure-last-counter (integer)Count of received Reconfigure (forcerenew) messages
custom-source-mac-address (MAC)Custom source MAC address used by the DHCP client.
custom-hostname-suffix (string)Suffix appended to the hostname sent to the DHCP server.
caps-managers (string)List of CAPsMAN controller addresses discovered via DHCP option 138.
PropertyDescription
release (numbers)Release current binding and restart the DHCP client
renew (numbers)Renew current leases. If the renewal operation was not successful, the client tries to reinitialize the lease (i.e. it starts the lease request procedure (rebind) as if it had not received an IP address yet)

Script Examples

Lease script example

It is possible to execute a script when a DHCP client obtains a new lease or loses an existing one. This is an example script that automatically adds a default route with routing-table=WAN1 and removes it when the lease expires or is removed.

warning

Be aware that some variables might be reserved in specific menus and cannot be used there. For details, see reserved variable names.

/ip/dhcp-client
add add-default-route=no dhcp-options=hostname,clientid disabled=no interface=ether2 script="{\r\
\n :local rmark \"WAN1\"\r\
\n :local count [/ip/route/print count-only where comment=\"WAN1\"]\r\
\n :if (\$bound=1) do={\r\
\n :if (\$count = 0) do={\r\
\n /ip/route/add gateway=\$\"gateway-address\" comment=\"WAN1\" routing-table=\$rmark\r\
\n } else={\r\
\n :if (\$count = 1) do={\r\
\n :local test [/ip/route/find where comment=\"WAN1\"]\r\
\n :if ([/ip/route/get \$test gateway] != \$\"gateway-address\") do={\r\
\n /ip/route/set \$test gateway=\$\"gateway-address\"\r\
\n }\r\
\n } else={\r\
\n :error \"Multiple routes found\"\r\
\n }\r\
\n }\r\
\n } else={\r\
\n /ip/route/remove [find comment=\"WAN1\"]\r\
\n }\r\
\n}\r\
\n"

Using received Option 43 to set the ACS URL

It is possible to set *Automatic Configuration Server (ACS)*URL in the TR069 client settings, when a DHCP client lease is bound, if the option sent from the DHCP server is configured to send it. Here's an example:

:if ($bound=1) do={
/tr069-client/set acs-url=$"vendor-specific"
}

Resolve default gateway when 'router' (option3) is from a different subnet

In some cases, administrators tend to set the 'router' option which cannot be resolved with the offered IP's subnet. For example, the DHCP server offers 192.168.88.100/24 to the client, and option 3 is set to 172.16.1.1. This will result in an unresolved default route:

# DST-ADDRESS PREF-SRC GATEWAY DISTANCE
0 DS 0.0.0.0/0 172.16.1.1 1
1 ADC 192.168.88.0/24 192.168.88.100 ether1

To fix this we need to add a /32 route to resolve the gateway over ether1, which can be done by the running script below each time the DHCP client gets an address

/system/script/add name="dhcpL" source={ /ip/address/add address=($"lease-address" . "/32") network=$"gateway-address" interface=$interface }

Now we can further extend the script, to check if the address already exists, and remove the old one if changes are needed

/system/script/add name="dhcpL" source={
/ip/address {
:local ipId [find where comment="dhcpL address"]
:if ($ipId != "") do={
:if (!([get $ipId address] = ($"lease-address" . "/32") && [get $ipId network]=$"gateway-address" )) do={
remove $ipId;
add address=($"lease-address" . "/32") network=$"gateway-address" \
interface=$interface comment="dhcpL address"
}
} else={
add address=($"lease-address" . "/32") network=$"gateway-address" \
interface=$interface comment="dhcpL address"
}
}
}