Created
November 11, 2015 21:33
-
-
Save rbenigno/b7c15caa97b3d7952bba to your computer and use it in GitHub Desktop.
Generalize a RHEL/CentOS VM for cloning
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 | |
# Would like this to work on RHEL/CentOS 6/7, but only tested on CentOS 7. | |
# unset HISTFILE; > ~/.bash_history; bash <(curl -sL https://goo.gl/ZbtT5z) | |
if [ -s ~/.bash_history ]; then | |
read -p "Stop script to manually clear history? <y/n>: [N] " prompt | |
if [[ $prompt =~ [yY](es)* ]]; then | |
echo "Command: unset HISTFILE; > ~/.bash_history" | |
exit | |
fi | |
fi | |
# Stop logging | |
/sbin/service rsyslog stop | |
/sbin/service auditd stop | |
# Clean old kernels | |
which package-cleanup 1>&- 2>&- && HAVE_YUM_UTILS=true || yum install yum-utils -y | |
/usr/bin/package-cleanup --oldkernels --count=1 | |
if [ ! $HAVE_YUM_UTILS ] ; then yum remove yum-utils -y; fi | |
# Clean yum cache | |
/usr/bin/yum clean all | |
# Remove udev hardware rules (not present on CentOS 7) | |
/bin/rm -f /etc/udev/rules.d/70-persistent-net.rules | |
# Remove nic mac addr and uuid from ifcfg scripts | |
/bin/sed -i '/^\(HWADDR\|UUID\)=/d' /etc/sysconfig/network-scripts/ifcfg-eth0 | |
/bin/rm -f /etc/sysconfig/network-scripts/ifcfg-ens* | |
# Remove host keys (important step security wise. similar to system GUID in Windows) | |
/bin/rm -f /etc/ssh/*key* | |
# Force the logs to rotate & cleanup old logs we don’t need | |
/usr/sbin/logrotate -f /etc/logrotate.conf | |
/bin/rm -f /var/log/*-???????? /var/log/*.gz | |
/bin/rm -f /var/log/dmesg.old | |
/bin/rm -rf /var/log/anaconda | |
/bin/cat /dev/null > /var/log/audit/audit.log | |
/bin/cat /dev/null > /var/log/wtmp | |
/bin/cat /dev/null > /var/log/lastlog | |
/bin/cat /dev/null > /var/log/grubby | |
# Empty tmp | |
/bin/rm -rf /tmp/* | |
/bin/rm -rf /var/tmp/* | |
# Cleanup home directory | |
#/bin/rm -f ~root/anaconda-ks.cfg | |
/bin/rm -f ~/.ssh/id* | |
/bin/rm -f ~/.ssh/known_hosts | |
read -p "Remove authorized_keys? <y/n>: [N] " prompt | |
if [[ $prompt =~ [yY](es)* ]]; then | |
/bin/rm -f ~/.ssh/authorized_keys | |
fi | |
# Clear bash history | |
unset HISTFILE | |
> ~/.bash_history | |
# Set random hostname | |
NEWHOST=$(cat /dev/urandom | tr -dc 'a-z' | head -c12) | |
which hostnamectl 1>&- 2>&- && hostnamectl set-hostname $NEWHOST || echo $NEWHOST > /etc/hostname | |
# Shutdown | |
read -p "Shutdown? <y/n>: [N] " prompt | |
if [[ $prompt =~ [yY](es)* ]]; then | |
init 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment