Configuring IP for Linux Server
At a glance#
- Purpose: Assign a static IP address, gateway and DNS servers to a Linux server.
- Applies to: Ubuntu 18.04+ (Netplan) and RHEL/CentOS/Rocky 8+ (NetworkManager).
- Risk: High when done remotely — a mistake disconnects your own SSH session.
- Time: About 15 minutes.
Overview#
Servers should hold a fixed address rather than a DHCP lease, so that DNS records, firewall rules and monitoring stay valid.
How you set it depends on the distribution. Ubuntu uses Netplan, which reads YAML files from /etc/netplan/. RHEL-family systems use NetworkManager, driven by the nmcli command.
Warning: Applying a wrong address, gateway or netmask over SSH will cut you off from the server, and you will need console access to recover. Always have the VM console or an IPMI/iDRAC session open before you begin. On Ubuntu, usenetplan tryrather thannetplan apply— it reverts automatically if you lose connectivity.
Before you start#
Collect and confirm the following with whoever manages the network:
| Value | Example | Notes |
|---|---|---|
| IP address and prefix | 192.168.1.100/24 | Must be free and outside the DHCP pool |
| Gateway | 192.168.1.1 | Must be on the same subnet |
| DNS servers | 8.8.8.8, 8.8.4.4 | Use internal resolvers where the network has them |
| Interface name | eth0, ens192 | Confirm it — do not assume |
Identify the correct interface name:
ip -br linkRecord the current configuration so you can restore it:
ip addr show
ip route showProcedure — Ubuntu (Netplan)#
1. Find the active Netplan file#
ls /etc/netplan/You will typically see something like 00-installer-config.yaml or 50-cloud-init.yaml. Edit the existing file rather than creating a second one — where multiple files exist, later filenames override earlier ones and the result is confusing.
2. Back up the current configuration#
cp /etc/netplan/00-installer-config.yaml /root/netplan-backup-$(date +%F).yaml3. Edit the configuration#
nano /etc/netplan/00-installer-config.yamlReplace the contents with the following, substituting your own values:
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.100/24
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
routes:
- to: default
via: 192.168.1.1Warning: YAML is indentation-sensitive and tabs are not permitted — use spaces only. A single misplaced character causes the whole file to be rejected.
Notes on the fields:
eth0must match the interface name fromip -br link.dhcp4: falseis important. Without it, the interface may keep a DHCP lease alongside the static address.to: defaultis the modern replacement for the deprecatedgateway4:key. If you are on Ubuntu 18.04 you may still seegateway4: 192.168.1.1in existing files; it works but warns.
4. Fix file permissions#
Netplan warns loudly if the file is world-readable:
chmod 600 /etc/netplan/00-installer-config.yaml5. Test the configuration safely#
netplan tryThis applies the change and starts a 120-second timer. If you confirm at the prompt, it is kept. If you lose your connection and cannot confirm, it automatically reverts — which is exactly what you want when working remotely.
Once you are satisfied:
netplan applyProcedure — RHEL / Rocky / AlmaLinux (nmcli)#
1. Identify the connection name#
nmcli connection showThe connection name is often the same as the interface, but not always.
2. Apply the settings#
Substitute your connection name for ens192:
nmcli connection modify ens192 ipv4.addresses 192.168.1.100/24
nmcli connection modify ens192 ipv4.gateway 192.168.1.1
nmcli connection modify ens192 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection modify ens192 ipv4.method manual
nmcli connection modify ens192 connection.autoconnect yes3. Bring the connection back up#
nmcli connection down ens192 && nmcli connection up ens192Warning: Run this as a single command joined with&&, as shown. Runningdownon its own over SSH disconnects you beforeupcan execute.
Verification#
Confirm the address and route:
ip addr show
ip route showTest connectivity in three stages — this order isolates where a fault lies:
ping -c3 192.168.1.1 # gateway reachable?
ping -c3 8.8.8.8 # internet routing works?
ping -c3 google.com # DNS resolution works?If the first two succeed but the third fails, the problem is DNS, not the address.
Check the resolvers actually in use:
resolvectl status # systemd-resolved
cat /etc/resolv.confFinally, reboot and confirm the settings persist:
rebootTroubleshooting#
| Symptom | Cause and fix |
|---|---|
netplan apply reports a YAML error | Indentation is wrong or a tab was used. Validate with netplan generate, which reports the offending line. |
| Interface has both a static and a DHCP address | dhcp4: false is missing. Add it and reapply. |
| Gateway unreachable | Gateway is outside the configured subnet, or the prefix length is wrong. Check /24 versus /16. |
| Name resolution fails, everything else works | DNS servers wrong or unreachable. Test directly: dig @8.8.8.8 google.com |
| Locked out after applying | Connect via console, restore the backup file, and run netplan apply. |
| Settings lost after reboot | On RHEL, connection.autoconnect was not set to yes. |
| Duplicate address warnings in logs | Another host already holds that IP. Confirm it is free before assigning. |