How to Setup Local DNS Using /etc/hosts File in Linux

DNS (Domain Name System or Service) is a hierarchical decentralized naming system/service that translates domain names into IP addresses on the Internet or a private network and a server that provides such a service is called a DNS server.

This article explains, how to setup a local DNS using the hosts file (/etc/hosts) in Linux systems for local domain resolution or testing the website before taking live.

For example, you may want to test a website locally with a custom domain name before going live publicly by modifying the /etc/hosts file on your local system to point the domain name to the IP address of the local DNS server you configured.

The /etc/hosts is an operating system file that translate hostnames or domain names to IP addresses. This is useful for testing websites changes or the SSL setup before taking a website publicly live.

Attention: This method will only work if the hosts have a static IP address. Therefore ensure that you have set static IP addresses for your Linux hosts or nodes running other operating systems.

For the purpose of this article, we will be using the following domain, hostnames and IP addresses (use values that apply to your local setting).

Domain:     tecmint.lan
Host 1:     ubuntu.tecmint.lan	 192.168.56.1
Host 2:     centos.tecmint.lan	 192.168.56.10

Understanding Name Service Switch in Linux

Before moving any further, you should understand a few things about another important file that is /etc/nsswitch.conf. It provides Name Service Switch functionality which controls the order in which services are queried for name service lookups.

The configuration is based on order; if files is before dns it means the system will query the /etc/hosts file before checking DNS for name service requests. But if DNS is before files then the domain lookup process will consult DNS first before any other appropriate services or files.

In this scenario, we want to query the “files” service. To check the order, type.

$ cat /etc/nsswitch.conf
OR
$ grep hosts /etc/nsswitch.conf
Check Name Service Switch
Check Name Service Switch

Configure DNS Locally Using /etc/hosts File in Linux

Now open the /etc/hosts file using your editor of choice as follows

$ sudo vi /etc/hosts

Then add the lines below to the end of the file as shown in the screen shot below.

192.168.56.1   ubuntu.tecmint.lan
192.168.56.10  centos.tecmint.lan

Next, test if everything is working well as expected, using the ping command from Host 1, you can ping Host 2 using it domain name like so.

$ ping -c 4 centos.tecmint.lan 
OR
$ ping -c 4 centos
Ping Domain Locally
Ping Domain Locally

On the Host 2, we have setup Apache HTTP server. So we can also test if the name translation service is working as follows by going to URL http://centos.tecmint.lan.

Check Domain Locally
Check Domain Locally

Important: To use the domain names on any host on the network, you must configure the above settings in its /etc/hosts file.

What does this mean, in the above example, we only configured the hosts file of Host 1 and we can only use the domain names on it. To use the same names on Host 2, we have to add the addresses and names to its hosts file as well.

Lastly, you should use host command or nslookup command to test if the name translation service is actually working, these commands only query DNS and overlook any configurations in /etc/hosts and /etc/nsswitch.conf files.

You may also like to read these following related articles.

  1. Install and Configure Caching-Only DNS Server in RHEL/CentOS 7
  2. Setup a Basic Recursive Caching DNS Server and Configure Zones for Domain
  3. 8 Linux Nslookup Commands to Troubleshoot DNS (Domain Name Server)
  4. Useful ‘host’ Command Examples for Querying DNS Lookups

That’s it! Do share any additional thoughts or questions about this topic with us, via the comment section below.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 thoughts on “How to Setup Local DNS Using /etc/hosts File in Linux”

  1. Linux does provide the bind RPM package to allow an administrative domain to configure and maintain its own DNS service, but frequently the size of the local network is only a few hosts and makes justifying the effort of maintaining a DNS service unwarranted.

    Reply
  2. Hello Aaron,

    I’m sad to say that in above article you did not describe DNS at all. You instead described how to configure local name resolution on a Linux host through /etc/hosts. In this scenario, DNS is not involved at all.

    Think of OS name resolution like an umbrella. In nsswitch.conf the sysadmin configures what name resolution services for this system should be. Files usually come first (means /etc/hosts), DNS is one of them – it usually follows files but there are a lot more different name resolution services and strategies available.

    getent hosts ” query the OS to resolve according to configured name resolution services in that order (and usually checks /etc/hosts firsts and then maybe DNS, but always does what’s written in nsswitch.conf behind “hosts:”)

    host” explicitly asks DNS service to resolve. This won’t resolve hostnames that only /etc/hosts can resolve – to demonstrate the difference.

    Please read the man pages of “getent” and “hosts“.

    I suggest completely overhaul this article or take it off-line.

    Greetings,
    Joerg

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.