VMware Firstboot Network Configuration Script For Ubuntu

VMware Firstboot Network Configuration Script For Ubuntu

๐Ÿงญ Goal

This script configures static network settings (hostname, IP address, subnet, gateway, DNS) automatically on the first boot of a VMware virtual machine. It pulls values passed via VMwareโ€™s guestinfo properties and applies them using Netplan.

๐Ÿ› ๏ธ Prerequisites

Requirement Description
OS Ubuntu 18.04+ (using Netplan)
VMware Tools open-vm-tools or equivalent must be installed
VMware GuestInfo Settings Must set guestinfo.labvm.* via vSphere or cloud-init

๐Ÿ“ฆ Input Parameters via guestinfo

Set the following advanced options in vSphere or through OVF:

Key Example Value Description
guestinfo.labvm.hostname labvm01 Hostname for the VM
guestinfo.labvm.ipaddr 192.168.1.101 Static IP address
guestinfo.labvm.subnet 24 Subnet mask in CIDR
guestinfo.labvm.gateway 192.168.1.1 Default gateway
guestinfo.labvm.dns 8.8.8.8,1.1.1.1 DNS servers (comma-separated)

 

๐Ÿง  How the Script Works

  1. Fetches guestinfo values using vmtoolsd --cmd info-get ...

  2. Validates all inputs (non-empty, valid CIDR for subnet)

  3. Detects default interface automatically

  4. Sets the hostname using hostnamectl

  5. Generates a Netplan YAML config in /etc/netplan/01-netcfg.yaml

  6. Applies Netplan, validates it, sets file permissions

  7. Logs each step to /var/log/vmware-netconfig.log

  8. If everything succeeds:

    • touch /etc/firstboot-done

    • systemctl disable firstboot-network-config.service

 

โš™๏ธ Script Path & Permissions

Create a script as:

Save the script as:

Set permission to the file

๐Ÿ›‘ Systemd Service Setup (to run on first boot)

Create file: /etc/systemd/system/firstboot-network-config.service

Enable the service

It will run once during boot, then disable itself.

๐Ÿ“„ Log Output Example

All activity is logged to /var/log/vmware-netconfig.log:

๐Ÿงช Troubleshooting

Issue Solution
๐Ÿ”ด Could not detect default interface Ensure network is up and ip route returns default
โš ๏ธ gateway4 is deprecated Already fixed โ€” script uses routes:
๐Ÿ” Permissions for 01-netcfg.yaml are too open Script sets chmod 600
๐Ÿ›‘ Netplan apply fails Check /etc/netplan/01-netcfg.yaml syntax and try netplan try manually

 

You can create an OVF template with this script pre-injected, enabling fully automated VM deployments where network settings and other configurations are applied seamlessly during the initial boot. Note, that during the deployment, the following values need to be updated in VM Advance settings.

You can use the following script to deploy the VM and update the respective values.

 

Leave a Reply

Your email address will not be published. Required fields are marked *