From Soiled Parakeet, 2 Years ago, written in Bash.
Embed
  1. #!/bin/bash
  2.  
  3. #Stop services for cleanup
  4. sudo service rsyslog stop
  5.  
  6. #clear audit logs
  7. if [ -f /var/log/wtmp ]; then
  8.     truncate -s0 /var/log/wtmp
  9. fi
  10. if [ -f /var/log/lastlog ]; then
  11.     truncate -s0 /var/log/lastlog
  12. fi
  13.  
  14. #cleanup /tmp directories
  15. rm -rf /tmp/*
  16. rm -rf /var/tmp/*
  17.  
  18. #cleanup current ssh keys
  19. rm -f /etc/ssh/ssh_host_*
  20.  
  21. #add check for ssh keys on reboot...regenerate if neccessary
  22. cat << 'EOL' | sudo tee /etc/rc.local
  23. #!/bin/sh -e
  24. #
  25. # rc.local
  26. #
  27. # This script is executed at the end of each multiuser runlevel.
  28. # Make sure that the script will "" on success or any other
  29. # value on error.
  30. #
  31. # In order to enable or disable this script just change the execution
  32. # bits.
  33. #
  34. # By default this script does nothing.
  35. # dynamically create hostname (optional)
  36. #if hostname | grep localhost; then
  37. #    hostnamectl set-hostname "$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 ; echo '')"
  38. #fi
  39. test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
  40. exit 0
  41. EOL
  42.  
  43. # make sure the script is executable
  44. chmod +x /etc/rc.local
  45.  
  46. #reset hostname
  47. # prevent cloudconfig from preserving the original hostname
  48. sed -i 's/preserve_hostname: false/preserve_hostname: true/g' /etc/cloud/cloud.cfg
  49. truncate -s0 /etc/hostname
  50. hostnamectl set-hostname localhost
  51.  
  52. #cleanup apt
  53. apt clean
  54.  
  55. # set dhcp to use mac - this is a little bit of a hack but I need this to be placed under the active nic settings
  56. # also look in /etc/netplan for other config files
  57. sed -i 's/optional: true/dhcp-identifier: mac/g' /etc/netplan/50-cloud-init.yaml
  58.  
  59. # cleans out all of the cloud-init cache / logs - this is mainly cleaning out networking info
  60. sudo cloud-init clean --logs
  61.  
  62. #cleanup shell history
  63. cat /dev/null > ~/.bash_history && history -c
  64. history -w