If you’re like me and enjoy the seamless integration of DHCP and DNS that dnsmasq provides, you may have wondered how to replicate this on MikroTik-based routers. While MikroTik offers great flexibility and power, their out-of-the-box DNS integration with DHCP isn’t quite as straightforward. But with a bit of scripting and the right approach, you can get dynamic DNS updates for your DHCP leases.

Let me walk you through the steps I used, so you can achieve the same effortless experience in your own network.

Step 1: Create the DHCP-DNS Script

First things first, let’s create a script that will automatically add or update DNS records whenever devices grab a DHCP lease. I found the most convenient way to do this was through the MikroTik web UI.

  1. Go to System > Scripts and click Add New.
  2. Give the script a name you’ll remember, like dhcp-dns.
  3. Paste the following code into the “Source” box:
:local domain;
:local hostname;
:local recordExists;

:set domain "$leaseServerName.example.com";
:set hostname "$"lease-hostname".$domain";
:set recordExists [:len [/ip dns static find name=$hostname]];

:log info "fqdn: $hostname";
:log info "ip: $leaseActIP";
:log info "registering: $leaseBound";
:log info "matching records: $recordExists";

:if (1 = $leaseBound && 0 = $recordExists) do={
    /ip dns static add name=$hostname address=$leaseActIP;
}

:if (1 = $leaseBound && 0 < $recordExists) do={
    /ip dns static set [find name=$hostname] address=$leaseActIP;
}

This script does a few key things every time a DHCP lease is updated:

  • Constructs a fully-qualified domain name in the format {client-hostname}.{dhcp-server-name}.example.com.
  • Checks if a DNS record for that name already exists.
  • Adds a new record if it doesn’t exist, or updates the IP address if it does.
  • Logs information about each step for easy troubleshooting.

Step 2: Associate the Script with Your DHCP Server

Now, we want this script to run automatically each time a DHCP lease changes. Here’s how to set that up:

  1. Go to IP > DHCP Server in the web UI.
  2. Select your DHCP server (you may have more than one, and you can use the same script for all of them).
  3. Find the Lease Script field and enter the name of the script you just created (for example, dhcp-dns).

That’s it! From now on, every time a client device receives or renews a lease, your MikroTik router will dynamically update your DNS records.

Why This Approach Works

With this method, you’re getting closer to the convenience of dnsmasq, new devices on your network can always be reached using their hostname, with automatic DNS updates. This is especially helpful in labs, small office setups, or even home automation networks where things change frequently.

And since the script uses variables provided by MikroTik’s DHCP lease scripting environment, you don’t need to tweak it when adding more DHCP servers or clients.


By taking these steps, I turned my MikroTik router into a dynamic, DNS-integrated network controller similar to what I’d grown accustomed to with dnsmasq. I hope you find this helpful! If you have any tweaks or improvements, feel free to share your own experience in the comments below.