Chapter 6. Rerunning cloud-init on a virtual machine


When additional configurations, such as modification in directives, modules, or datasources, are required for a virtual machine (VM) configured by the cloud-init service, you can rerun cloud-init.

Warning

Rerunning the cloud-init process can result in data loss and highly discouraged for a production environment. Credentials such as SSH keys and password could be lost or replaced.

6.1. Modifying a VM created from a KVM Guest Image after cloud-init has run

When you launch a VM with the cloud-init package installed and enabled, cloud-init runs in its default state on the initial boot of the VM. If you want to change the setting of the VM, you can modify your cloud-init configuration and rerun cloud-init.

Procedure

  1. Log in to your VM.
  2. Add or change directives, for example, modify the cloud.cfg file in the /etc/cloud directory or add directives to the /etc/cloud/cloud.cfg.d directory.
  3. Run the cloud-init clean command to clean directories so that cloud-init can rerun.
  4. Run the following commands as root to clean the VM data:

    rm -Rf /var/lib/cloud/instances/
    rm -Rf /var/lib/cloud/instance
    rm -Rf /var/lib/cloud/data/
    Copy to Clipboard
    Note

    You can save the cleaned image as a template image and use that image for multiple VMs. The new VMs will use the updated configuration to run cloud-init.

  5. Rerun cloud-init or reboot the VM to implement the configuration changes you made. For details on re-running cloud-init, see Re-run cloud-init.

6.2. Modifying a VM for a specific datasource after cloud-init has run

You can modify your cloud-init configuration before rerunning cloud-init. Note that the exact steps you need to perform vary based on your datasource. The following procedure uses OpenStack as an example datasource.

Procedure

  1. Create and launch an instance for the OpenStack Platform. For information about creating instances for OpenStack, see Creating an instance. In this example, the virtual machine (VM) includes cloud-init, which runs upon boot of the VM.
  2. Add or change directives. For example, modify the user-data.file file that is stored on the OpenStack HTTP server.
  3. Clean the virtual machine:

    # rm -rf /etc/resolv.conf /run/cloud-init
    # userdel -rf cloud-user
    # hostnamectl set-hostname localhost.localdomain
    # rm /etc/NetworkManager/conf.d/99-cloud-init.conf
    Copy to Clipboard
    Note

    You can save the cleaned image as a template image and use that image for multiple virtual machines. The new virtual machines run cloud-init, using your updated cloud-init configuration.

  4. Rerun cloud-init or reboot the VM to implement the configuration changes you made. For details on re-running cloud-init, see Re-run cloud-init.
Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat