Support > About cybersecurity > Configuring a Static IP on a Linux Server: A Comprehensive Approach to Interfaces, Routing, and DNS
Configuring a Static IP on a Linux Server: A Comprehensive Approach to Interfaces, Routing, and DNS
Time : 2026-12-03 14:51:48
Edit : Jtti

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.

Relevant contents

How to Purchase an Already Registered Domain Name: Expert Advice on Four Approaches and Key Pitfalls to Avoid Say Goodbye to Cumbersome Passwords: The Ultimate Configuration Guide for SSH Key-Based Login, Agent Usage, and Multi-Identity Management Can’t access your server? A ranked list of potential causes—troubleshoot from the most common issues to the least likely ones. What Is a Domain Name Server? A Detailed Explanation of the Difference Between a Domain Name Server and DNS SSH Logging, Auditing, and Troubleshooting: A Practical Guide from Investigating Anomalies to Security Hardening How to Choose a Domain Name That Won’t Need Replacing for a Decade: 5 Steps, AI Prompts, and a Key Checklist Behind the Usage Limits on DeepSeek’s Free Chat Service: AI Inference Hits a "Hard Wall" of Computing Power, Prompting a Structural Re-evaluation of the Server Supply Chain Essential Linux Commands for System Administrators in 2026: 14 Key Tools to Go from "Functional" to "Highly Efficient" The Risks of Setting DNS TTL Too Low: The Hidden Cost Paid in the Name of "Flexibility" Practical Guide to RSS and RPS Tuning: Reducing Network Packet Loss on Linux Servers via Receive-Side Steering
Go back

24/7/365 support.Here for you 24/7/365.

Support