Skip to content

Instantly share code, notes, and snippets.

@lucasmaurice
Created April 30, 2023 22:47
Show Gist options
  • Save lucasmaurice/8d902dc9d8603e83ac84ca14b5cfe514 to your computer and use it in GitHub Desktop.
Save lucasmaurice/8d902dc9d8603e83ac84ca14b5cfe514 to your computer and use it in GitHub Desktop.
This one clean up an Ubuntu installation before creating a template.
#!/usr/bin/bash
echo '> Begin Cleanup script'
echo '> Cleaning all audit logs ...'
sudo service rsyslog stop
if [ -f /var/log/audit/audit.log ]; then
cat /dev/null > /var/log/audit/audit.log
fi
if [ -f /var/log/wtmp ]; then
cat /dev/null > /var/log/wtmp
fi
if [ -f /var/log/lastlog ]; then
cat /dev/null > /var/log/lastlog
fi
if [ -f /var/log/syslog ]; then
cat /dev/null > /var/log/syslog
fi
echo '> Cleaning persistent udev rules ...'
if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
rm /etc/udev/rules.d/70-persistent-net.rules
fi
echo '> Cleaning /tmp directories ...'
rm -rf /tmp/*
rm -rf /var/tmp/*
echo '> Cleaning APT ...'
DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=300 -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" autoremove -y
DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=300 -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" clean
echo '> Cleaning the machine-id ...'
truncate -s 0 /etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id
echo '> Cleaning SSH Host Keys'
rm /etc/ssh/ssh_host_*
echo '> Cleaning shell history ...'
unset HISTFILE
truncate -s 0 ~/.bash_history
rm -fr /root/.bash_history
history -cw
### All done. ###
echo '> Done with cleanup script.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment