Created
January 26, 2026 08:42
-
-
Save M507/9055f1a247dd5ed2c043fa69cedd939a to your computer and use it in GitHub Desktop.
enable-dhcp.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| NETPLAN_FILE="/etc/netplan/50-cloud-init.yaml" | |
| BACKUP_FILE="/etc/netplan/50-cloud-init.yaml.bak.$(date +%s)" | |
| # Must be run as root | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "Run as root" | |
| exit 1 | |
| fi | |
| # Ensure file exists | |
| if [[ ! -f "$NETPLAN_FILE" ]]; then | |
| echo "File not found: $NETPLAN_FILE" | |
| exit 1 | |
| fi | |
| # Detect primary interface (first non-lo interface) | |
| IFACE=$(ip -o link show | awk -F': ' '{print $2}' | grep -v lo | head -n 1) | |
| if [[ -z "$IFACE" ]]; then | |
| echo "Could not detect network interface" | |
| exit 1 | |
| fi | |
| echo "Detected interface: $IFACE" | |
| # Backup | |
| cp "$NETPLAN_FILE" "$BACKUP_FILE" | |
| echo "Backup created: $BACKUP_FILE" | |
| # Rewrite netplan with DHCP enabled | |
| cat > "$NETPLAN_FILE" <<EOF | |
| network: | |
| version: 2 | |
| ethernets: | |
| $IFACE: | |
| dhcp4: true | |
| EOF | |
| # Apply netplan | |
| netplan apply | |
| echo "DHCP enabled on $IFACE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment