सामग्री पर जाएँ
Ubuntu / Linux

Set a Static IP on Ubuntu with a Bash Script (Netplan, 18.04–24.04)

Save the script, run it with sudo rights, answer four questions, and it writes /etc/netplan/99-static.yaml with the right gateway syntax for your release – then tests it with a 120-second automatic rollback.

परखा गया Ubuntu 24.04 LTS, 22.04 LTS, 20.04 LTS

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+ or gateway4: 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

set-static-ip.sh
#!/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 try

How to use it

  1. Save it as set-static-ip.sh (or use the Download button above).
  2. Make it executable: chmod +x set-static-ip.sh
  3. Run it: ./set-static-ip.sh and answer the prompts.
  4. When netplan try asks, 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.

Inder Kochar

लेखक

Inder Kochar

सिस्टम्स और नेटवर्क इंजीनियर – मैं वही समाधान लिखता हूँ जो क्लाइंट्स के साथ इस्तेमाल करता हूँ, असली सिस्टम पर परखे हुए। ऐसी ही किसी समस्या में फँसे हैं?

आगे पढ़ें

सवाल पूछें या अपना समाधान बताएँ

आपका ईमेल प्रकाशित नहीं होगा। टिप्पणियाँ जाँची जाती हैं – कृपया विषय से जुड़ी रखें।