Changing DNS server configuration can be very important. A switch in DNS can aid a server or user’s privacy, fix resolution issues, or allow you to use a self-created DNS server so you have full control over the process. Unfortunately, though, the process isn’t the same across all Linux distros, leading to some confusion. Today we’re going to be focusing on a DNS switch in CentOS 7, which naturally differs quite a bit from Ubuntu but has less guidance available.
CentOS stores network config files conveniently in /etc/sysconfig/network-scripts/. Here you’ll find options to configure your IP, gateway, and DNS server and will find information like the hardware address and UUID. You’ll need a couple of pre-requisites before we jump into the guide, which will focus on setup on a VPS:
- A CentOS VPS, preferably CentOS 7
- The VPS credentials, which should be found in your provider’s control panel
- PuTTy or another SSH client for VPS connection
- The nano text-editor or familiarity with vi
Change a DNS Server on a CentOS 7 VPS
Connect to your VPS by entering its IP address in PuTTY’s ‘Host Name’ field and clicking ‘Open’.
Enter the username and password and press Enter to connect to the Linux environment.
First, install nano with sudo yum nano. We’re going to be using it as a text editor in a tutorial, but you can replace “nano” with “vi” on each command if you’d prefer.
You can find general network configuration for a specific adaptor with the command:
Simply edit the fields that are required, such as IPADR. You may have additional lines in your config file, with a DNS showing as:
These values may cause issues if you edit your DNS via resolv.conf, which is the recommended method that we’ll be covering in a little bit. Keeping them may result in changes to resolv.conf being reset to these values on a reboot. As a result, you may want to remove these lines. Additionally, some network interfaces use DHCP for activation which would also cause the DNS in resolv.conf to be overwritten. To prevent this, you can add the following to ifcfg-eth0:
Once complete press Ctrl+O to write out the changes and Ctrl+X to exit nano, or Shift+zz to save and quit with vi.
We can now configure your DNS server with:
Change the nameserver values to your preference, in our case to Cloudflare. Press Ctrl + O to save and Ctrl + X to exit nano or Shift+zz to save and exit in vi.
You can use a similar technique to edit the gateway independently. Type nano
/etc/sysconfig/network and edit the gateway if it’s located there instead.
If your configuration files contain warnings about the modification of Cloud-init files, you may want to follow the steps here to disable Cloud-init’s network configuration capability.