Setting a static IP on Ubuntu means writing a Netplan YAML file – and the syntax changed between releases. This script asks for the new address, detects everything else, writes a correct file for your version and applies it with netplan try, so a mistake can’t lock you out of a remote server.
What the script does
- Detects the network interface and current gateway from the default route
- Reads the Ubuntu version and uses
routes:on 22.04+ orgateway4:on older releases - Writes a separate
/etc/netplan/99-static.yaml(the installer’s file is left alone) with root-only permissions - Tests the change with an automatic rollback after 120 seconds
The script
#!/usr/bin/env bashn# Configure a static IP on Ubuntu with Netplan – Inder Kochar (inderkochar.in)n# Detects the interface from the default route and writes the right YAML for the release.nset -euo pipefailnnif ! sudo -v; thenn echo u0022This script needs sudo rights.u0022 u003eu00262n exit 1nfinn. /etc/os-releasenUBUNTU_VERSION=u0022${VERSION_ID:-unknown}u0022necho u0022Ubuntu version: $UBUNTU_VERSIONu0022nniface=$(ip route | awk '/^default/ {print $5; exit}')ngateway=$(ip route | awk '/^default/ {print $3; exit}')ncurrent=$(ip -o -4 addr show dev u0022$ifaceu0022 | awk '{print $4}' | head -1)nnecho u0022=== Current network ===u0022necho u0022Hostname : $(hostname)u0022necho u0022Interface: $ifaceu0022necho u0022Address : $currentu0022necho u0022Gateway : $gatewayu0022nnread -rp u0022Switch $iface to a static IP? (y/N) u0022 answern[[ u0022${answer^^}u0022 == u0022Yu0022 ]] || { echo u0022No changes made.u0022; exit 0; }nnread -rp u0022New IP address (e.g. 192.168.1.50): u0022 new_ipnread -rp u0022Prefix length (e.g. 24): u0022 new_prefixnread -rp u0022Gateway [$gateway]: u0022 new_gwnnew_gw=${new_gw:-$gateway}nread -rp u0022DNS servers [1.1.1.1, 9.9.9.9]: u0022 dnsndns=${dns:-1.1.1.1, 9.9.9.9}nn# Ubuntu 22.04+ uses u0022routes:u0022; 20.04 and older use u0022gateway4:u0022.nif dpkg u002du002dcompare-versions u0022$UBUNTU_VERSIONu0022 ge 22.04; thenn route_block=u0022 routes:n - to: defaultn via: $new_gwu0022nelsen route_block=u0022 gateway4: $new_gwu0022nfinnfile=/etc/netplan/99-static.yamlnsudo tee u0022$fileu0022 u003e /dev/null u003cu003cEOFnnetwork:n version: 2n renderer: networkdn ethernets:n $iface:n dhcp4: falsen addresses: [$new_ip/$new_prefix]n$route_blockn nameservers:n addresses: [$dns]nEOFnsudo chmod 600 u0022$fileu0022nnecho u0022Written $file – testing (rolls back in 120 s unless you press Enter)…u0022nsudo netplan tryHow to use it
- Save it as
set-static-ip.sh(or use the Download button above). - Make it executable:
chmod +x set-static-ip.sh - Run it:
./set-static-ip.shand answer the prompts. - When
netplan tryasks, press Enter within 120 seconds to keep the new settings – or do nothing and it rolls back.
Cloud and cloud-init servers
If the server was installed with cloud-init (most cloud VMs), it may rewrite network settings at boot. Disable its network step first: echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Prefer a form?
The Netplan YAML Generator builds the same file in your browser – including extra addresses, IPv6, MTU and search domains.
Updated: the original version of this script had a stray fi and a broken sudo check; both are fixed above.

