LinuxnetworkingTested on real hardware

Set a static IP with netplan (Ubuntu / Debian)

Configure a static IP address on Ubuntu or Debian using netplan. Includes a dry-run safety step so you can't accidentally lock yourself out of a remote server.

Distrosubuntu, debian
Shellbash
Updated
Script
bash
# 1. Find your interface name and current IP
ip -brief addr show
ip route show default

# 2. Create a netplan config (adjust INTERFACE, ADDRESS, GATEWAY, DNS)
sudo tee /etc/netplan/99-static.yaml > /dev/null <<'EOF'
network:
  version: 2
  ethernets:
    eth0:                        # Replace with your interface name (e.g. ens18, enp3s0)
      dhcp4: false
      addresses:
        - 192.168.1.100/24       # Your desired static IP and subnet mask
      routes:
        - to: default
          via: 192.168.1.1       # Your router or gateway IP
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8
EOF

# Set correct permissions (netplan warns if world-readable)
sudo chmod 600 /etc/netplan/99-static.yaml

# 3. Dry run — applies for 120 seconds then reverts if you don't confirm
#    Press ENTER to accept, or wait for automatic rollback
sudo netplan try

# 4. Apply permanently (only run after confirming connectivity in step 3)
sudo netplan apply

What this does

Creates a netplan configuration file that assigns a static IP to a network interface. The critical safety step is netplan try, which applies the new config for 120 seconds and automatically rolls back if you don’t confirm — preventing permanent lockout on remote servers.

Prerequisites

  • Ubuntu 18.04+ or Debian 12+ (netplan is standard on Ubuntu; Debian may need it installed)
  • Root or sudo access
  • Know your desired IP, subnet mask, gateway, and interface name

Find your interface name and current network settings

# List interfaces and their IPs
ip -brief addr show

# Show current default gateway
ip route show default

# Example output:
# lo        UNKNOWN  127.0.0.1/8
# eth0      UP       192.168.1.50/24     ← interface is 'eth0'
# default via 192.168.1.1 dev eth0       ← gateway is 192.168.1.1

Common interface names: eth0, ens18 (VMware/Proxmox), enp3s0 (bare metal), ens3 (KVM).

Check for existing netplan files

ls /etc/netplan/

If there’s an existing file (e.g. 00-installer-config.yaml from Ubuntu’s installer), either edit it directly or disable DHCP in it and add your static config in 99-static.yaml. Higher-numbered files take precedence.

Disable an existing DHCP file

If you have a 00-installer-config.yaml that sets dhcp4: true, edit it:

sudo nano /etc/netplan/00-installer-config.yaml

Change dhcp4: true to dhcp4: false and remove any existing dhcp4-overrides blocks, then add your static config to 99-static.yaml.

The netplan try safety window

When you run sudo netplan try, the new config is applied immediately and a 120-second countdown begins. If you close your SSH session or the new IP is wrong:

  • Automatic rollback: the old config is restored after the timeout — you reconnect on the original IP
  • Manual accept: if connectivity works, press Enter before the countdown expires to make it permanent

This is the correct way to change network settings on a remote server.

Multiple IPs on one interface

addresses:
  - 192.168.1.100/24
  - 192.168.1.101/24

VLAN interface

network:
  version: 2
  vlans:
    vlan10:
      id: 10
      link: eth0
      dhcp4: false
      addresses:
        - 10.0.10.5/24

Debian: install netplan

Netplan is not installed by default on Debian. Install it and disable ifupdown:

sudo apt-get install -y netplan.io
sudo systemctl disable networking

Notes

  • Netplan is a YAML front-end; it generates and applies config for either networkd (servers) or NetworkManager (desktops) depending on the renderer: key (default is networkd)
  • chmod 600 on the YAML file prevents a netplan warning about world-readable credentials
  • After netplan apply, verify: ip addr show eth0 and ping 1.1.1.1