-
-
Save jeremygaither/11579f69b9e9bb626989394c5437d141 to your computer and use it in GitHub Desktop.
Script to clean up Ubuntu EC2 instance before packaging as an AMI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function print_green { | |
echo -e "\e[32m${1}\e[0m" | |
} | |
print_green 'Clean Apt' | |
apt-get -y autoremove | |
aptitude clean | |
aptitude autoclean | |
print_green 'Remove user SSH keys' | |
# [ -f /home/ubuntu/.ssh/authorized_keys ] && rm /home/ubuntu/.ssh/authorized_keys | |
for homefolder in /home/*; do | |
rm -rf $homefolder/.ssh | |
done | |
rm -rf /root/.ssh | |
print_green 'Remove host keys for regeneration at first boot' | |
for hostkey in /etc/ssh/*_host_*; do | |
rm $hostkey | |
done | |
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 'manually clean up ifcfg scripts?' | |
grep 'HOSTNAME=' /etc/sysconfig/network-scripts/ifcfg-* | |
grep 'HWADDR=' /etc/sysconfig/network-scripts/ifcfg-* | |
print_green 'AMI cleanup complete!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need to add more things from https://github.com/libguestfs/libguestfs/tree/master/sysprep