How to Set a Static IP Address on Ubuntu Server

How to Set a Static IP Address on Ubuntu Server

Ubuntu Server (24.04 LTS Noble Numbat and later releases, including point updates through 2026) uses Netplan as the default network configuration tool. Netplan employs declarative YAML files to define network settings, which are then translated into backend configurations (typically systemd-networkd on servers). This approach provides consistency across installations, supports complex setups (bridges, bonds, VLANs), and avoids legacy tools like /etc/network/interfaces.

Setting a static IP means disabling DHCP and explicitly defining the address, subnet mask (via CIDR notation), gateway, and DNS servers. The process is the same on physical hardware, VMs (KVM, VirtualBox, VMware), and most cloud instances (though cloud providers often override via metadata/cloud-init).

Prerequisites

  • Know your desired static IP, subnet, gateway, and preferred DNS servers. Example: IP 192.168.1.100/24, gateway 192.168.1.1, DNS 8.8.8.8 and 1.1.1.1
  • Identify the network interface name: ip link show or ip -c addr. Common names: enp1s0, ens3, eth0, eno1 (predictable naming since ~16.04).
  • Have console or out-of-band access (e.g., IPMI, cloud console) in case of misconfiguration — a wrong IP can lock you out of SSH.

Step-by-Step Configuration

  1. Backup existing configuration Netplan files live in /etc/netplan/. Back up before editing:

    Bash
    sudo cp -r /etc/netplan /etc/netplan.bak-$(date +%F)
  2. Locate or create the Netplan file List files: ls /etc/netplan/ Typical names: 00-installer-config.yaml, 50-cloud-init.yaml, 01-netcfg.yaml. If no suitable file exists, create one (higher numbers override lower):

    Bash
    sudo nano /etc/netplan/99-static-ip.yaml
  3. Edit the YAML configuration Replace or add content matching your interface and desired settings. Use spaces (not tabs) for indentation — YAML is strict.

    Minimal complete example for a single Ethernet interface:

    YAML
    network:
      version: 2
      renderer: networkd
      ethernets:
        enp1s0:                  # ← replace with your interface name
          dhcp4: false
          dhcp6: false
          addresses:
            - 192.168.1.100/24     # your static IP + subnet mask in CIDR
          routes:
            - to: default
              via: 192.168.1.1       # your gateway/router IP
          nameservers:
            addresses: [8.8.8.8, 1.1.1.1]   # preferred DNS servers
            # search: [example.local]       # optional search domain

    Key elements explained:

    • dhcp4: false / dhcp6: false — disables automatic IP assignment
    • addresses — list of IPs (usually one); CIDR notation combines address + prefix length
    • routes block — defines the default gateway (metric optional but useful with multiple interfaces)
    • nameservers — overrides DHCP-provided DNS; use public resolvers or internal ones
    • renderer: networkd — standard for servers (NetworkManager is desktop default)
  4. Validate syntax before applying

    Bash
    sudo netplan generate --debug

    No output or errors = good. Errors show line numbers and issues (indentation, missing colons, etc.).

  5. Apply safely

    Bash
    sudo netplan try
    • Applies changes for 120 seconds
    • Press Enter to keep; any key or timeout reverts automatically
    • Excellent safeguard against locking yourself out

    If confident: sudo netplan apply

  6. Verify the new configuration

    Bash
    ip -c addr show enp1s0
    ip route show
    ping 8.8.8.8
    ping google.com          # tests DNS too
    timedatectl status       # optional – shows resolver status

    Check logs if issues arise:

    Bash
    journalctl -u systemd-networkd -xe

Common Pitfalls & Troubleshooting

  • Wrong interface name → no IP assigned; double-check with ip link
  • YAML syntax error → netplan apply fails silently or reverts; always use netplan generate first
  • Multiple YAML files → higher-numbered files override; check all with ls /etc/netplan/
  • Cloud instances → cloud-init may override; look for 50-cloud-init.yaml and adjust or disable overrides
  • No connectivity after apply → revert with sudo netplan try (if still in session) or console access
  • DNS not resolving → ensure nameservers block is present; test with resolvectl status

Why Netplan Over Older Methods?

Netplan centralizes configuration, supports atomic application (safer than editing live interfaces), integrates cleanly with systemd-networkd, and scales to complex topologies without manual scripting. Legacy /etc/network/interfaces is no longer supported on modern Ubuntu Server installs.

For most servers (web, database, file, container hosts), a static IP ensures predictable access for SSH, monitoring agents, firewalls, and reverse proxies. Once set, it persists across reboots and upgrades unless cloud-init intervenes.

If you share your current ip addr output, desired IP details, or whether this is a cloud VM vs bare metal, more precise YAML examples can be tailored.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!