Skip to content

Instantly share code, notes, and snippets.

@stpettersens
Last active June 10, 2026 12:58
Show Gist options
  • Select an option

  • Save stpettersens/1c7bc7cf581d96cec738727df13bf047 to your computer and use it in GitHub Desktop.

Select an option

Save stpettersens/1c7bc7cf581d96cec738727df13bf047 to your computer and use it in GitHub Desktop.
Setup Netplan with WiFi settings on Ubuntu
#!/usr/bin/env bash
# Setup Netplan with WiFi settings on Ubuntu
# This should be copied to the target system with Rpi Imager etc.
# Usage: sudo setup-wifi-netplan-ubuntu.sh --ssid <SSID> --password <PASSWORD>
dump_netplan_config() {
echo "Setting WiFi SSID to \"$1\"."
echo "Setting WIFI password to \"$2\"."
local cfg="50-cloud-init.yaml"
truncate -s 0 $cfg
cat << EOF >> $cfg # I hate YAML :/
network:
version: 2
wifis:
wlan0:
dhcp4: true
access-points:
"$1":
password: "$2"
ethernets:
eth0:
optional: true
dhcp4: true
EOF
chmod 600 $cfg
cp -f $cfg /etc/netplan
}
main() {
local ssid="SSID_NETWORK"
local pass="password"
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--ssid)
ssid="$2"
shift 2
;;
-p|--password)
pass="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
dump_netplan_config "$ssid" "$pass"
netplan apply
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment