How to Configure DNS in Linux

Photo of author
Written by
Photo of author
Verified by
Updated On
— 5 min read

In this blog, we will explore how to configure DNS in Linux, which is crucial for managing domain name and IP address records effectively. The Domain Name System (DNS) acts as a database that helps browsers find the correct IP address for a hostname URL entered into them.

When we want to access a website, we typically type its domain name into the web browser, such as www.google.com, www.wikipedia.com, or www.techarticles.in. However, web browsers require specific IP addresses to load the website’s content. The DNS is responsible for converting domain names to IP addresses, ensuring that resources can be retrieved from the website’s server. Understanding how to configure DNS in Linux will enable you to optimize your network and enhance your server’s performance.

Websites may have many IP addresses linked to a single domain name. Large sites, such as Google, will have users requesting a server from all over the world.

Configure dns in linux

Introduction of Configure DNS in Linux

In this guide, we will explore the following methods for configuring DNS in Linux:

  • Modifying the network card configuration file.
  • Directly editing the DNS configuration file, /etc/resolv.conf.
  • Changing the hosts file for local DNS resolution.
  • Using NetworkManager for dynamic DNS management.


Method 1: Modifying the Network Card Configuration File

To permanently configure DNS settings in Linux, you can modify the network card’s configuration file. This method ensures that your DNS settings persist across reboots.

Step 1: Identify Your Network Card

First, you need to check the name of your network card. Run the following command:

# ip addr

This command will display the configuration of each network interface. Look for the second column, which indicates the name of your network card (e.g., loeth1ens33). For this example, let’s assume your network card’s name is xxxx.

Step 2: Set the Primary DNS

You can use any DNS server provided by your ISP or opt for popular public DNS servers like Google. In this tutorial, we will use Google DNS servers: 8.8.8.8 (primary) and 4.2.2.2 (backup).

To set the primary DNS, run the following command (root access required):

 echo 'DNS1="8.8.8.8" ' >> /etc/sysconfig/network-scripts/ifcfg-xxxx

Step 3: Set the Backup DNS

Next, set the backup DNS server:

echo 'DNS2="4.2.2.2" ' >> /etc/sysconfig/network-scripts/ifcfg-xxxx

Step 4: Restart the Network Service

To apply the changes to your DNS configuration in Linux, restart the network service. The command varies slightly depending on your Linux distribution:

  • For CentOS 7 and RHEL 7:
# systemctl restart network.service
  • For CentOS 8 and RHEL 8, RHEL 9:
# nmcli c up xxx

Step 5: Verify the Configuration

Finally, check if the DNS settings have been applied successfully:

 # cat /etc/resolv.conf

Method 2: Directly alter the /etc/resolv.conf DNS configuration file.

This method allows you to quickly set DNS servers in Linux, but keep in mind that changes made here are temporary and will be lost after a reboot.

Step 1: Add Nameservers

To add DNS servers to the /etc/resolv.conf file, run the following commands:

# echo 'nameserver 223.5.5.5' >> /etc/resolv.conf
# echo 'nameserver 223.6.6.6' >> /etc/resolv.conf 

Step 2: Edit with vi/vim (Optional)

If you prefer to edit the file manually, you can use the vi or vim editor:

# vi /etc/resolv.conf 

Step 3: Verify the Settings

Check if the new DNS settings are in effect:

# cat /etc/resolv.conf

Method 3: Implement local mapping for the prospective domain name.

This method allows you to create static mappings between specific domain names and IP addresses, bypassing DNS lookups for these domains.

Step 1: Edit the Hosts File

Open the hosts file using vi or vim:

# vi /etc/hosts

Step 2: Set IP and Domain Name Correspondence

Add your desired mappings in the following format:

192.168.10.1 www.example.com 

Step 3: Save and Exit

After adding the necessary entries, save the changes and exit the editor.

Method 4: Using NetworkManager for Dynamic DNS Management

For users leveraging NetworkManager, this method allows for dynamic DNS management, making it easy to adjust settings without directly editing configuration files.

Step 1: Open NetworkManager

You can use the command line or a graphical interface to access NetworkManager. To use the command line, run:

# nmcli connection show

Step 2: Modify the Connection

Identify the connection you want to modify and use the following command to set the DNS servers:

# nmcli connection modify <connection-name> ipv4.dns "8.8.8.8 4.2.2.2"

Step 3: Apply the Changes

After modifying the connection, apply the changes with:

# nmcli connection up <connection-name>

Step 4: Verify the DNS Configuration

To ensure that the DNS settings are correctly applied, check the current DNS configuration:

# nmcli device show <device-name> | grep DNS

Conclusion

Configuring DNS in Linux is essential for maintaining effective network communication and ensuring that domain names resolve correctly. By following the methods outlined in this guide, you can easily manage your DNS settings, whether you prefer to modify configuration files directly or utilize tools like NetworkManager for dynamic management. Remember to verify your settings after making changes to ensure everything is functioning as expected.

FAQs about Configuring DNS in Linux

What is DNS and why is it important in Linux?

DNS (Domain Name System) translates domain names into IP addresses, allowing users to access websites using human-readable URLs. Configuring DNS in Linux ensures efficient and accurate network communication.

What are the three methods to configure DNS in Linux?

The three methods to configure DNS in Linux include:
Permanently setting the DNS server address using network configuration files.
Temporarily setting the DNS server address using command-line tools.
Modifying the hosts file to create static mappings between domain names and IP addresses.

How can I permanently configure DNS in Linux?

To permanently configure DNS in Linux, you can edit the /etc/resolv.conf file or use network management tools like NetworkManager or systemd-resolved, depending on your Linux distribution.

How do I temporarily configure DNS in Linux?

You can temporarily configure DNS in Linux by using the nmcli command or by directly editing the /etc/resolv.conf file. However, changes made this way will be lost after a reboot.

What is the hosts file, and how does it relate to DNS?

The hosts file, located at /etc/hosts, is a local file used to map hostnames to IP addresses. Modifying the hosts file allows you to statically configure DNS in Linux for specific domains without relying on external DNS servers.

Reference wiki: https://man7.org/linux/man-pages/man5/resolv.conf.5.html

Related Posts


Share

About Author

Photo of author

Jay

I specialize in web development, hosting solutions, and technical support, offering a unique blend of expertise in crafting websites, troubleshooting complex server issues, and optimizing web performance. With a passion for empowering businesses and individuals online, I provide in-depth reviews, tech tutorials, and practical guides to simplify the digital landscape. My goal is to deliver clear, reliable, and insightful content that helps readers make informed decisions and enhance their online presence.

Leave a Comment