Centos 7 Cleanup before creating VM template


Centos 7

yum -y install yum-utils

# Clear big and old logs
#  older than 7 days and greater than 3M or older than 30 days.
find /var -name "*.log" \( \( -size +3M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;

# Cleanup YUM and remove orphan packages
yum clean all
rm -rf /var/cache/yum
package-cleanup --quiet --leaves --exclude-bin
package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y
rpm --rebuilddb

# Remove old kernels
package-cleanup --oldkernels --count=2

# Remove core dumps
find -regex ".*/core\.[0-9]+$" -delete

# Find files larger than 10 MB
find / -type f -size +10M -not -path "/sys/*" -not -path "/boot/*" -not -path "/proc/*" | xargs du -sh

# delete files larger than 100 MB
# find / -type f -size +100M -exec rm \{\} \;


# Delete non-english locales
localedef --list-archive | grep -v -i ^en | xargs localedef --delete-from-archive

mv /usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.tmpl
build-locale-archive
# if fail , try
# yum reinstall glibc-common

# Cleanup journals
journalctl --disk-usage
# journalctl --vacuum-size=1M
# journalctl --vacuum-time=1months
rm -rf /run/log/journal/*

# Shows you all messages marked as error, critical, alert, or emergency
# 0 emerg
# 1 alert
# 2 crit
# 3 err
# 4 warning
# 5 notice
# 6 info
# 7 debug
journalctl -p 3

# List services
# systemctl list-units --type service
# systemctl list-unit-files --type service



http://everything-virtual.com/2016/05/06/creating-a-centos-7-2-vmware-gold-template/



#!/bin/bash 
#stop logging services 
/sbin/service rsyslog stop 
/sbin/service auditd stop 
#remove old kernels 
/bin/package-cleanup –oldkernels –count=1 
#clean yum cache 
/usr/bin/yum clean all 
#force logrotate to shrink logspace and remove old logs as well as truncate logs 
/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 
#remove udev hardware rules 
/bin/rm -f /etc/udev/rules.d/70* 
#remove uuid from ifcfg scripts 
/bin/sed –i”.bak” ‘/UUID/d’ /etc/sysconfig/network-scripts/ifcfg-eno16777984 
#remove SSH host keys 
/bin/rm -f /etc/ssh/*key* 
#remove root users shell history 
/bin/rm -f ~root/.bash_history 
unset HISTFILE 
#remove root users SSH history 
/bin/rm -rf ~root/.ssh/

cat /dev/null > ~/.bash_history

history –c
sys-unconfig

# Check services
systemctl status httpd
systemctl disable httpd
systemctl enable httpd

systemctl list-unit-files | grep httpd

systemctl --failed