change dns linux with code examples

Domain Name System (DNS) is an important part of the internet infrastructure. It acts as a directory, facilitating the translation of domain names into IP addresses. DNS servers are responsible for resolving these domain names into the corresponding IP addresses so that devices can connect and communicate with one another.

There are many reasons why you may need to change the DNS server used by your Linux system. Maybe you want to improve browsing speed, or you need to access certain websites that are only accessible through specific DNS servers. Whatever reason you may have, changing the DNS in Linux is a simple process.

In this article, we will explore some methods for changing DNS servers in Linux, with examples of code snippets, starting with the use of network manager.

Method 1: Use the Network Manager

Network Manager is a powerful tool in Linux that allows you to manage your network settings. You can use this tool to change your DNS server settings in a few simple steps.

Step 1: Open the Network Manager

To open up the Network Manager, click on the network icon located on the top right of the screen, then select the 'Edit Connections' option.

Step 2: Select the connection to be modified

Next, select the connection you want to modify from the list that appears. This may be a wired or wireless connection.

Step 3: Edit the Network Settings

In the settings window that appears, navigate to the 'IPv4' or 'IPv6' tab, depending on the type of connection you're looking to modify. Here you can enter the new DNS server addresses you want to use.

If you're making the changes as a server, you can use the below command in your terminal:

sudo nano /etc/systemd/resolved.conf

In this file, you will see a section that reads [Resolve]. You can then modify this section to include your preferred DNS server address.

For example, say you want to use OpenDNS servers, you could modify the file to look like this:

[Resolve]
DNS=208.67.222.222 208.67.220.220

Once you've made the changes, save the file and exit the text editor.

Step 4: Apply Changes

Once you've made the changes, click on the 'Save' button to apply them. This will save the new DNS server settings and exit the Network Manager.

Method 2: Edit the /etc/resolv.conf file

Another way to change the DNS server settings on your Linux machine is by editing the /etc/resolv.conf file. This is the configuration file responsible for setting the DNS servers used by your system.

You can modify this file in two ways. One, directly from your terminal, or two, by installing NetworkManager-tui.

Option 1: By using terminal

To edit the file directly from the terminal, you could use the below command:

sudo nano /etc/resolv.conf

Once the file opens, you can add the new DNS servers that you want to use. You can do this by adding the IP addresses of the DNS servers.

For example, if you want to use Google's public DNS servers, you would add the following lines to the file:

nameserver 8.8.8.8
nameserver 8.8.4.4

Option 2: By using NetworkManager-tui

Alternatively, if you're working on a server and don't want to use the GUI, you could install and use NetworkManager-tui.

To install, you could use the below commands:

sudo yum install NetworkManager-tui (For RHEL and CentOS distributions)
sudo apt-get install network-manager-gnome (For Ubuntu and Debian distributions)

Once you've installed it, use the below command to open it:

sudo nmtui

This will open up the Network Manager TUI dashboard. Here you can edit the DNS settings by navigating to the 'Edit connections' option and selecting the connection you want to modify. Once you're in the editing screen, select the 'IPv4' or 'IPv6' tab depending on your requirements, and enter the DNS server IP addresses you want to use.

Conclusion

In summary, changing DNS servers in Linux is a simple process that can be done in a few steps. You can use the Network Manager to make the changes easily or edit the configuration files directly if you're working on a server. When changing DNS servers, it's important to choose reliable DNS servers, like those provided by Google, OpenDNS, or Cloudflare, to ensure your internet browsing remains secure and uninterrupted.

let me elaborate further on the topics discussed in the previous article.

The Importance of DNS in Linux

As mentioned, DNS is an integral part of the internet infrastructure, and it plays a crucial role in facilitating the translation of domain names into IP addresses. Since IP addresses are used by internet devices to locate and communicate with each other, DNS servers help in identifying the correct IP address for the requested domain name.

Linux systems offer various options for configuring DNS servers, including methods that allow you to change which servers are being used. This ability is particularly useful when you need to access websites that are not accessible through your local network's DNS servers. By changing the DNS server used by your Linux system, you can improve browsing speed, reliability, and security.

Method 1: Using the Network Manager

The Network Manager is a tool present in most Linux distributions that enables easy configuration of network settings. This tool helps you change your DNS server effortlessly using a few simple steps:

  1. Open the Network Manager: To open the Network Manager, click on the network icon on the top right of your screen and select the 'Edit Connections' option.

  2. Select the Connection to be Modified: From the list that appears, choose the connection you want to modify.

  3. Edit the Network Settings: In the settings window that opens, locate the 'IPv4' or 'IPv6' section, depending on the connection type. Here, you can enter the new DNS server addresses you want to use.

You can also modify the /etc/systemd/resolver.conf file (in server mode), which can be done by running the command sudo nano /etc/systemd/resolved.conf. Here you can enter the DNS server addresses you want to use, save the file and exit.

Method 2: Editing the /etc/resolv.conf File

Another method of changing DNS servers in Linux is by editing the /etc/resolv.conf file directly. This is the file that sets the DNS servers used by your system, and it can be edited in two ways:

  1. Edit the file Directly: Open your terminal and run the command sudo nano /etc/resolv.conf. Once the file opens, you can add the DNS servers (IP addresses) you want to use. For example:
nameserver 8.8.8.8
nameserver 8.8.4.4
  1. Install NetworkManager-tui: For servers, you could install the NetworkManager-tui by running the command sudo yum install NetworkManager-tui for Red Hat or CentOS-based distros or sudo apt-get install network-manager-gnome for Debian-based distributions. Once installed, you can use the sudo nmtui command to access the Network Manager TUI and edit the DNS settings.

Conclusion

Changing DNS servers on a Linux system is a straightforward process that can be done through various methods. Each of the methods described above offers different benefits, and you can use the one that best suits your requirements. Changing DNS servers can help improve your internet speed, security, and reliability, so always ensure that you choose reliable DNS servers to use on your system.

Popular questions

  1. What is DNS, and why is it important in Linux?
    DNS (Domain Name System) essentially acts as a directory that translates domain names into IP addresses. It is crucial in Linux because it allows devices to connect and communicate with one another. By changing the DNS on your Linux system, you can improve browsing speed, security, and access to certain websites.

  2. What is the Network Manager, and how can it be used to change DNS servers in Linux?
    The Network Manager is a tool present in most Linux distributions that simplifies network configuration tasks. By opening the Network Manager and selecting the connection you want to modify, you can enter the new DNS servers you want to use, making it an easy and efficient way to change DNS servers in Linux.

  3. How can the /etc/resolv.conf file be used to change DNS servers on Linux systems?
    The /etc/resolv.conf file on Linux controls the system's DNS settings. To change DNS servers using this method, you can use a text editor to open the file using: sudo nano /etc/resolv.conf. Then, add the IP addresses of the DNS servers you want to use, save the file, and exit.

  4. Which command can be used to install the Network Manager TUI on Linux distros?
    The command for installing the Network Manager TUI differs depending on the Linux distribution you're using. For Red Hat or CentOS-based distributions, you can use the command sudo yum install NetworkManager-tui. For Debian-based distributions, use sudo apt-get install network-manager-gnome.

  5. What is the importance of choosing reliable DNS servers?
    Choosing reliable DNS servers is crucial because it ensures that your internet browsing remains secure and uninterrupted. Additionally, some DNS servers are known to be faster and more reliable than others, so it's essential to choose a trustworthy DNS provider to ensure optimum results when browsing.

Tag

DNSConfig

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top