In this guide, we’ll cover how to add a local DNS entry on Linux machines. Find out how you can do the same.
Domain Name System (DNS) Server
Domain Name System (DNS) is a decentralized naming service that translates domain URLs into IP addresses on the internet and intranet. In addition, a system that provides this dedicated service is called a DNS server.
You can add local entries in the DNS server when testing websites before taking them live. Also, the configuration files are stored in
/etc/hosts on your local system, and you can edit those files to point to the local DNS. Also, local DNS entries help in resolving naming conflicts before launching the websites.
For this article, we’ll use:
- A Linux machine with sudo access
- Configuration files in /etc/hosts
- A domain name, hostname, and static IP address.
How to Add a Local DNS Entry on Linux
In this article, we’ll look at how you can set a local DNS entry on a Linux machine. You can perform this step to test websites before taking them live.
We’ll add a local DNS entry using the non-permanent solution in this section. This means the configuration will be gone once the DHCP server is reset. The local DNS entry won’t exist in the configuration file if the system reconnects to the network, the network service is restarted, or the machine is rebooted. So, let’s get started!
1. Check the DNS Order in Name Service Switch
In Linux, there is an essential file that provides Name Service Switch functionality. Its configurations are in
/etc/nsswitch.conf. The Name Service Switch functionality controls the order of execution of DNS and the domain lookup process.
If the domain lookup process is before the DNS, then it will query the /etc/hosts first. However, if the DNA is before the files, then the service will consult DNS first. For this guide, we want to query the files service first. To check this, first, open the terminal by pressing “Ctrl + Alt + T”. After that, type:
# view content on the terminal cat /etc/nsswitch.conf
# search for hosts in the file and view them on terminal grep hosts /etc/nsswitch.conf # view file content in another window less /etc/nsswitch.conf
To change the order, use any text editor to open the file and reorder the configurations. Specifically, type:
# nano text editor sudo nano /etc/nsswitch.conf # vim text editor sudo vi /etc/nsswitch.conf
As we can see from the output, the “files” is already at the top by default.
2. Add New Name Server in Linux
/etc/resolv.conf file using the nano or vi text editor as shown below:
sudo nano /etc/resolv.conf sudo vi /etc/resolv.conf
Next, add the following configurations:
nameserver 127.0.0.1 nameserver 192.168.0.10
Make sure to replace the name server and IP address with your required server and IP. After that, save the file and exit the text editor.
3. Configure /etc/hosts File in Linux to Add a Local DNS Entry
Now we’ll reconfigure the /etc/hosts file to add the local DNS entry. Open the file using your favorite text editor and type:
# vim editor sudo vi /etc/hosts # nano editor sudo nano /etc/hosts
Add both the static IP addresses at the end of this file in this format:
IP-address localhost localhost. localdomain
192.168.0.1 nameserver nameserver DOMAIN
Press “Ctrl + S” to save and “Ctrl + X” to exit the editor.
The output should look something like this:
If you want to know the DNS server and IP address of the host machine, then use the
less command to view the contents of
/etc/resolv.conf file. Specifically, type:
cat /etc/resolv.conf less /etc/resolv.conf
Alternatively, you can do the same from Settings. For this, head to Settings and locate Wi-Fi. Long press the network and choose Modify Network.
After that, locate IP Settings and change it to static. Next, enter the static IP address and click Apply to save the changes.
4. Test the Static Hosts in Linux
Next, use the
ping command to test the hosts. After that, type the hostname directly with the ping command as shown below:
ping -c 4 domain.name ping -c 4 domain
How to Add a Local DNS Entry on Linux Using Network Manager (Permanent Solution)
If you want to add a local DNS entry permanently, use the Network Manager. However, you’ll have to revert it manually if it is no longer required. For this setup, we’ll edit the
1. Add Local DNS Configurations
First, open the file using any text editor as shown below:
sudo nano /etc/dhcp/dhclient.conf sudo vi /etc/dhcp/dhclient.conf
After that, add the following lines at the beginning of the file:
# using network manager prepend domain-name-servers 127.0.0.1; # using systemd /etc/resolv.conf.head nameserver 127.0.0.1
2. Restart Network Manager Service
Since we’ve configured the network manager for the first time, we’ll have to explicitly restart the service. For this step, use the
restart command. Specifically, type:
# using the service sudo service network-manager restart # using the systemd sudo systemctl restart network-manager
3. Prevent Network Manager From Modifying Files
To prevent Network Manager from modifying the contents of
/etc/resolv.conf and let systemd-resolved handle the local DNS entry, we’ll edit the
/etc/NetworkManager/NetworkManager.conf file. For this step, open this file using any text editor:
sudo nano /etc/NetworkManager/NetworkManager.conf sudo vi /etc/NetworkManager/NetworkManager.conf
Now, type the following lines under the [main] section of this file:
Lastly, we’ll restart the systemd-resolved service to implement the changes. We’ll use the systemctl
start commands. Specifically, type:
sudo systemctl stop systemd-resolved sudo systemctl start systemd-resolved
To view the effective settings, use the status option as shown below:
sudo systemd-resolve --status
And that’s a wrap! We’ve covered how you can use both the permanent and nonpermanent solutions to add a local DNS entry on Linux. This will help you in testing your websites and routes before uploading them on server or SSH routes. For more information, check out the systemctl manual.
If this guide helped you, please share it.