Skip to content

Instantly share code, notes, and snippets.

@xykj61
Forked from jdowning/ami-clean.sh
Last active May 17, 2018 09:30
Show Gist options
  • Save xykj61/b8e4da06448e2d52cb3f4388ff47fd73 to your computer and use it in GitHub Desktop.
Save xykj61/b8e4da06448e2d52cb3f4388ff47fd73 to your computer and use it in GitHub Desktop.
Script to clean up Ubuntu EC2 instance before packaging as an AMI
#!/bin/bash
# This script cleans up your EC2 instance before baking a new AMI.
# Run the following command in a root shell:
#
# bash <(curl -s https://gist.githubusercontent.com/keatondunsford/b8e4da06448e2d52cb3f4388ff47fd73/raw/9a5c3cfd77ecb07fe725259df4c230531401575a/ami-clean.sh)
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Apt'
apt -y autoremove
apt clean
apt autoclean
print_green 'Remove SSH keys'
[ -f /home/ubuntu/.ssh/authorized_keys ] && rm /home/ubuntu/.ssh/authorized_keys
print_green 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done
print_green 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/ubuntu/.bash_history ] && rm /home/ubuntu/.bash_history
print_green 'AMI cleanup complete!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment