Ubuntu 16.04 Desktop Unattended Installation

Ubuntu Installation

I previously wrote a tutorial on how to automate Ubuntu Desktop 14.04 LTS installation, but for 16.04 it didn’t work because of some differences in installation media.

Here’s a full and tested solution to create your unattended Ubuntu installation media. For me the process on VirtualBox guest took about 30 minutes to fully boot into working system without even touching the keyboard.

1. Create Kickstart file

Install Kickstart by typing this command into Terminal:

sudo apt-get install system-config-kickstart

After the installation process is completed, open Kickstart Configurator using Unity search or just by typing system-config-kickstart in Terminal.

When the Kickstart opens, choose the settings you need for your installation. You can review all possible options on the official Kickstart Configurator documentation site.

When you are finished with the configuration, press File > Save File in the top menu. Keep default file name as ks.cfg and save it to your Desktop. Some of the options doesn’t work in Kickstart configurator, but it can be edited manually after saving ks.cfg.

Here are the contents of my ks.cfg file:

#Generated by Kickstart Configurator
#platform=AMD64 or Intel EM64T

#System language
lang en_US
#Language modules to install
langsupport en_US
#System keyboard
keyboard us
#System mouse
mouse
#System timezone
timezone America/New_York
#Root password
rootpw --disabled
#Initial user
user ernestas --fullname "Ernestas" --iscrypted --password $1$dLKYoVUI$nb3qZVsrV7IIJMia1kskE0
#Reboot after installation
reboot
#Use text mode install
text
#Install OS instead of upgrade
install
#Use CDROM installation media
cdrom
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part swap --size 2048
part /boot --fstype ext4 --size 512
part / --fstype ext4 --size 1 --grow
#System authorization infomation
auth  --useshadow  --enablemd5
#Network information
network --bootproto=dhcp --device=eth0
#Firewall configuration
firewall --disabled
#Do not configure the X Window System
skipx

# Additional packages to install
%packages
ca-certificates
openssl
python
openssh-server
vim
ubuntu-desktop
unity

# Add your custom post installation script here
%post

# Add post installation script to /usr/local/bin/ directory
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
cp /mnt/cdrom/post.sh /usr/local/bin/
chown ernestas:ernestas /usr/local/bin/post.sh
chmod 744 /usr/local/bin/post.sh

# Fix locale
echo 'LANG="en_US.UTF-8"' > /etc/default/locale
echo 'LANGUAGE="en_US:en"' >> /etc/default/locale
echo 'LC_ALL="en_US.UTF-8"' >> /etc/default/locale

# Clean
apt-get -f -y install
apt-get -y autoremove
apt-get clean

2. Create Preseed file

Pressed commands work when they are directly written inside the Kickstart file, but I wanted to separate the two files. Create new file on you desktop named ubuntu-auto.seed with the following contents:

# Unmount drives with active partitions. Without this command all the installation process would stop and require confirmation to unmount drives that are already mounted.
d-i preseed/early_command string umount /media || true

# Don't install recommended items
d-i preseed base-installer/install-recommends boolean false

# Install only security updates automatically
d-i preseed pkgsel/update-policy select unattended-upgrades For additional Preseed configuration options, refer to official Ubuntu [installation guide](https://help.ubuntu.com/lts/installation-guide/armhf/apbs01.html).

3. Create post installation script

Create a file named post.sh with your desired commands.

Even if Kickstart has the %post section, sometimes you may need to make some changes only after the system has fully booted.

My ks.cfg copies post.sh file to /usr/local/bin directory for that. If you don’t want to use the post installation script simply comment out the commands in ks.cfg after # Add post installation script... line.

Here is my shortened post.sh file for example:

#!/bin/bash
# Set hostname
read -p "Hostname: " pcHost
echo "Changing the hostname to $pcHost"
sed -i "s/ubuntu/$pcHost/g" /etc/hostname
sed -i "s/ubuntu/$pcHost/g" /etc/hosts

4. Extract original ISO image

Download Ubuntu Server 16.04 x64 from official Ubuntu website and put it on your desktop. It is necessary to use server version, because desktop version doesn’t support unattended installations. Desktop functionality will work after ubuntu-desktop package in %packages section of kb.cfg file is installed.

Mount the .iso file to Ubuntu filesystem. The commands below will mount .iso file to the folder named ubuntu_iso:

cd ~/Desktop
mkdir ubuntu_iso
sudo mount -r -o loop ubuntu-16.04.1-server-amd64.iso ubuntu_iso

Copy .iso contents to another folder on your desktop so we can edit the files. Don’t forget to set the right permissions to be able to make changes.

mkdir ubuntu_files
rsync -a ubuntu_iso/ ubuntu_files/
sudo chown ernestas:ernestas ubuntu_files
sudo chmod 755 ubuntu_files
sudo umount ubuntu_iso
rm -rf ubuntu_iso

5. Edit contents of ISO image

Copy ks.cfg, ubuntu-auto.seed and post.sh files to newly created ubuntu_files folder

cp {ks.cfg,ubuntu-auto.seed,post.sh} ubuntu_files
chmod 644 ubuntu_files/ks.cfg ubuntu_files/ubuntu-auto.seed
chmod 744 ubuntu_files/post.sh

Change isolinux folder and isolinux/txt.cfg permissions for editing:

chmod 755 ubuntu_files/isolinux ubuntu_files/isolinux/txt.cfg ubuntu_files/isolinux/isolinux.cfg

Now we need to make the installer read Kickstart and Preseed files by including new menu selection for automatic Ubuntu installation. To do this edit txt.cfg in isolinux folder:

nano ubuntu_files/isolinux/txt.cfg

Paste the following content right after the line containing default install:

label autoinstall
  menu label ^Automatically install Ubuntu
  kernel /install/vmlinuz
  append file=/cdrom/preseed/ubuntu-server.seed vga=788 initrd=/install/initrd.gz ks=cdrom:/ks.cfg preseed/file=/cdrom/ubuntu-auto.seed quiet --

Set timeout to start the installation automatically:

sed -i -r 's/timeout\s+[0-9]+/timeout 3/g' ubuntu_files/isolinux/isolinux.cfg

Warning! This command will start installation proccess immediately and format the hard drive as defined in ks.cfg. Skip it if you want to manually start the process after booting into ISO.

Change the permissions back:

chmod 555 ubuntu_files/isolinux
chmod 444 ubuntu_files/isolinux/txt.cfg ubuntu_files/isolinux/isolinux.cfg

6. Recreate ISO image

Create new .iso:

sudo mkisofs -D -r -V "ubuntu-auto" -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -input-charset utf-8 -cache-inodes -quiet -o ubuntu-auto.iso ubuntu_files/

Remove files:

sudo rm -rf ubuntu_files

7. Optional – Create bootable USB media

Make the .iso file bootable from external USB devices:

sudo apt-get install syslinux-utils
sudo isohybrid ubuntu-auto.iso

Plug in USB device and determine the path to it:

lsblk

My USB device was identified as /dev/sdb. Unmount the media:

sudo umount /dev/sdb

Copy ISO to USB device. Make sure to copy directly to the drive, not the first partition (/dev/sdb1)

sudo dd if=ubuntu-auto.iso of=/dev/sdb bs=4M && sync

8. Known Issues

8.1. USB doesn’t mount as CD-ROM Error message: “Your installation CD-ROM couldn’t be mounted. This probably means that the CD-ROM was not in the drive. If so you can insert it and try again.”

Fix: Use dd to create bootable USB. Previuosly this error was caused by Ubuntu Startup Disk Creator.