A Beginner’s Guide to Using DNS in Linux

Published August 24, 2024
A Beginner's Guide to Using DNS in Linux
Cheap Dedicated Server

A Beginner’s Guide to Using DNS in Linux


 

DNS (Domain Name System) is a fundamental component of the internet that translates human-friendly domain names (like example.com) into IP addresses (like 192.0.2.1). If you’re using Linux, understanding how to manage DNS settings can be crucial for troubleshooting network issues, improving performance, or configuring custom DNS settings. In this guide, we’ll walk you through the basics of using DNS in Linux, covering everything from checking your current DNS settings to changing them.

1. Understanding DNS in Linux

In Linux, DNS settings are typically managed through the /etc/resolv.conf file, which contains the nameservers that your system will use for DNS resolution. When you type a domain name in your browser or use a command like ping or curl, your system will refer to these nameservers to resolve the domain to an IP address.

2. Checking Current DNS Settings

To see which DNS servers your Linux system is currently using, you can simply view the contents of the /etc/resolv.conf file.

Open a terminal and run:

cat /etc/resolv.conf

 Checking Current DNS      

This command will display something like:

Checking Current DNS output

 

nameserver 8.8.8.8

nameserver 8.8.4.4

 

In this example, the system is using Google’s public DNS servers.

3. Changing DNS Settings

There are several ways to change DNS settings on a Linux system, depending on your distribution and networking setup. Below, we’ll cover the two most common methods: directly editing /etc/resolv.conf and using Network Manager.

Method 1: Editing /etc/resolv.conf Directly

You can manually edit the /etc/resolv.conf file to change your DNS servers. However, keep in mind that this file might be overwritten by network management tools, so changes may not persist across reboots.

To edit the file, use a text editor like nano:

use a text editor like nano

sudo nano /etc/resolv.conf

 

Then, update the file with the nameservers you want to use:

update the file with the nameservers

nameserver 1.1.1.1

nameserver 1.0.0.1

Save and exit the editor. Your system will now use the specified DNS servers.

Method 2: Using Network Manager

If your Linux distribution uses Network Manager, you can change DNS settings through the graphical interface or the command line.

Using the GUI:

  1. Open the Network Manager from your system settings.
  2. Select the network connection you want to modify (e.g., Wi-Fi or Ethernet).
  3. Go to the “IPv4 Settings” or “IPv6 Settings” tab.
  4. Set the “Method” to “Automatic (DHCP) addresses only” or “Manual” if you want to specify both IP and DNS manually.
  5. Enter the desired DNS servers in the “DNS servers” field, separated by commas.
  6. Save the changes and restart the network connection.

Using the Command Line:

Network Manager can also be configured via the command line using the nmcli tool.

To set DNS servers for a specific connection:

To set DNS servers for a specific connection

sudo nmcli con mod <connection_name> ipv4.dns "1.1.1.1 1.0.0.1"

sudo nmcli con up <connection_name>

Replace <connection_name> with the name of your network connection (you can list connections with nmcli con show).

4. Flushing DNS Cache

If you change DNS settings or suspect that your system is caching outdated DNS records, you may need to flush the DNS cache. Different distributions have different ways to do this:

  • Systemd-based systems (e.g., Ubuntu, Fedora):
    Systemd-based systems (e.g., Ubuntu, Fedora)
    sudo systemd-resolve --flush-caches
     
  • DNSmasq:
    DNSmasq
    sudo systemctl restart dnsmasq
     
  • nscd (Name Service Cache Daemon):
    nscd (Name Service Cache Daemon)

    sudo systemctl restart nscd

5. Troubleshooting DNS Issues

If you’re experiencing DNS issues, here are a few steps you can take to troubleshoot:

  1. Check /etc/resolv.conf: Ensure that it contains valid nameservers.
  2. Ping the nameservers: Use ping or dig to check connectivity to the nameservers.
  3. Restart your network: Sometimes simply restarting the network interface or the Network Manager can resolve issues.
  4. Check firewall settings: Ensure that DNS queries (typically on port 53) are not being blocked by your firewall.

Conclusion

Managing DNS settings in Linux is a fundamental skill that can help you solve network issues and optimize your internet experience. Whether you prefer using command-line tools or a graphical interface, Linux provides you with flexible options to control how your system interacts with the DNS. By understanding and applying the concepts in this guide, you can ensure that your Linux system is always pointing to the right DNS servers.

 


 

A Beginner’s Guide to Using DNS in Linux

 

How do I check which DNS server my Linux system is using?

To check the DNS servers your Linux system is currently using, you can view the /etc/resolv.conf file by running the following command in the terminal:

cat /etc/resolv.conf

This will list the nameservers your system is using for DNS resolution.

 

How can I permanently change the DNS server in Linux?

To permanently change the DNS server in Linux, you can use Network Manager, which will ensure that your settings persist across reboots:

  • GUI Method: Go to Network Manager, select your network, and set the DNS servers in the “IPv4 Settings” or “IPv6 Settings” tab.

  • Command-Line Method: Use nmcli to configure DNS for a specific connection:

     
    sudo nmcli con mod <connection_name>
    ipv4.dns "1.1.1.1 1.0.0.1"
    sudo nmcli con up <connection_name>

How do I flush the DNS cache in Linux?

Flushing the DNS cache depends on the caching service your system uses:

  • For systemd-resolved, use:
     
    sudo systemd-resolve --flush-caches
  • For DNSmasq, restart it with:
     
    sudo systemctl restart dnsmasq
  • For nscd (Name Service Cache Daemon), restart it with:
     
    sudo systemctl restart nscd

What should I do if my DNS changes don’t take effect?

If your DNS changes don’t seem to be working, try the following troubleshooting steps:

  1. Check /etc/resolv.conf: Ensure the file contains the correct nameservers.
  2. Restart Network Manager: Run sudo systemctl restart NetworkManager.
  3. Flush DNS cache: Depending on your system, flush the DNS cache to clear old records.
  4. Verify connection: Ensure your system can connect to the specified DNS servers using ping or dig.