Manual:IP/DHCP Client

From MikroTik Wiki
Revision as of 14:32, 9 February 2017 by Emilsz (talk | contribs)
Jump to navigation Jump to search

Applies to RouterOS: v3, v4 +

Summary

The MikroTik RouterOS DHCP client may be enabled on any Ethernet-like interface at a time. The client will accept an address, netmask, default gateway, and two dns server addresses. The received IP address will be added to the interface with the respective netmask. The default gateway will be added to the routing table as a dynamic entry. Should the DHCP client be disabled or not renew an address, the dynamic default route will be removed. If there is already a default route installed prior the DHCP client obtains one, the route obtained by the DHCP client would be shown as invalid.

RouterOS DHCP cilent asks for following options:

  • option 1 - SUBNET_MASK,
  • option 3 - GATEWAY_LIST,
  • option 6 - TAG_DNS_LIST,
  • option 33 - STATIC_ROUTE,
  • option 42 - NTP_LIST,
  • option 121 - CLASSLESS_ROUTE,


Option

DHCP client has possibility to set up options that are sent to DHCP server. For example, host-name and MAC address. Syntax is same as for DHCP server options.

Note: This feature is available since RouterOS 6.0



Currently there are two variables that can be used in options:

  • HOSTNAME;
  • CLIENT_MAC - client's interface MAC address;

IPv6

Starting from v5.8 DHCP Client can receive delegated prefixes from DHCPv6 server. Currently received prefix is added to IPv6 pool, which later can be used for example in pppoe server configuration. Starting from v5.9, DHCPv6 client configuration was moved to /ipv6 sub-menu. Read-more >>

Quick setup example

Add a DHCP client on ether1 interface:

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

After interface is added, you can use "print" or "print detail" command to see what parameters 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>

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


Lease script example

Since RouterOS v6.39rc27, it is possible to execute a script when DHCP client status changes. This is an example script that automatically adds a default route with routing-mark=WAN1 and removes it when the lease expires or is removed.

/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 (\$valid=1) do={\r\
    \n        :if (\$count = 0) do={\r\
    \n            /ip route add gateway=\$serverAddress comment=\"WAN1\" routing-mark=\$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] != \$serverAddress) do={\r\
    \n                    /ip route set \$test gateway=\$serverAddress\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"

Properties

Sub-menu: /ip dhcp-client


Property Description
add-default-route (yes | no | special-classless; Default: yes) Whether to install default route in routing table received from dhcp server. By default RouterOS client complies to RFC and ignores option 3 if classless option 121 is received. To force 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 if received and default route (MS style)
client-id (string; Default: ) Corresponds to the settings suggested by the network administrator or ISP. If not specified, 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 of the client sent to a DHCP server. If not specified, client's system identity will be used.
interface (string; Default: ) Interface on which DHCP client will be running.
script (script; Default: ) Execute script on status change. This parameter is available in v6.39rc27+ These are available variables that are accessible for the event script:
  • valid - 1 - lease is added/changed; 0 - lease is removed
  • serverAddress - server address
  • leaseAddress - address provided by server
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)

Status

Command /ip dhcp-client print detail will show current status of dhcp client and read-only properties listed in table below:


Property Description
address (IP/Netmask) IP address and netmask, which is assigned to DHCP Client from the Server
dhcp-server (IP) IP address of the DHCP server.
expires-after (time) Time when the lease expires (specified by the DHCP server).
gateway (IP) IP address of the gateway which is assigned by DHCP server
invalid (yes | no) Shows whether configuration is invalid.
netmask (IP)
primary-dns (IP) IP address of the primary DNS server, assigned by the DHCP server
primary-ntp (IP) IP address of the primary NTP server, assigned by the DHCP server
secondary-dns (IP) IP address of the secondary DNS server, assigned by the DHCP server
secondary-ntp (IP) 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 operation was not successful, client tries to reinitialize lease (i.e. it starts lease request procedure (rebind) as if it had not received an IP address yet)


[ Top | Back to Content ]