Ubuntu autoinstall example

The ability to automatically install Ubuntu Linux would be useful to system administrators that must deploy the operating system to new physical or virtual machines on a regular basis. Automatic provision of these systems can save hundreds of man hours as well as decrease the likelihood of human error. Automation of installing Ubuntu involves supplying the operating system with the desired hostname, username, and password.

With this information, Ubuntu can install itself without the need for any user interaction. Once everything is done installing, you can boot into Ubuntu for the first time and have everything set up the way you planned.

In this tutorial, we will show you a couple examples of an Ubuntu autoinstall configuration. This will include generating an ISO file that can automatically install Ubuntu, as well as deploying an autoinstall to a new virtual machine with KVM.

In this tutorial you will learn:

  • How to generate cloud-init files for autoinstall
  • How to launch Ubuntu autoinstall for kvm
  • How to generate an Ubuntu autoinstall ISO file
Ubuntu autoinstall example
Ubuntu autoinstall example
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software kvm, xorriso, sed, curl, gpg, isolinux, wget, python3
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Ubuntu autoinstall examples




Below we will cover the step by step instructions to automatically install Ubuntu. Depending on your situation and environment, you may want to either generate an Ubuntu ISO that will self install, or just deploy a virtual machine (we will use KVM in this example) to automatically install Ubuntu. Choose whichever method suits you best below.

NOTE
These instructions have been tested and verified to work with Ubuntu 20.04. You may have varying levels of success with these steps on other versions of Ubuntu.

Creating an Ubuntu autoinstall config for KVM

For these steps, we will utilize the instructions found in the Ubuntu Autoinstall Quick Start guide.

  1. The first thing you will need to do is download Ubuntu 20.04. If you already have the ISO file, you can simply proceed to the next step.
  2. The next step is to mount your downloaded Ubuntu ISO file.
    $ sudo mount -r ~/Downloads/ubuntu-20.04-live-server-amd64.iso /mnt
    
  3. Next, create and navigate to a new directory from which we will host the cloud-init configuration that we create in the next steps.
    $ mkdir ~/www && cd ~/www
    
  4. Next, create a new file called user-data with the following content. Executing this entire block will generate the file for you.
    $ cat > user-data << 'EOF'
    #cloud-config
    autoinstall:
      version: 1
      identity:
        hostname: ubuntu-server
        password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
        username: ubuntu
    EOF
    

    The configuration above will set your system hostname to ubuntu-server, your username to ubuntu, and the password (which is an encrypted hash in the above code) to ubuntu.

  5. You will also need to generate a meta-data file which can simply be left empty.


    $ touch meta-data
    
  6. We will now use python3 to create a very basic web server that can serve our cloud-init configuration over http.
    $ cd ~/www && python3 -m http.server 3003
    
  7. Create a target disk to which you will install Ubuntu. You can put this file wherever you want and adjust the size according to your needs.
    $ truncate -s 20G image.img
    
  8. Next, we can run the Ubuntu auto installation.
    $ kvm -no-reboot -m 1024 \
        -drive file=image.img,format=raw,cache=none,if=virtio \
        -cdrom ~/Downloads/ubuntu-20.04-live-server-amd64.iso \
        -kernel /mnt/casper/vmlinuz \
        -initrd /mnt/casper/initrd \
        -append 'autoinstall ds=nocloud-net;s=http://_gateway:3003/'
    

    After the installation is complete, which should take a few minutes, the virtual machine will power off.

    Ubuntu being automatically installed via kvm
    Ubuntu being automatically installed via kvm
  9. Finally, to run your newly installed Ubuntu VM, you can boot it with this command.


    $ kvm -no-reboot -m 1024 \
        -drive file=image.img,format=raw,cache=none,if=virtio
    

Creating an Ubuntu autoinstall ISO

For these steps, we will utilize covertsh’s Ubuntu autoinstall generator hosted on Git Hub. This is just a Bash script that helps us simplify the process of generating an autoinstall ISO, thus making everything much easier.

  1. The first thing you will need to do is download Ubuntu 20.04. Then, download the necessary Bash script off of Git Hub:
    $ wget https://raw.githubusercontent.com/covertsh/ubuntu-autoinstall-generator/main/ubuntu-autoinstall-generator.sh
    
  2. Before proceeding further, ensure your system has the necessary prerequisites:
    $ sudo apt update
    $ sudo apt install xorriso curl isolinux
    
  3. Next, create a new file called user-data with the following content. Executing this entire block will generate the file for you.


    $ cat > user-data << 'EOF'
    #cloud-config
    autoinstall:
      version: 1
      identity:
        hostname: ubuntu-server
        password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
        username: ubuntu
    EOF
    

    The configuration above will set your system hostname to ubuntu-server, your username to ubuntu, and the password (which is an encrypted hash in the above code) to ubuntu.

  4. You will also need to generate a meta-data file which can simply be left empty.
    $ touch meta-data
    
  5. Now you are ready to run the script and create your Ubuntu autoinstall ISO file. Execute the following command, while substituting your own paths to the ISO file, user-data file, and meta-data file.
    $ chmod +x ubuntu-autoinstall-generator.sh
    $ bash ubuntu-autoinstall-generator.sh -s ~/Downloads/ubuntu-20.04-live-server-amd64.iso -a -k -u ~/user-data -m ~/meta-data -d ubuntu-autoinstall.iso
    

    Your new autoinstall ISO file will be named ubuntu-autoinstall.iso.

    Generating the Ubuntu autoinstall ISO file
    Generating the Ubuntu autoinstall ISO file
  6. All that is left to do now is use your ISO to install Ubuntu. This can be popped into a physical machine or used on a virtual machine like KVM, VirtualBox, VMware, etc, and the operating system will automatically be installed without user interaction.


Closing Thoughts

In this tutorial, we saw how to create an Ubuntu autoinstall configuration through two different examples on a Linux system. The first method involved deploying a cloud-init configuration to a kvm instance, and the second solution generated our own Ubuntu autoinstall ISO with the proper configuration files baked in. Regardless of your scenario, this should allow you to install Ubuntu without user interaction, cutting down on wasted hours and ultimately cost.