KB
Linux Administration

Configuring IP for Linux Server

4 min read857 words16 code blocks

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, use netplan try rather than netplan apply — it reverts automatically if you lose connectivity.

Before you start#

Collect and confirm the following with whoever manages the network:

ValueExampleNotes
IP address and prefix192.168.1.100/24Must be free and outside the DHCP pool
Gateway192.168.1.1Must be on the same subnet
DNS servers8.8.8.8, 8.8.4.4Use internal resolvers where the network has them
Interface nameeth0, ens192Confirm it — do not assume

Identify the correct interface name:

bash
ip -br link

Record the current configuration so you can restore it:

bash
ip addr show
ip route show

Procedure — Ubuntu (Netplan)#

1. Find the active Netplan file#

bash
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#

bash
cp /etc/netplan/00-installer-config.yaml /root/netplan-backup-$(date +%F).yaml

3. Edit the configuration#

bash
nano /etc/netplan/00-installer-config.yaml

Replace the contents with the following, substituting your own values:

yaml
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.1
Warning: 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:

  • eth0 must match the interface name from ip -br link.
  • dhcp4: false is important. Without it, the interface may keep a DHCP lease alongside the static address.
  • to: default is the modern replacement for the deprecated gateway4: key. If you are on Ubuntu 18.04 you may still see gateway4: 192.168.1.1 in existing files; it works but warns.

4. Fix file permissions#

Netplan warns loudly if the file is world-readable:

bash
chmod 600 /etc/netplan/00-installer-config.yaml

5. Test the configuration safely#

bash
netplan try

This 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:

bash
netplan apply

Procedure — RHEL / Rocky / AlmaLinux (nmcli)#

1. Identify the connection name#

bash
nmcli connection show

The connection name is often the same as the interface, but not always.

2. Apply the settings#

Substitute your connection name for ens192:

bash
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 yes

3. Bring the connection back up#

bash
nmcli connection down ens192 && nmcli connection up ens192
Warning: Run this as a single command joined with &&, as shown. Running down on its own over SSH disconnects you before up can execute.

Verification#

Confirm the address and route:

bash
ip addr show
ip route show

Test connectivity in three stages — this order isolates where a fault lies:

bash
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:

bash
resolvectl status          # systemd-resolved
cat /etc/resolv.conf

Finally, reboot and confirm the settings persist:

bash
reboot

Troubleshooting#

SymptomCause and fix
netplan apply reports a YAML errorIndentation is wrong or a tab was used. Validate with netplan generate, which reports the offending line.
Interface has both a static and a DHCP addressdhcp4: false is missing. Add it and reapply.
Gateway unreachableGateway is outside the configured subnet, or the prefix length is wrong. Check /24 versus /16.
Name resolution fails, everything else worksDNS servers wrong or unreachable. Test directly: dig @8.8.8.8 google.com
Locked out after applyingConnect via console, restore the backup file, and run netplan apply.
Settings lost after rebootOn RHEL, connection.autoconnect was not set to yes.
Duplicate address warnings in logsAnother host already holds that IP. Confirm it is free before assigning.