Quick nmcli setting of an IP for a device

NetworkManager has two different objects. A device is the interface (ens18, eth0). A connection is the saved profile. nmcli connection modify takes the connection name, which is often Wired connection 1, not the device name. Mixing those up is why the commands fail on a fresh VM.

See both:

ip -br addr
nmcli -f NAME,UUID,DEVICE,TYPE connection show

Example device (the name you care about is in the NAME column of the second command):

# ip addr # or ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether bc:24:11:8a:2e:b9 brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.100.10/24 brd 192.168.100.255 scope global noprefixroute ens18

Set a static address on the existing profile

Use the connection NAME. Set method, address, gateway, and DNS in one shot so NetworkManager does not apply a half-edited profile. Needs root unless polkit is already granting it:

CON="Wired connection 1"   # from: nmcli -f NAME,DEVICE connection show
DEV=ens18                  # from: ip -br addr

sudo nmcli connection modify "$CON" 
  connection.interface-name "$DEV" 
  ipv4.method manual 
  ipv4.addresses 192.168.100.10/24 
  ipv4.gateway 192.168.100.1 
  ipv4.dns "8.8.8.8,8.8.4.4"

sudo nmcli connection up "$CON"

If you are SSH’d in over that same NIC, skip connection down then up. connection up (or nmcli device reapply "$DEV") applies the new IPv4 without an extra drop.

Replace the profile (not the device)

nmcli connection delete deletes the saved profile. The NIC stays. Then add a new profile bound to that interface:

sudo nmcli connection delete "$CON"
sudo nmcli connection add type ethernet con-name "$DEV" ifname "$DEV" 
  ipv4.method manual 
  ipv4.addresses 192.168.100.10/24 
  ipv4.gateway 192.168.100.1 
  ipv4.dns "8.8.8.8,8.8.4.4"
sudo nmcli connection up "$DEV"

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.