Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Exit on error
- set -e
- # Check if script is run as root
- if [ "$EUID" -ne 0 ]; then
- echo "Please run as root"
- exit 1
- fi
- # Generate new hostname
- NEW_HOSTNAME="vm-$(cat /dev/urandom | tr -dc 'a-z0-9' | head -c 8)"
- # Set new hostname
- hostnamectl set-hostname "$NEW_HOSTNAME"
- echo "$NEW_HOSTNAME" > /etc/hostname
- # Clear domain name
- sed -i '/^127.0.1.1/d' /etc/hosts
- echo "127.0.1.1 $NEW_HOSTNAME" >> /etc/hosts
- # Reset machine-id
- truncate -s 0 /etc/machine-id
- rm -f /var/lib/dbus/machine-id
- systemd-machine-id-setup
- # Clear SSH keys
- rm -f /etc/ssh/ssh_host_*
- dpkg-reconfigure openssh-server
- # Reset network configuration
- nmcli con delete "$(nmcli -t -f UUID con show)"
- nmcli con add type ethernet ifname eth0 con-name "eth0" autoconnect yes
- # Clear logs
- truncate -s 0 /var/log/*.log
- truncate -s 0 /var/log/**/*.log
- # Remove temporary files
- rm -rf /tmp/*
- rm -rf /var/tmp/*
- echo "VM template reset complete. New hostname: $NEW_HOSTNAME"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement