In server administration, changing a dynamic IP to a static IP is a fundamental task—yet one where it is easy to run into pitfalls. A misconfigured gateway can cause a server to lose connectivity instantly, while an improperly set DNS can leave you in a situation where you can `ping` an IP address but are unable to load web pages. This guide clarifies the configuration logic for network interfaces, gateways, and DNS, tailored to the standards of today’s mainstream Linux distributions.
Step 1: Identify your network interface and current status
Before making any changes, use the following command to identify your network interface name and its current address:
ip addr show
Look for entries like `enp0s3`, `ens33`, or `eth0`—these are your network interface names; do not rely on guesswork or memory. Also, note your current IP address, subnet mask, and default gateway. You must use the same subnet and gateway when configuring the static IP; otherwise, you risk losing network connectivity entirely:
ip route show default
The address following `default via` in the output is your gateway address.
Step 2: Choose the configuration method based on your distribution
Ubuntu / Debian (Netplan, version 18.04 and later)
On Ubuntu servers, network management is handled by Netplan. Configuration files are located in the `/etc/netplan/` directory and use the `.yaml` file extension. Create or edit a configuration file (using a `99-` prefix is recommended, as it ensures higher priority):
yaml
network:
version: 2
ethernets:
enp0s3:
dhcp4: no
addresses: [192.168.1.100/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [223.5.5.5, 1.1.1.1]
Note two points: addresses must be written in CIDR format (e.g., `/24` corresponds to `255.255.255.0`); for the gateway, it is recommended to use the `to: default` syntax under `routes`, as `gateway4` is considered deprecated in newer versions. After saving, run `sudo netplan try` first; this command will automatically roll back changes if a timeout occurs. Once you have confirmed that the network is functioning correctly, run `sudo netplan apply`.
RHEL / CentOS / Rocky / AlmaLinux (NetworkManager)
For RHEL-based distributions, there is no need to manually write configuration files; the most reliable approach is to use `nmcli` to modify connection properties. Assuming the connection name is `ens33`:
nmcli connection modify ens33 \
ipv4.method manual \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "223.5.5.5 1.1.1.1"
nmcli connection up ens33
Setting `ipv4.method manual` informs NetworkManager that this is a static configuration; omitting this may result in the specified address and gateway failing to take effect as expected. If you prefer a visual interface, you can enter `nmtui` in your terminal; within the graphical menu, change the IPv4 configuration to "Manual" and enter the address, gateway, and DNS settings accordingly. The Traditional Debian Method (`/etc/network/interfaces`)
Some older Debian systems or minimal installations still use `ifupdown`. Edit the `/etc/network/interfaces` file:
text
auto ens18
iface ens18 inet static
address 192.168.1.100/24
gateway 192.168.1.1
dns-nameservers 223.5.5.5 1.1.1.1
After making changes, run `systemctl restart networking`. If `resolvconf` is installed on the system, the `dns-nameservers` entries will be handled automatically; otherwise, you may need to verify that the settings have been correctly written to `/etc/resolv.conf`.
systemd-networkd (Common in Minimal Server Environments)
Create a `.network` file (using INI format) within the `/etc/systemd/network/` directory:
ini
[Match]
Name=enp1s0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=223.5.5.5
Important note: By default, `systemd-networkd` does not automatically update `/etc/resolv.conf` nor does it provide DNS resolution services itself. It must be used in conjunction with `systemd-resolved`; otherwise, domain name resolution may fail, even if a `DNS=` entry is specified.
Step 3: The "Hidden Pitfalls" of DNS and Verification
Many tutorials instruct you only to modify your network interface configuration; however, whether DNS settings actually take effect depends on how your system manages its DNS resolver. If `systemd-resolved` is enabled, `/etc/resolv.conf` is typically a symbolic link pointing to `127.0.0.53`. Do not edit this file manually—doing so may result in your changes being overwritten upon reboot or causing conflicts with the `systemd-resolved` configuration. DNS settings should be defined within the appropriate configuration interface—such as the `nameservers` field in Netplan, `ipv4.dns` in `nmcli`, or the `DNS=` directive in `systemd-networkd`—so that they can be correctly injected by the system's network manager.
Once configured, verify the setup layer by layer:
`ip addr show enp0s3` # Confirm that the IP address has been applied
`ip route show default` # Confirm that the default gateway is correct
`resolvectl status` # Or `cat /etc/resolv.conf`; confirm that the DNS settings are loaded
`ping -c 3 223.5.5.5` # Ping an external IP to verify that routing is functional
`ping -c 3 www.baidu.com` # Ping a domain name to verify that DNS resolution is working
If the IP address and gateway are correct and you can ping external IP addresses, but domain name resolution fails, the issue almost certainly lies within the DNS configuration layer rather than the network interface itself. Revisiting the DNS configuration point specific to your Linux distribution is far more effective than repeatedly restarting network services.
The key to configuring a static IP is not just entering the correct addresses, but ensuring that the IP, gateway, and DNS settings are consistent within a single configuration source and are actively managed by the system's network manager. Backing up your existing configuration before making changes, and verifying the setup layer by layer using `ip route` and `resolvectl` afterward, will help you avoid most instances of losing connectivity after a configuration change.