Last active
November 2, 2025 19:40
-
-
Save l-nmch/14e883ddaad86fd7e7930dcbb2fcef5e to your computer and use it in GitHub Desktop.
Simple script to randomize a machine hostname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # --- CONFIGURATION --- | |
| ADJECTIVES=("brave" "curious" "clever" "swift" "calm" "silent" "bold" "sly" "wild" "gentle" | |
| "bright" "fierce" "happy" "lazy" "mighty" "noisy" "quiet" "shy" "strong" "wise") | |
| ANIMALS=("fox" "wolf" "tiger" "penguin" "bear" "eagle" "deer" "panda" "owl" "lynx" | |
| "lion" "otter" "rabbit" "falcon" "seal" "shark" "whale" "hawk" "cougar" "panther") | |
| # --- GENERATION --- | |
| adjective=${ADJECTIVES[$RANDOM % ${#ADJECTIVES[@]}]} | |
| animal=${ANIMALS[$RANDOM % ${#ANIMALS[@]}]} | |
| new_hostname="${adjective}-${animal}" | |
| old_hostname=$(hostname) | |
| echo "Changing hostname from '$old_hostname' to '$new_hostname'..." | |
| # --- APPLY HOSTNAME --- | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "⚠️ This script must be run as root to change the hostname." | |
| echo "Usage: sudo $0" | |
| exit 1 | |
| fi | |
| hostnamectl set-hostname "$new_hostname" | |
| # --- SAFE /etc/hosts UPDATE --- | |
| if [[ -f /etc/hosts ]]; then | |
| if [[ "$old_hostname" != "localhost" && "$old_hostname" != "localhost.localdomain" ]]; then | |
| # Replace old hostname with new one, but skip any line containing 'localhost' | |
| sed -i "/localhost/! s/\b${old_hostname}\b/${new_hostname}/g" /etc/hosts | |
| else | |
| echo "Skipping /etc/hosts modification to avoid breaking localhost entries." | |
| fi | |
| fi | |
| echo "✅ Hostname successfully changed to: $(hostname)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment