DHCP Client
Summary
...
Code Block |
---|
|
/ip dhcp-client |
The DHCP (Dynamic Host Configuration Protocol) is used for the easy distribution of IP addresses in a network. The MikroTik RouterOS implementation includes both server and client parts and is compliant with RFC 2131.
...
Name | code | value |
---|
clientid_duid | 61 | 0xff$(CLIENT_DUID) |
clientid | 61 | 0x01$(CLIENT_MAC) |
hostname | 12 | $(HOSTNAME) |
Properties
Property | Description |
---|
add-default-route (yes | no | special-classless; Default: yes) | Whether to install default route in routing table received from |
dhcp DHCP server. By default, the RouterOS client complies |
to 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 classless route if received, if not then add default route (old behavior)
- special-classless - adds both classless
|
route - routes if received and a default route (MS style)
|
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-distance (integer:0..255; Default: ) | Distance of default route. Applicable if add-default-route is set to yes . |
disabled (yes | no; Default: yes) |
|
host-name (string; Default: ) |
Host name 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: ) |
Interface The interface on which the DHCP client will be running. |
script (script; Default: ) | Execute script on status change. This parameter is available in v6.39rc33+ These are available 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 - an array of received options
Example >> |
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) |
Read-only properties
Property | Description |
---|
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) |
Time A time when the lease expires (specified by the DHCP server). |
gateway (IP) | The IP address of the gateway which is assigned by DHCP server |
invalid (yes | no) | Shows whether a configuration is invalid. |
netmask (IP) |
|
primary-dns (IP) | The IP address of the first DNS resolver, that 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 DHCP Client |
Menu specific commands
Property | Description |
---|
release (numbers) | Release current binding and restart DHCP client |
renew (numbers) | Renew current leases. If the |
renew 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) |
Configuration Examples
Simple DHCP client
Add a DHCP client on the ether1 interface:
Code Block |
---|
|
/ip dhcp-client add interface=ether1 disabled=no |
...
Now we can further extend the script, to check if the address already existexists, and remove the old one if changes are needed
...
DHCP-client in RouterOS is capable of being a DHCPv6-client and DHCP-PD client. So it is able to get a prefix from the DHCP-PD server as well as the DHCPv6 stateful address from the DHCPv6 server.
Properties
Property | Description |
---|
add-default-route (yes | no; Default: no) | Whether to add default IPv6 route after a client connects. |
comment (string; Default: ) | Short description of the client |
disabled (yes | no; Default: no) |
|
interface (string; Default: ) |
Interface The interface on which the DHCPv6 client will be running. |
pool-name (string; Default: ) | Name of the IPv6 pool in which received IPv6 prefix will be added |
pool-prefix-length (string; Default: ) | Prefix length parameter that will be set for IPv6 pool in which received IPv6 prefix is added. Prefix length must be greater than the length of the received prefix, otherwise, prefix-length will be set to received prefix length + 8 bits. |
prefix-hint (string; Default: ) | Include a preferred prefix length. |
request (prefix, address; Default: ) | to choose if the DHCPv6 request will ask for the address or the IPv6 prefix, or both. |
script (string; Default: ) | Run this script on the |
dhcpDHCP-client status change. Available variables:- pd-valid - if the prefix is acquired by the client;
- pd-prefix - the prefix acquired by the client if any;
- na-valid - if the address is acquired by the client;
- na-address - the address acquired by the client if any.
- options - array of received options (only ROSv7)
|
use-peer-dns (yes | no; Default: yes) | Whether to accept the DNS settings advertised by the IPv6 DHCP Server. |
Read-only properties
Property | Description |
---|
duid (string) | Auto-generated DUID that is sent to the server. DUID is generated using one of the MAC addresses available on the router. |
request (list) | specifies what was requested - prefix, address, or both. |
dynamic (yes | no) |
|
expires-after (time) |
Time A time when the IPv6 prefix expires (specified by the DHCPv6 server). |
invalid (yes | no) | Shows whether a configuration is invalid. |
prefix (IPv6 prefix) | Shows received IPv6 prefix from DHCPv6-PD server |
status (stopped | searching | requesting... | bound | renewing | rebinding | error | stopping) | Shows the status of DHCPv6 Client:- stopped - dhcpv6 client is stopped
- searching - sending "solicit" and trying to get "advertise"
- requesting - sent "request" waiting for "reply"
- bound - received "reply". Prefix assigned.
- renewing - sent "renew", waiting for "reply"
- rebinding - sent "rebind", waiting for "reply"
- error - reply was not received in time or some other error
|
ocurred- occurred.
- stopping - sent "release"
|
Menu specific commands
Property | Description |
---|
release (numbers) | Release current binding and restart DHCPv6 client |
renew (numbers) | Renew current leases. If the |
renew 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
It is possible to add a script that will be executed when a prefix or an address is acquired and applied or expires and is removed using the DHCP client. There are separated sets of variables that will have the value set by the client depending on prefix or address status change as the client can acquire both and each of them can have a different effect on the router configuration.
...
To determine what IAID will be used, convert the internal ID of an interface on which the DHCP client is running from hex to decimal.
For example, the DHCP client is running on interface pppoePPPoE-out1. To get internal ID use the following command:
Code Block |
---|
|
[admin@t36] /interface> :put [find name="pppoe-out1"]
*15 |
...
Code Block |
---|
|
/ip dhcp-server option
add code=252 name=auto-proxy-config value="'https://autoconfig.something.lv/wpad.dat'" |
Vendor Classes
Since the 6.45beta6 version RouterOS support vendor class, ID matcher. The vendor class is used by DHCP clients to optionally identify the vendor and configuration.
Note |
---|
Vendor-class-id matcher changes to generic matcher since RouterOS v7.4beta4. |
Example
In the following configuration example, we will give an IP address from a particular pool for an Android-based mobile phone. We will use the RouterBOARD with a default configuration
...
Code Block |
---|
|
10:30:31 dhcp,debug,packet defconf received request with id 4238230732 from 0.0.0.0
10:30:31 dhcp,debug,packet secs = 3
10:30:31 dhcp,debug,packet ciaddr = 0.0.0.0
10:30:31 dhcp,debug,packet chaddr = 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet Msg-Type = request
10:30:31 dhcp,debug,packet Client-Id = 01-30-07-4D-F5-07-49
10:30:31 dhcp,debug,packet Address-Request = 172.16.16.120
10:30:31 dhcp,debug,packet Server-Id = 192.168.88.1
10:30:31 dhcp,debug,packet Max-DHCP-Message-Size = 1500
10:30:31 dhcp,debug,packet Class-Id = "android-dhcp-9"
10:30:31 dhcp,debug,packet Host-Name = "Galaxy-S8"
10:30:31 dhcp,debug,packet Parameter-List = Subnet-Mask,Router,Domain-Server,Domain-Name,Interface-MTU,Broadcast-Address,Address-Time,Ren
ewal-Time,Rebinding-Time,Vendor-Specific
10:30:31 dhcp,info defconf assigned 172.16.16.120 to 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet defconf sending ack with id 4238230732 to 172.16.16.120
10:30:31 dhcp,debug,packet ciaddr = 0.0.0.0
10:30:31 dhcp,debug,packet yiaddr = 172.16.16.120
10:30:31 dhcp,debug,packet siaddr = 192.168.88.1
10:30:31 dhcp,debug,packet chaddr = 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet Msg-Type = ack
10:30:31 dhcp,debug,packet Server-Id = 192.168.88.1
10:30:31 dhcp,debug,packet Address-Time = 600
10:30:31 dhcp,debug,packet Domain-Server = 192.168.88.1,10.155.0.1,10.155.0.126 |
Configuration Examples
Setup
To simply configure DHCP server you can use a setup
command.
Generic matcher
Since RouterOS 7.4beta4 (2022-Jun-15 14:04) the vendor-id matcher is converted to a generic matcher. The genric matcher allows matching any of the DHCP options.
And an example to match DHCP option 60 similar to vendor-id-class matcher:
Code Block |
---|
|
/ip dhcp-server matcher
add address-pool=pool1 code=60 name=test value=android-dhcp-11 |
Match the client-id with option 61 configured as hex value:
Code Block |
---|
|
/ip dhcp-server matcher
add address-pool=pool1 code=61 name=test value=0x016c3b6bed8364 |
Match the code 12 using the string:
Code Block |
---|
|
/ip dhcp-server matcher
add address-pool=testpool code=12 name=test server=dhcp1 value="MikroTik" |
Configuration Examples
Setup
To simply configure DHCP server you can use a setup
command.
FirstFirst, you configure an IP address on the interface:
...
- And finally, add DHCP Server, here we will add the previously created address pool, and specify on which interface the DHCP server should work on
...
Client binding creates a dynamic pool with a timeout set to binding's expiration time (note that now dynamic pools can have a timeout), which will be updated every time binding gets renewed.
...