Blog Β» Linux Β» How to Set a Static IP in Linux: Ubuntu, RHEL, and Debian Covered
β€Ί how-to-set-static-ip-linux Linux terminal showing netplan YAML configuration for setting a static IP address on Ubuntu server

How to Set a Static IP in Linux: Ubuntu, RHEL, and Debian Covered

Table of Contents

The first time I tried to figure out how to set a static IP in Linux, I locked myself out of a $5 VPS in Frankfurt. I was 4,800 miles away, it was 2 a.m., and I had just confidently typed sudo netplan apply on a misconfigured YAML file. The SSH session died mid-keystroke. That was the night I learned the difference between netplan apply and netplan try β€” and trust me, you want to learn it before I did, not after.

Linux terminal showing netplan YAML configuration for setting a static IP address on Ubuntu server

This guide walks you through three battle-tested methods for assigning a static IP on Linux: Netplan (Ubuntu), nmcli (RHEL, Rocky, AlmaLinux), and the classic /etc/network/interfaces (Debian without NetworkManager). I’ll also flag the breaking changes in Rocky Linux 10 and the deprecated syntax that’s still floating around half the tutorials on the internet.

Quick answer: On Ubuntu, edit a YAML file in /etc/netplan/, then run sudo netplan try. On RHEL/Rocky, use nmcli con mod to set ipv4.method manual with your address, gateway, and DNS. On Debian without NetworkManager, edit /etc/network/interfaces and restart networking. Details below β€” and there’s one chmod gotcha most guides miss that I’ll cover in section 3.

Why Static IPs Matter for Linux Servers

Linux now powers about 78% of web-facing servers and roughly 90% of public cloud workloads. Almost every one of those boxes needs a predictable address. DHCP is fine for your laptop β€” it’s a disaster for a database server.

RackNerd Mobile Leaderboard Banner

Get a VPS from as low as $11/year! WOW!

Dynamic IPs cause real problems:

  • SSH access breaks when your server’s address shifts and your ~/.ssh/config entries go stale.
  • DNS records point to ghosts if your A records still reference yesterday’s IP.
  • Service bindings (Postgres, Redis, Nginx upstreams) fail when the address they’re bound to disappears.
  • Firewall rules become useless β€” try writing a clean UFW firewall or nftables ruleset when your trusted hosts keep moving.
  • Fail2ban gets confused when banned addresses are recycled. Pairing a static IP with set up fail2ban is one of the cleanest hardening combos there is.

A static IP is also the foundation of any sane remote admin setup. I won’t even build a secure SSH configuration on a server until I know its address is pinned. Everything else assumes it.

Before You Begin: Gather Your Network Details

Three minutes of prep saves you from a 3 a.m. console-recovery session. Don’t skip this part.

Find Your Interface Name

Modern Linux uses predictable network interface names β€” ens3, enp3s0, eth0, wlp2s0. They vary by hardware and distro. Find yours with:

ip link
# or
ip a

Look for the interface that isn’t lo (the loopback). If you want the full breakdown of what those flags and states mean, I wrote a deeper guide on the ip command in Linux that covers it all. There’s also a focused walkthrough on how to check your network interface if you’ve got multiple NICs to sort through.

What You Need Before Configuring

Before you touch any config file, write down:

  1. The static IP address you want, plus subnet in CIDR notation β€” usually something like 192.168.1.50/24.
  2. The default gateway β€” typically your router, like 192.168.1.1.
  3. DNS server IPs β€” 1.1.1.1 and 8.8.8.8 are reliable choices.
  4. The interface name from the step above.

Per RFC 1918, your private ranges are 192.168.0.0/16, 10.0.0.0/8, and 172.16.0.0/12. Don’t grab an IP that’s inside your DHCP pool β€” pick one above it or carve out a static reservation in your router.

Method 1: Netplan (Ubuntu 18.04 and Later)

Netplan has been the default on Ubuntu server and desktop since 18.04. Configuration lives in /etc/netplan/ as YAML files. If you only see one, edit it; if there are several, remember they’re applied in lexicographic order.

Edit the Netplan YAML File

sudo nano /etc/netplan/01-netcfg.yaml

Here’s a working modern config for an interface named ens3:

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.1.50/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8

Two things will bite you here. First, YAML hates tabs β€” use spaces, two per indent level. Mixed indentation produces silent failures. Second, the gateway4 key you’ll see in old tutorials is deprecated since Netplan 0.104. Use the routes: to: default form above. On Ubuntu, DNS is typically handled by systemd-resolved, which Netplan integrates with automatically.

Now lock down permissions β€” this is the chmod gotcha I mentioned earlier:

sudo chmod 600 /etc/netplan/01-netcfg.yaml

Modern Netplan refuses to read world-readable files and just silently ignores them. I’ve seen people lose hours to this. For the canonical syntax reference, the Ubuntu Server networking documentation is the source of truth.

Apply It Safely with netplan try (Not netplan apply)

Remote server warning: If you’re SSH’d into a box, NEVER run netplan apply first. Use netplan try. It applies the config for 120 seconds and auto-reverts if you don’t confirm. This is the safety net I wish I’d known about that night in Frankfurt.

sudo netplan try

If your SSH connection holds, hit Enter to keep the config. If not, wait two minutes β€” Netplan rolls it back automatically and your old settings return.

Verify the Static IP Is Active

ip a
ip route
ping -c 3 8.8.8.8

The interface should show your new address, the default route should point to your gateway, and pings should return.

Method 2: nmcli (Works on Ubuntu, RHEL, Rocky Linux, AlmaLinux)

nmcli is the command-line frontend for NetworkManager. RHEL 9, RHEL 10, Rocky Linux 9/10, AlmaLinux, and Fedora all default to it. Heads up: Rocky Linux 10 removed Network-Scripts (ifcfg) entirely. The official format now is keyfiles in /etc/NetworkManager/system-connections/. If you’re following an old tutorial that edits /etc/sysconfig/network-scripts/ifcfg-*, it won’t work on Rocky 10. The Rocky Linux 10 network configuration guide spells out the change.

List and Identify Your Connection

nmcli con show

You’ll see connection names like System eth0 or Wired connection 1. Note the exact name β€” quotes matter if it has spaces.

Configure the Static IP

sudo nmcli con mod "System eth0" ipv4.method manual \
  ipv4.addresses 192.168.1.50/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "1.1.1.1 8.8.8.8"

That single chained command sets the method to manual, assigns the address, sets the gateway, and configures DNS. Backslashes let it wrap across lines cleanly.

Bring the Connection Down and Back Up

sudo nmcli con down "System eth0" && sudo nmcli con up "System eth0"

If you’re on SSH, do these on the same line with && so the connection comes back automatically. Don’t run them separately or you’ll lock yourself out the moment the connection goes down.

Verify

nmcli con show "System eth0"
ip a
ss -tuln

The nmcli con show command displays the saved settings. ip a confirms what’s live. The ss command lists active sockets β€” handy to confirm your services are still listening. If you prefer the classic, netstat works too.

Method 3: /etc/network/interfaces (Debian Without NetworkManager)

If you’re on a minimal Debian 12 install with no NetworkManager, the old ifupdown system still works. Edit:

sudo nano /etc/network/interfaces

Add a stanza like this:

auto eth0
iface eth0 inet static
    address 192.168.1.50
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 1.1.1.1 8.8.8.8

Apply it with:

sudo systemctl restart networking
# or, more granular:
sudo ifdown eth0 && sudo ifup eth0

Verify with ip a the same way as the other methods. Same idea, older syntax, no YAML drama.

Common Mistakes That Will Lock You Out

I’ve watched smart people make every one of these. Save yourself.

Tabs Instead of Spaces in Netplan YAML

YAML is whitespace-sensitive. A single tab character will produce a parse error β€” or worse, a partial config that boots into a broken state. Always use spaces. Set your editor to convert tabs to spaces and you’ll never think about it again.

Using the Deprecated gateway4 Key

gateway4: 192.168.1.1 has been deprecated since Netplan 0.104. Newer versions warn and may ignore it. Use the routes form shown above. Half the tutorials on Google still show the old syntax β€” don’t trust anything written before 2022 without verifying.

Running netplan apply Instead of netplan try Over SSH

This was my Frankfurt mistake. netplan apply has no rollback. If your config has a typo, your network goes down with no recovery path except hosting-provider console access. netplan try waits 120 seconds for your confirmation, then reverts. Make it a reflex.

Forgetting to Disable DHCP

If you set addresses but leave dhcp4: yes, you get a race between the DHCP client and your static config. The result is unpredictable β€” sometimes the right address sticks, sometimes it doesn’t. Always set dhcp4: no when assigning a static address.

How to Revert to DHCP

Worth knowing before you need it. When a server’s static IP conflicts with something on the network and you need to flip back fast:

For Netplan, edit the YAML file:

network:
  version: 2
  ethernets:
    ens3:
      dhcp4: yes

Strip out the addresses, routes, and nameservers sections, then run sudo netplan try.

For nmcli:

sudo nmcli con mod "System eth0" ipv4.method auto
sudo nmcli con mod "System eth0" ipv4.addresses "" ipv4.gateway "" ipv4.dns ""
sudo nmcli con down "System eth0" && sudo nmcli con up "System eth0"

Once you’re back on DHCP and the network is healthy, it’s worth a quick check your network speed pass to confirm throughput before redeploying anything.

Quick Troubleshooting Checklist

When something’s off, walk this list top to bottom. Each step rules out one layer.

  1. ip a β€” does the interface actually show your static IP?
  2. ip route β€” does the default route point to your gateway?
  3. ping <gateway IP> β€” can you reach your gateway? If not, the link or address is wrong.
  4. ping 8.8.8.8 β€” can you reach the public internet? If not, the gateway or NAT is the problem.
  5. dig google.com β€” is DNS resolving? If pings work but DNS doesn’t, your nameservers are misconfigured. The full dig command guide covers more diagnostics.
  6. journalctl -u NetworkManager or systemctl status systemd-networkd β€” check service logs for parser errors and apply failures.

If you’ve checked all six and you’re still stuck, the deeper guide on how to troubleshoot network issues in Linux is the next stop β€” it covers MTU mismatches, ARP weirdness, and the kind of subtle layer-2 problems that don’t show up in the basic checklist.

Final Thoughts

Setting a static IP in Linux looks intimidating until you’ve done it a couple of times. The three methods β€” Netplan, nmcli, and /etc/network/interfaces β€” cover every mainstream distro you’ll touch in 2026. Pick the one your distro defaults to, gather your network details first, and please, please use netplan try if you’re on a remote box. My Frankfurt VPS forgives me, but it doesn’t forget.

Once your address is pinned, the rest of your stack gets a lot more reliable: firewalls work, SSH config files stay accurate, and DNS records actually point somewhere useful. If you’re hardening a fresh server, the natural next steps are configuring UFW, securing SSH, and setting up fail2ban β€” I’ve linked guides for each above. Got a tricky network issue I didn’t cover? Drop into the troubleshooting guide linked in the checklist, or stick around and browse the rest of the Linux-guides catalog. There’s always more to break and fix.

author avatar
Alexa Velinxs
I'm Alexa Velinxs, a cryptocurrency trading expert passionate about demystifying digital assets for both beginners and seasoned investors. Through my writing, I share actionable strategies, market insights, and practical tips to help you navigate the crypto landscape with confidence. Let's explore the future of finance together.
Related Posts