Last active
January 7, 2025 15:19
-
-
Save allanbatista/bd04f237a3bafa44f8826c578684e349 to your computer and use it in GitHub Desktop.
Change hostname on first linux boot
This file contains 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
chmod +x /usr/local/bin/first-boot.sh | |
systemctl daemon-reload | |
systemctl enable first-boot.service |
This file contains 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
# /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 |
This file contains 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/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