Skip to content

Instantly share code, notes, and snippets.

@sonuame
Last active November 16, 2021 03:24
Show Gist options
  • Save sonuame/02f5368ef30f79ad2a1be294395efeb4 to your computer and use it in GitHub Desktop.
Save sonuame/02f5368ef30f79ad2a1be294395efeb4 to your computer and use it in GitHub Desktop.
Ubuntu Cleanup
#!/bin/sh -eux
# Delete all Linux headers
dpkg --list \
| awk '{ print $2 }' \
| grep 'linux-headers' \
| xargs apt-get -y purge;
# Remove specific Linux kernels, such as linux-image-3.11.0-15-generic but
# keeps the current kernel and does not touch the virtual packages,
# e.g. 'linux-image-generic', etc.
dpkg --list \
| awk '{ print $2 }' \
| grep 'linux-image-3.*-generic' \
| grep -v `uname -r` \
| xargs apt-get -y purge;
# Delete Linux source
dpkg --list \
| awk '{ print $2 }' \
| grep linux-source \
| xargs apt-get -y purge;
# Delete development packages
dpkg --list \
| awk '{ print $2 }' \
| grep -- '-dev$' \
| xargs apt-get -y purge;
# Delete compilers and other development tools
apt-get -y purge cpp gcc g++;
# Delete X11 libraries
apt-get -y purge libx11-data xauth libxmuu1 libxcb1 libx11-6 libxext6;
# Delete obsolete networking
apt-get -y purge ppp pppconfig pppoeconf;
# Delete oddities
apt-get -y purge popularity-contest;
apt-get -y autoremove;
apt-get -y clean;
# clean up lingering cache files
rm -f /etc/apt/apt.conf.d/01proxy
# Cleanup puppet certs
find /etc/puppet -iname "*base*" -delete
find /var/lib/puppet/ -iname "*apache.org*" -type f -delete
# Finally, enable puppet
puppet agent --enable
# Make sure ssh host keys get regen'd at instance startup
cat > /etc/dhcp/dhclient-exit-hooks.d/sethostname <<'EOM'
#!/bin/sh
# dhclient change hostname script for Ubuntu
# /etc/dhcp/dhclient-exit-hooks.d/sethostname
# logs in /var/log/upstart/network-interface-eth0.log
# for debugging:
echo "cloudstack-sethostname BEGIN"
export
set -x
if [ $reason = "BOUND" ]; then
echo new_ip_address=$new_ip_address
echo new_host_name=$new_host_name
echo new_domain_name=$new_domain_name
oldhostname=$(hostname -s)
if [ $oldhostname != 'localhost' ]; then
# Rename Host
echo $new_host_name > /etc/hostname
hostname -F /etc/hostname
# Update /etc/hosts if needed
TMPHOSTS=/etc/hosts.dhcp.new
if ! grep "$new_ip_address $new_host_name.$new_domain_name $new_host_name" /etc/hosts; then
# Remove the 127.0.1.1 put there by the debian installer
grep -v '127\.0\.1\.1 ' < /etc/hosts > $TMPHOSTS
# Add the our new ip address and name
echo "$new_ip_address $new_host_name.$new_domain_name $new_host_name" >> $TMPHOSTS
mv $TMPHOSTS /etc/hosts
fi
# Recreate SSH2 keys
export DEBIAN_FRONTEND=noninteractive
dpkg-reconfigure openssh-server
fi
fi
echo "cloudstack-sethostname END"
EOM
# set perms on script
chmod 774 /etc/dhcp/dhclient-exit-hooks.d/sethostname
# Cleanup log files
cat /dev/null > /var/log/wtmp 2>/dev/null
logrotate -f /etc/logrotate.conf 2>/dev/null
find /var/log -type f -delete
rm -f /var/lib/dhcp/dhclient.*
#!/bin/bash
###############################################################################
# This script removes unneeded files and libraries, including log files and snaps.
# I originally created it to combat the /var directory filling up (when on a
# seperate partition). I know there is redundency with apt vs apt-get commands,
# but I alsways seem to have more drive space after running both!
#
# I created this for Ubuntu 18.04 (Desktop), but it should run on other distros
# based on Ubuntu without issue.
#
# To run this script from anywhere, place it in your /usr/local/bin directory
#
# To list your partition sizes run: df -Th | sort
###############################################################################
# Remove apt / apt-get files
sudo apt clean
sudo apt -s clean
sudo apt clean all
sudo apt autoremove
sudo apt-get clean
sudo apt-get -s clean
sudo apt-get clean all
sudo apt-get autoclean
#Remove Old Log Files
sudo rm -f /var/log/*gz
# Remove Thumbnail Cache
rm -rf ~/.cache/thumbnails/*
# Remove old snaps
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
sudo snap remove "$snapname" --revision="$revision"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment