How to Install CoreDNS

1. Introduction

CoreDNS is a flexible, extensible open-source DNS server. It supports a variety of protocols and storage backends, making it an ideal choice for building modern, customizable DNS infrastructure. This article explains how to install and configure CoreDNS so you can quickly set up a powerful and stable DNS service.

2. Steps

  1. Download CoreDNS and put the binary in the bin folder. We'll use version 1.11.1 as an example. Download link: Github

    wget https://github.com/coredns/coredns/releases/download/v1.11.1/coredns_1.11.1_linux_amd64.tgz
    
    tar -xvf coredns_1.11.1_linux_amd64.tgz
    
    cd coredns_1.11.1_linux_amd64
    
    cp coredns /usr/bin/coredns
    
  2. Create the service file

    vi /etc/systemd/system/coredns.service
    

    Enter the following content:

    [Unit]
    Description=CoreDNS DNS server
    Documentation=https://coredns.io
    After=network.target
    
    [Service]
    PermissionsStartOnly=true
    LimitNOFILE=1048576
    LimitNPROC=512
    CapabilityBoundingSet=CAP_NET_BIND_SERVICE
    AmbientCapabilities=CAP_NET_BIND_SERVICE
    NoNewPrivileges=true
    User=root
    WorkingDirectory=~
    ExecStart=/usr/bin/coredns -conf=/etc/coredns/Corefile
    ExecReload=/bin/kill -SIGUSR1 
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    

    After saving, reload the systemd configuration: systemctl daemon-reload

  3. Create the Corefile configuration file

    First, create a folder to hold the configuration file

    mkdir /etc/coredns
    

    Create the configuration file

    vi /etc/coredns/Corefile
    

    This is a simple configuration that uses the 114 public DNS server for name resolution and enables a few basic plugins.

    . {
        errors
        log
        health
        ready
        forward . 114.114.114.114
        cache
    }
    

    Save the file

  4. Make sure port 53 is not in use

    lsof -i:53
    

    dnsmasq occupying port 53 (common on CentOS 7) First check whether it is enabled with systemctl status dnsmasq.service. If it is, first kill -9 the process, stop it with systemctl stop dnsmasq.service, and then disable it at boot with systemctl disable dnsmasq.service. After disabling it, reboot the server to test and make sure the service doesn't come back up.

    systemd-resolved occupying port 53 (common on Ubuntu Server) First edit /etc/systemd/resolved.conf as follows:

    [Resolve]
    DNS=114.114.114.114
    DNSStubListener=no
    

    DNS sets the DNS server this machine points to; your ISP's DNS or a public DNS is recommended. DNSStubListener=no means it won't listen on port 53. Once configured, run these commands to restart the system service: sudo systemctl daemon-reload sudo systemctl restart systemd-resolved Check where the /etc/resolv.conf symlink points by running ll /etc/resolv.conf. If the symlink shows the following, no change is needed: /etc/resolv.conf -> /run/systemd/resolve/resolv.conf. Otherwise, set the symlink with sudo ln -s -f /run/systemd/resolve/resolv.conf /etc/resolv.conf.

  5. Enable CoreDNS at boot and start it

    systemctl enable coredns
    systemctl start coredns
    

3. FAQ

Question 1: What if I still can't connect after configuring it? Answer 1: Check whether CoreDNS is listening on port 53. If it is, check whether the firewall allows access to port 53.