Skip to content

Intermediate · Software and networking · topic 22 of 57 · 35 min

NetworkManager with nmcli, the hostname and name resolution

Explanation

NetworkManager owns the network settings on RHEL. nmcli manages connections: add one with a static address, gateway and DNS, modify it, and bring it up. hostnamectl sets the hostname. Names are looked up in /etc/hosts first and then in DNS, as /etc/nsswitch.conf says.

Devices and connections

NetworkManager is the service that configures networking on RHEL. It separates two ideas. A device is a network interface, like enp1s0. A connection is a saved set of settings (address, gateway, DNS, and so on) that can be applied to a device. A device can have several saved connections, but only one is active at a time. Changes made with ip addr add are temporary; NetworkManager connections are saved and survive reboots.

bash
$ nmcli device status
DEVICE  TYPE      STATE      CONNECTION
enp1s0  ethernet  connected  enp1s0
lo      loopback  unmanaged  --
$ nmcli connection show
NAME    UUID                                  TYPE      DEVICE
enp1s0  8a1d5f6e-3c2b-4e9a-9f10-2b7c1d0e4a55  ethernet  enp1s0
$ nmcli connection show enp1s0 | grep ipv4
ipv4.method:                            auto
ipv4.addresses:                         --
...
ipv4.method auto means DHCP: the address is given by a DHCP server.

A static address

Servers usually need a fixed (static) address so others can find them. Either change the existing connection or add a new one. Use the property names exactly: ipv4.addresses, ipv4.gateway, ipv4.dns, ipv4.method.

bash
# nmcli connection add con-name static-lan ifname enp1s0 type ethernet \
    ipv4.method manual ipv4.addresses 192.168.122.50/24 \
    ipv4.gateway 192.168.122.1 ipv4.dns "192.168.122.1 1.1.1.1" \
    connection.autoconnect yes
Connection 'static-lan' (2f6c...) successfully added.
# nmcli connection up static-lan
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
# nmcli connection modify static-lan +ipv4.dns 8.8.8.8      # + adds, - removes
# nmcli connection modify static-lan ipv4.addresses 192.168.122.51/24
# nmcli connection up static-lan                            # re-apply after modify!

nmcli connection modify only saves the change. The device keeps the old settings until you run nmcli connection up again (or reboot). Also set the old DHCP connection to not autoconnect (nmcli con mod enp1s0 connection.autoconnect no), or the two may fight at boot.

On RHEL 9, connections are stored as keyfiles in /etc/NetworkManager/system-connections/NAME.nmconnection. The older ifcfg files in /etc/sysconfig/network-scripts are deprecated. nmtui is a simple text menu for the same settings, handy when you forget the property names.

Careful: If you are connected over SSH, changing the address of the interface you are using will drop your session. Do it from the console, or be sure of every value before you run nmcli connection up.

The hostname

bash
# hostnamectl hostname web01.example.com      # (set-hostname also works)
$ hostnamectl
 Static hostname: web01.example.com
       Icon name: computer-vm
Operating System: Red Hat Enterprise Linux 9.4 (Plow)
          Kernel: Linux 5.14.0-427.13.1.el9_4.x86_64
$ cat /etc/hostname
web01.example.com
The static hostname is stored in /etc/hostname and survives reboots.

How a name becomes an address

When a program looks up a name, the system reads /etc/nsswitch.conf. On RHEL the hosts line normally says files first, then dns. So /etc/hosts is checked first, and only if the name is not there, the DNS servers in /etc/resolv.conf are asked. NetworkManager writes /etc/resolv.conf from the connection's ipv4.dns settings, so do not edit resolv.conf by hand; your edit will be overwritten.

output
# /etc/hosts
127.0.0.1   localhost localhost.localdomain
::1         localhost localhost.localdomain
192.168.122.50  web01.example.com web01
192.168.122.60  db01.example.com  db01
IP address, then the full name, then short aliases.
bash
$ grep ^hosts /etc/nsswitch.conf
hosts:      files dns myhostname
$ getent hosts db01           # uses the same order programs use
192.168.122.60  db01.example.com db01
$ cat /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 192.168.122.1

Common mistakes

  • Typing ipv4.address (no s) or ipv4.gateway with a prefix. Use Tab completion in nmcli; it completes property names.
  • Forgetting nmcli connection up after modify.
  • Setting ipv4.addresses but leaving ipv4.method auto. Set method manual for a static address.
  • Using dig to test a name that is only in /etc/hosts. dig asks DNS directly and ignores /etc/hosts; getent follows nsswitch.conf.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

NetworkManager with nmcli, the hostname and name resolution · 1/7

device ≠ connection

NetworkManager owns network settings. A device is a card; a connection is a saved set of settings.

Prefer a video by a teacher? Find free videos on “nmcli static ip rhel 9 hostnamectl tutorial” on YouTube ↗

In real life: Moving a college lab to static addresses

A Mangaluru engineering college gives each lab server a fixed address so students can reach them by name. The admin adds a static connection on each server with nmcli, sets hostnames like lab-web01, and adds all lab servers to /etc/hosts as a backup in case the campus DNS server is down during practical exams.

bash
# nmcli con add con-name lab ifname ens3 type ethernet ipv4.method manual ipv4.addresses 172.16.40.11/24 ipv4.gateway 172.16.40.1 ipv4.dns 172.16.0.2
# nmcli con mod ens3 connection.autoconnect no
# nmcli con up lab
# hostnamectl hostname lab-web01.cs.example.edu
# getent hosts lab-web02
172.16.40.12    lab-web02.cs.example.edu lab-web02

Practice (0/3 done)

Task 1 · Question

You ran nmcli connection modify office ipv4.addresses 10.0.0.9/24 but ip addr still shows the old address. What is missing?

Task 2 · Code · Python

Write lookup(hosts_text, name) that searches /etc/hosts text like the system does. Skip empty lines and comments (from # to the end of a line). Each line is an IP followed by one or more names. Return the IP of the FIRST line that lists name (as full name or alias), or None if not found.

Task 3 · Practice

Give your lab VM a static IPv4 address in its current network, the right gateway and a DNS server, using a new connection. Set the hostname to server1.lab.example.com and add your host machine to /etc/hosts as "host". Reboot and check everything stays.

Quick quiz

Question 1

1.Which ipv4.method gives a static address?

Question 2

2.Why should you not edit /etc/resolv.conf by hand on RHEL?

Question 3

3.Which file decides the order "files, then dns" for name lookups?

Question 4

4.Where does RHEL 9 store NetworkManager connections?

Question 5

5.Which tool tests a name the same way programs resolve it, including /etc/hosts?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which ipv4.method gives a static address?

Answer: manual

manual = static; auto = DHCP.

Why should you not edit /etc/resolv.conf by hand on RHEL?

Answer: NetworkManager regenerates it from the connection settings

Set ipv4.dns on the connection instead.

Which file decides the order "files, then dns" for name lookups?

Answer: /etc/nsswitch.conf

nsswitch.conf sets the lookup order.

Where does RHEL 9 store NetworkManager connections?

Answer: /etc/NetworkManager/system-connections/*.nmconnection

RHEL 9 uses keyfiles; ifcfg is deprecated.

Which tool tests a name the same way programs resolve it, including /etc/hosts?

Answer: getent hosts

getent follows nsswitch.conf; dig asks DNS only.

How many usable hosts are in a /24 network?Related

Answer: 254

256 addresses minus network and broadcast.

From Network basics: IP addresses, routes, DNS and the ip command

Users report that connections drop at random, and Windows shows an "IP address conflict" warning. What is the most likely cause?Related

Answer: Two devices have the same IP address

An IP conflict means two devices use the same address, often a static address that is also in the DHCP pool.

From Validating addresses and checking the same subnet

Which flag sets the port for scp?Related

Answer: -P

scp uses capital -P for the port; ssh uses lower-case -p.

From Linux networking for DevOps

Which port does classic DNS use?Related

Answer: 53

DNS uses port 53 (UDP mostly, TCP for big answers). DoT uses 853 and DoH uses 443.

From DNS: the internet's phone book

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Software and networking” before its exam: firewalld: zones, services and ports.
  2. Module 4 exam: Software and networking · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Storage: partitions, file systems and LVM. 57 topics left in the course before the final exam.

← Previous: Network basics: IP addresses, routes, DNS and the ip command