On CentOS 7 systems managed by NetworkManager, DNS settings are typically controlled through nmcli
rather than direct file editing. While manual edits to /etc/resolv.conf
or interface configuration files might work temporarily, they can be overwritten by NetworkManager.
First, let's verify the current DNS settings:
nmcli device show eth0 | grep IP4.DNS
cat /etc/resolv.conf
There are two primary methods to change DNS settings:
Method 1: Modifying Connection Profile
The most reliable approach is to modify the connection profile:
nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection up eth0
Method 2: Using Interactive Mode
For more complex configurations, use interactive mode:
nmcli connection edit eth0
set ipv4.dns 8.8.8.8 8.8.4.4
save
activate
quit
To prevent DHCP from overwriting your DNS settings:
nmcli connection modify eth0 ipv4.ignore-auto-dns yes
nmcli connection up eth0
Confirm your new DNS settings are active:
nmcli device show eth0 | grep DNS
systemd-resolve --status
- If changes don't appear in
/etc/resolv.conf
, check if NetworkManager is managing it - For temporary testing, use
resolvconf
or manually edit/etc/resolv.conf
- Remember that changes might take a few seconds to propagate
For systems not using NetworkManager, you can edit the interface configuration file:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# Add these lines:
DNS1=8.8.8.8
DNS2=8.8.4.4
PEERDNS=no
Then restart networking:
systemctl restart network
When working with CentOS 7 (especially in virtualized environments like VirtualBox), you'll often find that NetworkManager automatically generates the /etc/resolv.conf
file. The default configuration typically looks like this:
$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.0.2.3
Instead of manually editing network configuration files, you can use nmcli
(NetworkManager's command-line tool) to manage DNS settings more effectively. Here's how to modify DNS servers for your active connection:
# List all connections
nmcli connection show
# Modify DNS for your active connection (replace 'eth0' with your connection name)
nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
# To make changes persistent and apply immediately:
nmcli connection up eth0
After applying the changes, verify that they took effect:
# Check the connection details
nmcli connection show eth0 | grep ipv4.dns
# Verify resolv.conf
cat /etc/resolv.conf
For more complex setups, you might want to configure DNS search domains or set DNS priority:
# Add search domains
nmcli connection modify eth0 ipv4.dns-search "example.com"
# Set DNS options (like timeout)
nmcli connection modify eth0 ipv4.dns-options "timeout:1"
# Make DNS changes survive reboots
nmcli connection modify eth0 ipv4.ignore-auto-dns yes
If changes don't appear in /etc/resolv.conf
, check if NetworkManager is managing it:
# Check if resolv.conf is managed
ls -l /etc/resolv.conf
# If it's a symlink to another location (like /run/NetworkManager/resolv.conf),
# your changes should still work as NetworkManager will use them internally
Remember that after making changes, you need to either restart the network connection or NetworkManager service for changes to take effect:
# Restart NetworkManager
systemctl restart NetworkManager
# Or bring the connection down and up
nmcli connection down eth0 && nmcli connection up eth0