Generate /etc/resolv.conf by DHCP server

If you use multiple machines or manage virtual machines, it is going to be difficult to map IP address and hostname in private network. DHCP server and DNS server will resolve it. But you might want to generate /etc/resolv.conf by DHCP server. This article will describe how to generate /etc/resolv.conf by DHCP server.

1 The goal /etc/resolv.conf

The nameserver 192.168.11.2 manages hiroom2.com network. The nameserver 192.168.11.1 manages the internet.

search hiroom2.com
nameserver 192.168.11.2
nameserver 192.168.11.1

2 Configuration of DHCP server

Define domain-name and domain-name-servers in /etc/dhcp/dhcpd.conf.

subnet 192.168.11.0 netmask 255.255.255.0 {
  option domain-name "hiroom2.com";
  option domain-name-servers 192.168.11.2, 192.168.11.1;
  option routers 192.168.11.1;
}

The mapping between dhcpd.conf and resolv.conf is as below.

 

dhcpd.conf resolv.conf
option domain-name serach
option domain-name-servers nameserver

 

3 Client Fedora/CentOS

When PEERDNS is defined in /etc/sysconfig/network-scripts/ifcfg-xxx, /etc/resolv.conf will be generated by DHCP server. Client Fedora/CentOS defines PEERDNS by default.

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
DEVICE="eth0"
ONBOOT="yes"
NAME="eth0"

4 Client Debian

/etc/resolv.conf is generated in client Debian by default.

$ sudo cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifupdown,keyfile

[ifupdown]
managed=false

5 Client Ubuntu

/etc/resolv.conf is not generated in client Ubuntu by default.

$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by
# resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE
# OVERWRITTEN
nameserver 127.0.1.1
search hiroom2.com

You need to disable dnsmasq in NetworkManager.

$ sudo diff -uprN /etc/NetworkManager/NetworkManager.conf{.org,}
--- /etc/NetworkManager/NetworkManager.conf.org 2016-04-28 01:07:32.015679999 +0900
+++ /etc/NetworkManager/NetworkManager.conf     2016-04-28 01:07:45.307679999 +0900
@@ -1,6 +1,6 @@
 [main]
 plugins=ifupdown,keyfile,ofono
-dns=dnsmasq
+#dns=dnsmasq

 [ifupdown]
 managed=false
$ sudo systemctl restart network-manager

/etc/resolv.conf is generated in client Ubuntu.

$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by
# resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE
# OVERWRITTEN
nameserver 192.168.11.2
nameserver 192.168.11.1
search hiroom2.com