Skip to content

Instantly share code, notes, and snippets.

@allanbatista
Last active January 7, 2025 15:19
Show Gist options
  • Save allanbatista/bd04f237a3bafa44f8826c578684e349 to your computer and use it in GitHub Desktop.
Save allanbatista/bd04f237a3bafa44f8826c578684e349 to your computer and use it in GitHub Desktop.
Change hostname on first linux boot
chmod +x /usr/local/bin/first-boot.sh
systemctl daemon-reload
systemctl enable first-boot.service
# /etc/systemd/system/first-boot.service
[Unit]
Description=Script para rodar uma única vez no primeiro boot
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/first-boot.sh
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl enable first-boot.service
# /usr/local/bin/first-boot.sh
# Este script será executado apenas no primeiro boot e alterará o hostname para uma string aleatória
set -e
# 1. Alterar hostname para server{timestamp}
NOVO_HOSTNAME=$(head -c 256 /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)
hostnamectl set-hostname "node-$NOVO_HOSTNAME"
# 2. Desabilitar este serviço para não rodar novamente
systemctl disable first-boot.service
# 3. (Opcional) Remover o próprio script para não ficar no sistema
# rm -f /usr/local/bin/first-boot.sh
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment