nmcli: static IP, DNS, gateway
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- A device (
ens18) is a physical interface, a connection is the settings profile applied to it. nmcli con mod ens18 ipv4.method manual ipv4.addresses 10.20.0.15/24 ipv4.gateway 10.20.0.1 ipv4.dns 10.20.0.1— a static IP.- The change is written to the profile;
nmcli con up ens18is needed to apply it. +ipv4.addressesadds a second address;connection.autoconnect yesmakes it come up at boot.- On RHEL 9 profiles live in
/etc/NetworkManager/system-connections/*.nmconnection;nmtuiis a text menu.
Device and connection
[ali@srv-db01 ~]$ nmcli dev status DEVICE TYPE STATE CONNECTION ens18 ethernet connected ens18 lo loopback connected (externally) lo [ali@srv-db01 ~]$ nmcli con show NAME UUID TYPE DEVICE ens18 3c8a1f2e-7b4d-4e6a-9c1b-2d3e4f5a6b7c ethernet ens18 lo 9f8e7d6c-5b4a-4392-8170-6f5e4d3c2b1a loopback lo
| Term | What it is | Example |
|---|---|---|
| Device | A physical or virtual interface | ens18 |
| Connection | A settings profile: IP, DNS, routes | ens18 (the name may or may not match the device) |
One device can have several profiles (for example "office" and "lab"), but only one is active at a time. All nmcli con mod commands apply to the profile.
Current settings
[ali@srv-db01 ~]$ nmcli -f ipv4.method,ipv4.addresses,ipv4.gateway,ipv4.dns,connection.autoconnect con show ens18 ipv4.method: auto ipv4.addresses: -- ipv4.gateway: -- ipv4.dns: -- connection.autoconnect: yes [ali@srv-db01 ~]$ ip -br a show ens18 ens18 UP 10.20.0.20/24 fe80::be24:11ff:fe5e:19a3/64
method: auto — DHCP. The server got its address from DHCP.
A static IP
[ali@srv-db01 ~]$ sudo nmcli con mod ens18 \ ipv4.method manual \ ipv4.addresses 10.20.0.20/24 \ ipv4.gateway 10.20.0.1 \ ipv4.dns "10.20.0.1 1.1.1.1" \ ipv4.dns-search itnuvo.local \ connection.autoconnect yes [ali@srv-db01 ~]$ sudo nmcli con up ens18 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4) [ali@srv-db01 ~]$ ip route show default default via 10.20.0.1 dev ens18 proto static metric 100 [ali@srv-db01 ~]$ grep nameserver /etc/resolv.conf nameserver 10.20.0.1 nameserver 1.1.1.1
| Parameter | Meaning |
|---|---|
ipv4.method manual | Static IP (auto — DHCP) |
ipv4.addresses | IP/mask; several separated by commas |
ipv4.gateway | Default gateway |
ipv4.dns | DNS servers, separated by spaces |
ipv4.dns-search | Search domain |
connection.autoconnect yes | Come up automatically at boot |
Careful: if you write ipv4.method manual without giving ipv4.addresses, con up fails. Also on RHEL (unlike Ubuntu) NetworkManager writes /etc/resolv.conf itself — do not edit it by hand, use ipv4.dns.
When changing remotely over SSH: after changing the IP, con up drops the connection. Be ready to connect to the new address, or run nmcli con mod ... && nmcli con up ... on one line inside tmux.
A second address and removing it
[ali@srv-db01 ~]$ sudo nmcli con mod ens18 +ipv4.addresses 10.20.0.21/24 [ali@srv-db01 ~]$ sudo nmcli con up ens18 [ali@srv-db01 ~]$ ip -br a show ens18 ens18 UP 10.20.0.20/24 10.20.0.21/24 fe80::be24:11ff:fe5e:19a3/64 [ali@srv-db01 ~]$ sudo nmcli con mod ens18 -ipv4.addresses 10.20.0.21/24
+ adds, - removes, and without a sign it replaces. The same rule applies to ipv4.dns and ipv4.routes. A static route: nmcli con mod ens18 +ipv4.routes "10.30.5.0/24 10.20.0.254".
Creating a new connection
A second network card (ens19) was added — for the management network:
[ali@srv-db01 ~]$ sudo nmcli con add type ethernet con-name mgmt ifname ens19 \ ipv4.method manual ipv4.addresses 192.168.100.20/24 Connection 'mgmt' (a1b2c3d4-e5f6-4789-a0b1-c2d3e4f5a6b7) successfully added. [ali@srv-db01 ~]$ nmcli con show NAME UUID TYPE DEVICE ens18 3c8a1f2e-7b4d-4e6a-9c1b-2d3e4f5a6b7c ethernet ens18 mgmt a1b2c3d4-e5f6-4789-a0b1-c2d3e4f5a6b7 ethernet ens19 lo 9f8e7d6c-5b4a-4392-8170-6f5e4d3c2b1a loopback lo
No gateway is set on the management network — the default route must be on one interface only.
| Command | What it does |
|---|---|
nmcli con add ... | A new profile |
nmcli con mod name ... | Change it |
nmcli con up name / down name | Activate / deactivate |
nmcli con del name | Delete |
nmcli con reload | Re-read files if you edited them by hand |
nmcli dev disconnect ens19 | Disconnect a device |
nmtui | A text menu interface — handy in the exam too |
The settings file
[ali@srv-db01 ~]$ sudo cat /etc/NetworkManager/system-connections/ens18.nmconnection [connection] id=ens18 uuid=3c8a1f2e-7b4d-4e6a-9c1b-2d3e4f5a6b7c type=ethernet interface-name=ens18 [ipv4] address1=10.20.0.20/24,10.20.0.1 dns=10.20.0.1;1.1.1.1; dns-search=itnuvo.local; method=manual
On RHEL 9 profiles use this keyfile format (RHEL 8's /etc/sysconfig/network-scripts/ifcfg-* is deprecated). If you edit the file by hand: mode 600, then nmcli con reload and nmcli con up.
Lab: a static IP
In the Red Hat simulator, on srv-db01 (the simulator supports nmcli con show, con mod, con up):
- Look at the current profile and address.
- Set a static IP: 10.20.0.20/24, gateway 10.20.0.1, DNS 10.20.0.1.
- Run
con upand check withip -br aandip route. - Check connectivity with
ping srv-db01fromsrv-web01.
[ali@srv-db01 ~]$ nmcli con show [ali@srv-db01 ~]$ sudo nmcli con mod ens18 ipv4.method manual ipv4.addresses 10.20.0.20/24 ipv4.gateway 10.20.0.1 ipv4.dns 10.20.0.1 [ali@srv-db01 ~]$ sudo nmcli con up ens18 [ali@srv-db01 ~]$ ip -br a show ens18 [ali@srv-db01 ~]$ ip route
- (On your own VM) add a second address with
+ipv4.addresses, then remove it. Check the settings survive a reboot.
Check: nmcli -f ipv4.method con show ens18 → manual; ping works.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the difference between a "device" and a "connection" in nmcli?
2After nmcli con mod, when is the change applied?
3How do you add a second IP without removing the existing one?
4How do you correctly set DNS servers on RHEL?
5Where are NetworkManager profiles stored on RHEL 9?
Comments
Sign in to comment.