Advertisement
NOVACyclist

Ubuntu Clone Hostname Reset

Jul 18th, 2025 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Exit on error
  4. set -e
  5.  
  6. # Check if script is run as root
  7. if [ "$EUID" -ne 0 ]; then
  8.   echo "Please run as root"
  9.   exit 1
  10. fi
  11.  
  12. # Generate new hostname
  13. NEW_HOSTNAME="vm-$(cat /dev/urandom | tr -dc 'a-z0-9' | head -c 8)"
  14.  
  15. # Set new hostname
  16. hostnamectl set-hostname "$NEW_HOSTNAME"
  17. echo "$NEW_HOSTNAME" > /etc/hostname
  18.  
  19. # Clear domain name
  20. sed -i '/^127.0.1.1/d' /etc/hosts
  21. echo "127.0.1.1 $NEW_HOSTNAME" >> /etc/hosts
  22.  
  23. # Reset machine-id
  24. truncate -s 0 /etc/machine-id
  25. rm -f /var/lib/dbus/machine-id
  26. systemd-machine-id-setup
  27.  
  28. # Clear SSH keys
  29. rm -f /etc/ssh/ssh_host_*
  30. dpkg-reconfigure openssh-server
  31.  
  32. # Reset network configuration
  33. nmcli con delete "$(nmcli -t -f UUID con show)"
  34. nmcli con add type ethernet ifname eth0 con-name "eth0" autoconnect yes
  35.  
  36. # Clear logs
  37. truncate -s 0 /var/log/*.log
  38. truncate -s 0 /var/log/**/*.log
  39.  
  40. # Remove temporary files
  41. rm -rf /tmp/*
  42. rm -rf /var/tmp/*
  43.  
  44. echo "VM template reset complete. New hostname: $NEW_HOSTNAME"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement