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
- # Check if FQDN is provided
- if [ -z "$1" ]; then
- echo "Please provide an FQDN"
- exit 1
- fi
- # Extract hostname and domain from FQDN
- FQDN="$1"
- NEW_HOSTNAME=$(echo "$FQDN" | cut -d'.' -f1)
- DOMAIN=$(echo "$FQDN" | cut -d'.' -f2-)
- # Set new hostname
- hostnamectl set-hostname "$NEW_HOSTNAME"
- echo "$NEW_HOSTNAME" > /etc/hostname
- # Update hosts file
- sed -i '/^127.0.1.1/d' /etc/hosts
- echo "127.0.1.1 $FQDN $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_*
- if dpkg -l | grep -q openssh-server; then
- dpkg-reconfigure openssh-server
- systemctl restart ssh
- else
- echo "Warning: openssh-server not installed, skipping SSH key regeneration"
- fi
- # Reset network configuration
- # Detect primary network interface
- IFACE=$(ip link | awk -F: '$0 !~ "lo|vir|docker|br-|^[^0-9]"{print $2;getline}' | head -n 1 | xargs)
- if [ -z "$IFACE" ]; then
- echo "Error: No network interface found"
- exit 1
- fi
- # Delete existing connections
- nmcli -t -f UUID con show | while read -r uuid; do
- nmcli con delete "$uuid" 2>/dev/null || true
- done
- # Add new ethernet connection
- nmcli con add type ethernet ifname "$IFACE" con-name "eth0" autoconnect yes
- # Clear logs
- find /var/log -type f -name "*.log" -exec truncate -s 0 {} \;
- # Remove temporary files
- rm -rf /tmp/* /var/tmp/*
- echo "VM template reset complete. New FQDN: $FQDN"
Advertisement
Add Comment
Please, Sign In to add comment