#!/bin/bash # # Sysprep OS for vmware template creation. # # Shamelessly taken from https://oitibs.com/linux-vm-template-creation/ echo "Removing openssh-server's host keys..." rm -vf /etc/ssh/ssh_host_* cat /dev/null > /etc/rc.local chmod +x /etc/rc.local cat << 'EOL' | sudo tee /etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # dynamically create hostname (optional) if hostname | grep localhost; then hostnamectl set-hostname "$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 ; echo '')" fi test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server if [ ! -f /etc/machine-id ] then /usr/bin/systemd-machine-id-setup fi exit 0 EOL echo "Cleaning up /var/mail..." rm -vf /var/mail/* echo "Clean up apt cache..." find /var/cache/apt/archives -type f -exec rm -vf \{\} \; echo "Clean up ntp..." rm -vf /var/lib/ntp/ntp.drift rm -vf /var/lib/ntp/ntp.conf.dhcp echo "Clean up dhcp leases..." rm -vf /var/lib/dhcp/*.leases* rm -vf /var/lib/dhcp3/*.leases* echo "Clean up udev rules..." rm -vf /etc/udev/rules.d/70-persistent-cd.rules rm -vf /etc/udev/rules.d/70-persistent-net.rules echo "Clean up urandom seed..." rm -vf /var/lib/urandom/random-seed echo "Clean up backups..." rm -vrf /var/backups/*; rm -vf /etc/shadow- /etc/passwd- /etc/group- /etc/gshadow- /etc/subgid- /etc/subuid- echo "Cleaning up /var/log..." find /var/log -type f -name "*.gz" -exec rm -vf \{\} \; find /var/log -type f -name "*.1" -exec rm -vf \{\} \; find /var/log -type f -exec truncate -s0 \{\} \; echo "Compacting drive..." dd if=/dev/zero of=EMPTY bs=1M > /dev/null rm -vf /root/EMPTY fstrim -av echo "Clearing bash history..." cat /dev/null > /root/.bash_history history -c echo "Process complete..." poweroff