Introduction
By default, most servers obtain their IP address via DHCP. However, servers should use a static IP to ensure the address does not change after a reboot. This guide covers three methods depending on your Linux distribution.
Method 1: Netplan (Ubuntu 18.04+)
Ubuntu uses Netplan for network configuration. Configuration files are located in /etc/netplan/.
Step 1: Identify your network interface
ip link show
Common names: eth0, ens18, enp0s3.
Step 2: Edit the Netplan configuration
sudo nano /etc/netplan/01-netcfg.yaml
Replace the content with:
network:
version: 2
renderer: networkd
ethernets:
ens18:
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
Replace ens18 with your interface name, and adjust the IP, gateway, and DNS values.
Step 3: Apply the configuration
sudo netplan apply
Step 4: Verify
ip addr show ens18
ip route show
Method 2: /etc/network/interfaces (Debian)
Debian uses the traditional interfaces file for network configuration.
Step 1: Edit the interfaces file
sudo nano /etc/network/interfaces
Find the section for your interface (e.g., eth0) and change it from dhcp to static:
auto eth0
iface eth0 inet static
address 203.0.113.10
netmask 255.255.255.0
gateway 203.0.113.1
dns-nameservers 8.8.8.8 1.1.1.1
Step 2: Restart networking
sudo systemctl restart networking
Step 3: Verify
ip addr show eth0
ip route show
Method 3: NetworkManager (AlmaLinux / RHEL / Rocky Linux)
RHEL-based distributions use NetworkManager and the nmcli command-line tool.
Step 1: Identify the connection name
nmcli connection show
Output example:
NAME UUID TYPE DEVICE
System eth0 a1b2c3d4-e5f6-7890-abcd-ef1234567890 ethernet eth0
Note the connection name (e.g., System eth0 or ens18).
Step 2: Set the static IP
sudo nmcli connection modify "System eth0" ipv4.addresses 203.0.113.10/24 ipv4.gateway 203.0.113.1 ipv4.dns "8.8.8.8 1.1.1.1" ipv4.method manual
Step 3: Restart the connection
sudo nmcli connection down "System eth0"
sudo nmcli connection up "System eth0"
Step 4: Verify
nmcli connection show "System eth0" | grep ipv4
ip addr show eth0
Alternative: Edit the connection file directly
NetworkManager stores connection files in /etc/NetworkManager/system-connections/ (or /etc/sysconfig/network-scripts/ on older systems). You can edit these files and then reload:
sudo nmcli connection reload
sudo nmcli connection up "System eth0"
Configuring DNS separately
If DNS is not set via the network configuration, you can edit /etc/resolv.conf directly:
nameserver 8.8.8.8
nameserver 1.1.1.1
Note: On systems using NetworkManager or systemd-resolved, this file may be overwritten. Use the methods above to set DNS persistently.
Quick reference
| Distribution | Config method | Config file(s) | Apply command |
|---|---|---|---|
| Ubuntu 18.04+ | Netplan | /etc/netplan/*.yaml |
sudo netplan apply |
| Debian | interfaces | /etc/network/interfaces |
sudo systemctl restart networking |
| AlmaLinux / RHEL | NetworkManager |
nmcli or /etc/NetworkManager/system-connections/
|
sudo nmcli connection up "name" |