Learn about the differences between NMCLI and NMTUI, two powerful tools for managing network connections on Linux systems, and discover which one is best suited
In this article, we will review how to change DNS settings using nmcli. In RHEL7 and CentOS7, modifying the ifcfg scripts or /etc/resolv.conf files directly won’t work as they are overwritten by the NetworkManager.
The nmcli command-line (CLI) tool is used for controlling the NetworkManager. It not only displays the network device status, but can also create, edit, activate/deactivate, and delete network connections. The following command (below) shows how to change DNS settings using the nmcli utility.
Run the following command (below) to show all the available network connections or display the network interface card(s) (NIC) on your RHEL7/CentOS7 machine.
$ nmcli con show
NAME UUID TYPE DEVICE
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
In this example (above) the eth0 is displayed.
The nmcli con show eth0
command will show even more detailed information (below):
$ nmcli con show eth0
connection.id: eth0
connection.uuid: 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: eth0
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0 (default)
connection.auth-retries: -1
connection.timestamp: 1650169927
connection.read-only: no
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1 (default)
connection.secondaries: --
connection.gateway-ping-timeout: 0
connection.metered: unknown
connection.lldp: default
connection.mdns: -1 (default)
connection.llmnr: -1 (default)
802-3-ethernet.port: --
802-3-ethernet.speed: 0
802-3-ethernet.duplex: --
802-3-ethernet.auto-negotiate: no
802-3-ethernet.mac-address: --
802-3-ethernet.cloned-mac-address: --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist: --
802-3-ethernet.mtu: auto
802-3-ethernet.s390-subchannels: --
802-3-ethernet.s390-nettype: --
802-3-ethernet.s390-options: --
802-3-ethernet.wake-on-lan: default
802-3-ethernet.wake-on-lan-password: --
ipv4.method: auto
ipv4.dns: --
ipv4.dns-search: --
ipv4.dns-options: ""
ipv4.dns-priority: 0
ipv4.addresses: --
ipv4.gateway: --
ipv4.routes: --
ipv4.route-metric: -1
ipv4.route-table: 0 (unspec)
You can grep for dns to only show DNS-related information.
$ nmcli con show eth0 | grep dns
connection.mdns: -1 (default)
ipv4.dns: --
ipv4.dns-search: --
ipv4.dns-options: ""
ipv4.dns-priority: 0
ipv4.ignore-auto-dns: no
ipv6.dns: --
ipv6.dns-search: --
ipv6.dns-options: ""
ipv6.dns-priority: 0
ipv6.ignore-auto-dns: no
Before we change our DNS settings, we’ll check the contents of the /etc/resolv.conf file. The /etc/resolv.conf file is where the resolver parameters are stored when changes are made to the DNS on your Linux machine.
$ cat /etc/resolv.conf
search dev.infotechys.com
nameserver 192.168.1.254
We can see (above) what the NetworkManager stores in this file by default. We’ll add google’s nameservers 8.8.8.8
and 8.8.4.4
to demonstrate how to modify DNS settings.
To add nameservers to the /etc/resolv.conf
run the following:
$ sudo nmcli connection modify eth0 ipv4.dns "8.8.8.8,8.8.4.4"
As you can see (below) the dns entries were added successfully.
$ nmcli con show eth0 | grep dns
connection.mdns: -1 (default)
ipv4.dns: 8.8.8.8,8.8.4.4
ipv4.dns-search: --
ipv4.dns-options: ""
ipv4.dns-priority: 0
ipv4.ignore-auto-dns: no
ipv6.dns: --
ipv6.dns-search: --
ipv6.dns-options: ""
ipv6.dns-priority: 0
ipv6.ignore-auto-dns: no
As previously mentioned, the nmcli interactive editor can be used to create, edit, activate/deactivate, and delete entries or network connections. In this case however, we will use it to undo the DNS changes we just made.
Run the following command (below) to launch the interactive editor.
$ sudo nmcli con edit eth0
===| nmcli interactive connection editor |===
Editing existing '802-3-ethernet' connection: 'eth0'
Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [.]' for detailed property description.
You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, tc, proxy
nmcli>
At the interactive prompt (shown below), you can remove the dns entries by issuing the following command:
nmcli> remove ipv4.dns
To verify the entries are removed, run this command (below):
nmcli> print ipv4.dns
ipv4.dns:
Then, ensure the changes persist by issuing the save command and finally, quit the interactive prompt (below):
nmcli> save
Connection 'eth0' (5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03) successfully updated.
nmcli> quit
Once again, changes will not be visible in the /etc/resolv.conf file until we restart the NetworkManager service.
$ cat /etc/resolv.conf
# Generated by NetworkManager
search dev.infotechys.com
nameserver 192.168.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
$ sudo systemctl restart NetworkManager
$ cat /etc/resolv.conf
# Generated by NetworkManager
search dev.infotechys.com
nameserver 192.168.1.1
As you can see (above), the original DNS settings have been successfully restored.
In our exploration of DNS management with the nmcli utility, we’ve delved into a versatile tool that not only displays network connections but also enables seamless modification of nameserver entries. With nmcli, Linux administrators and engineers can efficiently navigate through network configurations, inspecting existing settings and making necessary adjustments with ease. The interactive editor feature of nmcli further enhances this process, providing a user-friendly interface for modifying DNS entries directly from the command line.
Was this article helpful to you? If so, leave us a comment below and share!
Related Posts
Learn about the differences between NMCLI and NMTUI, two powerful tools for managing network connections on Linux systems, and discover which one is best suited
Discover the convenient and efficient way to change IPv4 addresses on your Linux machine using the nmcli utility, and take control of your network settings
In this article, we’ll show you how to quickly add multiple IP addresses using NMCLI, a powerful command-line tool for configuring network settings on RHEL8/9