How to change DNS settings on CentOS 7

Published: August 8, 2017 (Updated: Aug 8, 2017)

Enjoying this content? Subscribe to the Channel!

Skip the DNS Server: How to Manually Set DNS on CentOS 7 (The Easy Way)


Welcome back to Darren’s Tech Tutorials! If you’re managing a small-scale server environment, you know that efficiency is key. While robust DNS servers are crucial for large networks, deploying a full-fledged DNS service might be total overkill for just a handful of servers. Why add complexity where you don’t need it?

Today, we’re diving into a simple, effective solution: manually setting the DNS server configuration directly on your CentOS 7 machines. This method is fast, reliable, and perfect for getting your small setup talking to the internet and to each other without unnecessary overhead.

Let’s get started!

Why Set DNS Manually on CentOS 7?

The core value of this technique lies in its simplicity. If you are running a virtual lab, a small home server rack, or an environment with fewer than five systems, dedicating resources and configuration time to a separate DNS server just doesn’t make sense.

By manually editing a single configuration file on CentOS 7, you tell the operating system exactly where to look for domain name resolution. This provides immediate network connectivity without requiring you to manage an additional service. It’s clean, direct, and avoids potential headaches.


Prerequisites: Getting Ready to Edit

To perform this configuration, you only need command-line access to your CentOS 7 server and an understanding of a basic text editor. We recommend vi, which is universally available on almost all Linux distributions.

Quick Links:

The Step-by-Step Guide: Configuring DNS with /etc/resolv.conf

On CentOS 7 (and most RHEL-based systems), the file responsible for resolving domain names is /etc/resolv.conf. This is where we will specify the IP addresses of the DNS servers we want our machine to use.

Note: If you are running NetworkManager or have specific networking services enabled, they might overwrite this file automatically upon reboot. We will address potential persistence issues below, but for a quick setup, this is the file you need.

Step 1: Open the DNS Configuration File

Use vi (or your preferred command-line editor like nano) to open the resolv.conf file with root privileges:

sudo vi /etc/resolv.conf

Step 2: Review and Clear Existing Entries

When you open the file, you might see existing entries, usually pointing to the internal network gateway or loopback address if DHCP was previously used. To ensure a clean configuration, it is best to remove or comment out any lines that conflict with your intended DNS servers.

Step 3: Add Your DNS Server Entries

The configuration is straightforward: we use the nameserver directive followed by the IP address of the DNS server. It is best practice to include at least two servers for redundancy.

Enter the insert mode in vi (press the i key) and add your server lines.

Example using Google Public DNS and Cloudflare DNS:

nameserver 8.8.8.8
nameserver 1.1.1.1

If you are using an internal DNS server (like one provided by your router or domain controller), replace the public IPs with your internal server’s IP address.

Step 4: Save and Exit the File

Once you have added your desired nameserver lines:

  1. Press the Esc key to exit insert mode.
  2. Type :wq (write and quit).
  3. Press Enter.

The configuration is now saved! Unlike some networking changes in older Linux systems, the changes to /etc/resolv.conf are usually applied instantly.

Step 5: Test Your DNS Configuration

To confirm that your CentOS 7 server is now properly resolving domain names, use the ping command:

ping google.com

If you see successful replies and the domain name is resolved to an IP address, congratulations! Your manual DNS setup is working perfectly. If you receive an error like “Name or service not known,” double-check the IP addresses you entered in Step 3.

Summary and Next Steps

That’s all there is to it! By making a quick manual edit to the /etc/resolv.conf file, you have successfully configured DNS resolution on your CentOS 7 server, keeping your small environment streamlined and efficient.

This method is quick, dirty, and exactly what you need when a complex DNS service would just be overkill.

Did this tutorial help you get connected? Let us know in the comments below! If you found this guide valuable, please give this post a Like, and be sure to Subscribe to Darren’s Tech Tutorials for more clear, practical technology guides just like this one.

Happy serving!