Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Commands

Do not run these over SSH before you have read the next section: a wrong value cuts your connection to the server.

sudo nmcli connection modify "System eth0" \
  ipv4.addresses 203.0.113.10/24 \
  ipv4.gateway 203.0.113.1 \
  ipv4.dns "192.0.2.53 198.51.100.53" \
  ipv4.method manual
sudo nmcli connection up "System eth0"

The first command stores the settings; the second applies them. The connection name, interface, and addresses here are examples. On a cloud server, the address, prefix, gateway, and DNS servers come from the provider: use the values shown in your provider's control panel, never invented ones.

Before You Change Anything

A wrong network setting cuts your SSH session, so prepare a way back first.

  • Console: find the server console in your provider's control panel. Working from the console is safest, because losing the network does not end that session.
  • Current state: save ip a and ip r, and copy the connection file (see below) to /root.
  • Test first: changes made with ip addr and ip route last only until reboot and are the safe way to try an address, as shown in the Linux ip command.
  • Timed rollback: nmcli has no "try" mode. If you work over SSH, schedule a restore before you apply, as shown next.
sudo ls -l /etc/NetworkManager/system-connections/
sudo cp -a /etc/NetworkManager/system-connections/eth0.nmconnection /root/
sudo systemd-run --on-active=5m --unit=net-rollback /bin/sh -c \
  'cp -a /root/eth0.nmconnection /etc/NetworkManager/system-connections/ && nmcli connection reload && nmcli connection up "System eth0"'
systemctl list-timers 'net-rollback*'

Use the real file and connection names from your server. On release 9, a profile can still come from an ifcfg file in /etc/sysconfig/network-scripts/; back up and restore that file instead. After five minutes the timer copies the saved file back, reloads it, and activates the old profile. If the new settings work, cancel it with sudo systemctl stop net-rollback.timer and check that systemctl list-timers no longer shows it; otherwise the restore undoes your change. If you lose the connection, wait five minutes and log in again with the old address.

Find the Connection Name

nmcli device status
nmcli connection show

A device is the interface, such as eth0 or ens3. A connection is a saved profile applied to a device, and it has its own name: System eth0, cloud-init eth0, and Wired connection 1 are typical. nmcli commands take the connection name; put it in quotes when it contains a space.

Check the Stored Settings

nmcli -f ipv4 connection show "System eth0"

This prints every ipv4. setting of the profile. To add a second address instead of replacing the first, prefix the setting with a plus sign: +ipv4.addresses 192.0.2.50/24. With ipv4.method manual, DHCP is no longer used, so the DNS servers you set are the only ones.

Apply the Change

nmcli connection up reactivates the profile with the new settings. The interface drops for a moment, and an SSH session to an address you changed ends. nmcli device reapply eth0 applies changes to a running device and may avoid that break for some settings.

Where the Settings Live

NetworkManager stores each profile as a keyfile in /etc/NetworkManager/system-connections/, with the ending .nmconnection. The file must belong to root and be readable only by root. The [ipv4] section holds the method, addresses, gateway, and DNS. Prefer nmcli over editing the file; if you do edit it, run sudo nmcli connection reload afterward. Older systems kept profiles as ifcfg files in /etc/sysconfig/network-scripts/. AlmaLinux and Rocky Linux 9 still read them but treat the format as deprecated, and release 10 no longer supports it, so keyfiles are the format to use.

On cloud images, cloud-init may write the profile at first boot. To keep it from replacing your settings, disable its network configuration as described in what is cloud-init.

The Menu Alternative: nmtui

sudo dnf install NetworkManager-tui
sudo nmtui

nmtui is a text menu over the same settings. Choose Edit a connection, set IPv4 configuration to Manual, fill in the address with its prefix, the gateway, and DNS, and save. Then activate the profile. The same lockout risk applies.

Back to DHCP

sudo nmcli connection modify "System eth0" ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns ""
sudo nmcli connection up "System eth0"

This also changes the address, so the same precautions apply: work from the console or set up the timed rollback first. Check the result with ip -br a, ip r, and a ping to the gateway. Ubuntu servers configure the network with netplan instead: see how to configure a static IP with netplan.

Was this answer helpful?
Loading...