Sub topic 6
Setting a Static IP with NetworkManager
Assume you can use a Linux shell, edit configuration with sudo, and understand IPv4 addresses, prefix lengths, gateways, and DNS. This chapter uses NetworkManager's command-line tool, nmcli. Do not use these commands if another service, such as Netplan with systemd-networkd, owns the interface.
Identify the connection and current state
NetworkManager stores settings in connection profiles. A profile describes how an interface connects, including its address, route, and DNS servers. First, list the profiles and devices:
nmcli connection show
nmcli device status
ip -br address show
ip route showThe connection name is not always the same as the device name. For example, a profile named Wired connection 1 may manage the device enp1s0.
Record the current settings before changing them. This gives you a recovery reference and helps you detect mistakes.
| Setting | Example | Purpose |
|---|---|---|
| IPv4 address | `192.168.1.50/24` | Identifies this server on its local network |
| Gateway | `192.168.1.1` | Sends traffic for other networks |
| DNS server | `9.9.9.9` | Resolves names such as `example.com` |
An address such as 192.168.1.50/24 uses a 24-bit prefix. The prefix determines which addresses are local. With /24, addresses from 192.168.1.1 through 192.168.1.254 are normally on the same local network.
Configure the connection profile
The following example changes an existing profile. Replace the connection name, address, gateway, and DNS values with those provided by your network administrator.
sudo nmcli connection modify "Wired connection 1" \
ipv4.method manual \
ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "192.168.1.1 9.9.9.9" \
ipv4.dns-search example.comipv4.method manual disables DHCP for IPv4 and tells NetworkManager to use the values you specify. The ipv4.addresses setting assigns the static address. The gateway becomes the default route. The DNS list gives NetworkManager resolver addresses, and ipv4.dns-search supplies a search domain for short names.
For a server that should not use a search domain, clear any old value:
sudo nmcli connection modify "Wired connection 1" ipv4.dns-search ""Check the saved profile before activating it:
nmcli connection show "Wired connection 1" \
| grep -E 'ipv4.method|ipv4.addresses|ipv4.gateway|ipv4.dns|ipv4.dns-search'Activate and verify the change
Bring the profile up to apply its settings:
sudo nmcli connection up "Wired connection 1"This can briefly interrupt the network. On an SSH session, the connection may close while the interface restarts.
Verify each layer separately:
ip -br address show dev enp1s0
ip route get 9.9.9.9
resolvectl status
resolvectl query example.com
getent hosts example.comThe route lookup should show the intended interface and gateway. resolvectl query tests the resolver service, while getent hosts tests the system's name-service path, which can include /etc/hosts as well as DNS.
Common errors
A wrong prefix length can place the gateway outside the local network. For example, /16 and /24 produce different local-network boundaries. If the address is correct but the gateway cannot be reached, check the prefix first.
A profile can be configured correctly but not attached to the expected device. Use nmcli connection show and nmcli device status to compare profile and device names.
A DNS failure does not always mean the network is down. Test the gateway and an IP address before testing a hostname:
ping -c 3 192.168.1.1
ping -c 3 9.9.9.9
resolvectl query example.comExercise
Which nmcli setting disables DHCP and assigns a fixed IPv4 address?
Show answer
Set ipv4.method manual and provide the address with ipv4.addresses, such as 192.168.1.50/24.