Skip to content

Instantly share code, notes, and snippets.

@alias454
Created February 2, 2018 01:29
Show Gist options
  • Save alias454/2d711a6942640209f1df828990db1444 to your computer and use it in GitHub Desktop.
Save alias454/2d711a6942640209f1df828990db1444 to your computer and use it in GitHub Desktop.
Prepare RHEL/CentOS 7 VM for template creation
# Script created based on info from
# https://lonesysadmin.net/2013/03/26/preparing-linux-template-vms/
#
# Prepare RHEL/CentOS 7 VM for template creation
#!/bin/bash
# Stop logging services
systemctl stop rsyslog
service auditd stop
# Remove old kernels
/bin/package-cleanup --oldkernels --count=1
# Clean out yum
/usr/bin/yum clean all
rm -rf /var/cache/yum
# Force the logs to rotate & remove 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
# Truncate the audit logs
truncate -s 0 /var/log/audit/audit.log
truncate -s 0 /var/log/wtmp
truncate -s 0 /var/log/lastlog
truncate -s 0 /var/log/grubby
# Remove the udev persistent device rules
/bin/rm -f /etc/udev/rules.d/70*
# Clean /tmp
/bin/rm -rf /tmp/*
/bin/rm -rf /var/tmp/*
# Remove SSH host keys
/bin/rm -f /etc/ssh/*key*
# Remove the root user’s shell history
/bin/rm -f ~root/.bash_history
unset HISTFILE
# Remove the root user’s SSH history & other cruft
/bin/rm -rf /root/.ssh/
/bin/rm -f /root/anaconda-ks.cfg
# shutdown
init 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment