How to configure a static IP

Topic: Networking basics

Summary

Set a static IPv4 address on Linux using netplan, NetworkManager, or /etc/network/interfaces. Include address, prefix length, default gateway, and nameservers so the host has stable addressing and can reach other networks and resolve names. Use this when deploying a server or when DHCP is not desired.

Intent: How-to

Quick answer

  • Netplan: under the interface set addresses: [IP/PREFIX], gateway4: GATEWAY, nameservers.addresses: [DNS1, DNS2]; run netplan apply.
  • NetworkManager: nmcli con mod ID ipv4.addresses IP/PREFIX ipv4.gateway GATEWAY ipv4.dns 'DNS1 DNS2' ipv4.method manual; nmcli con up ID.
  • Verify with ip addr, ip route, getent hosts google.com, and ping to gateway and 8.8.8.8; ensure the IP is not in use by another host on the subnet.

Prerequisites

Steps

  1. Choose the right config source

    Netplan: Ubuntu and many cloud images; config in /etc/netplan/*.yaml. NetworkManager: common on desktops and some servers; use nmcli or the GUI. interfaces: Debian legacy; edit /etc/network/interfaces.

  2. Configure with netplan

    Edit /etc/netplan/50-cloud-init.yaml or new file. Under ethernets.IF: set addresses: [192.168.1.10/24], gateway4: 192.168.1.1, nameservers.addresses: [8.8.8.8]. Use spaces not tabs. Run sudo netplan apply.

  3. Configure with NetworkManager

    nmcli con mod CONNECTION_ID ipv4.addresses 192.168.1.10/24 ipv4.gateway 192.168.1.1 ipv4.dns '8.8.8.8 1.1.1.1' ipv4.method manual; nmcli con up CONNECTION_ID.

  4. Verify and persist

    ip addr; ip route; getent hosts google.com; ping gateway and 8.8.8.8. Config is persistent; reboot and confirm the address and route are applied on boot.

Summary

Configure a static IP by setting the address, prefix, default gateway, and nameservers in netplan, NetworkManager, or /etc/network/interfaces. Use this when you need a fixed address for a server or when DHCP is not used.

Prerequisites

Steps

Step 1: Choose the right config source

  • Netplan: Common on Ubuntu and cloud images; YAML files in /etc/netplan/.
  • NetworkManager: Common on desktops and some servers; use nmcli or GUI.
  • /etc/network/interfaces: Legacy Debian-style; still used on some systems.

Step 2: Configure with netplan

Edit a file in /etc/netplan/ (e.g. 99-static.yaml):

network:
  version: 2
  ethernets:
    eth0:
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]

Run sudo netplan apply. Use spaces, not tabs.

Step 3: Configure with NetworkManager

sudo nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.10/24 ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8 1.1.1.1" ipv4.method manual
sudo nmcli con up "Wired connection 1"

Step 4: Verify and persist

ip addr show
ip route
getent hosts google.com
ping -c 2 192.168.1.1
ping -c 2 8.8.8.8

Config is persistent; reboot and confirm.

Verification

  • Interface has the configured IP and prefix; default route points to the gateway; DNS resolution works; connectivity to gateway and internet works.

Troubleshooting

Address not applied — Netplan: run netplan apply and check YAML syntax. NM: run nmcli con up ID; check that the connection is for the correct interface.

No connectivity — Wrong gateway or prefix; gateway not on the same subnet; or firewall blocking; verify with ip route and ping.

Next steps

Continue to