mirror of
https://github.com/marcogll/scripts_mg.git
synced 2026-01-13 13:25:15 +00:00
Update auto_server_setup.sh
This commit is contained in:
@@ -1,130 +1,133 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# auto_server_setup.sh — Marco G. & ChatGPT — 2025-04-28
|
# auto_server_setup_extended.sh — 2025-04-30
|
||||||
# Prepara un home-server en Ubuntu 22.04/24.04 de forma (semi)automática.
|
# Configura un home-server Ubuntu 22.04/24.04 con Docker, Portainer,
|
||||||
|
# ZeroTier, Tailscale, Plex, Samba, Oh-My-Zsh, Oh-My-Posh, etc.
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
LOG(){ printf "\n\e[1;32m▶ %s\e[0m\n" "$*"; }
|
################################################################################
|
||||||
|
# Barra de progreso simple (texto) #
|
||||||
|
################################################################################
|
||||||
|
STEPS_TOTAL=12 # ajusta si añades/eliminas pasos
|
||||||
|
STEP_NOW=0
|
||||||
|
bar() {
|
||||||
|
local width=50
|
||||||
|
local filled=$(( STEP_NOW*width/STEPS_TOTAL ))
|
||||||
|
local empty=$(( width-filled ))
|
||||||
|
printf "\r[%s%s] %3d%% %s" \
|
||||||
|
"$(printf '%0.s#' $(seq 1 $filled))" \
|
||||||
|
"$(printf '%0.s-' $(seq 1 $empty))" \
|
||||||
|
$(( STEP_NOW*100/STEPS_TOTAL )) \
|
||||||
|
"$1"
|
||||||
|
}
|
||||||
|
next() { STEP_NOW=$(( STEP_NOW+1 )); bar "$1"; echo; }
|
||||||
|
LOG() { echo -e "\n\033[1;32m▶ $*\033[0m"; }
|
||||||
|
|
||||||
##########################
|
################################################################################
|
||||||
# 0. Comprobaciones previas
|
# Comprobaciones previas #
|
||||||
##########################
|
################################################################################
|
||||||
if [[ "$(id -u)" -ne 0 ]]; then
|
if [[ "$(id -u)" -ne 0 ]]; then
|
||||||
echo "⚠️ Este script debe ejecutarse como root (sudo)."
|
echo "⚠️ Este script debe ejecutarse como root (sudo)." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
############ CONFIGURACIÓN INTERACTIVA ############
|
################################################################################
|
||||||
echo -e "\n--- Configuración interactiva ---"
|
# 0. Hostname y recordatorio Oh-My-Posh profile #
|
||||||
|
################################################################################
|
||||||
# 1) Usuario del sistema
|
next "Configurando hostname"
|
||||||
DEFAULT_USER="${SUDO_USER:-$USER}"
|
read -rp "➤ Nombre para el servidor (hostname): " NEW_HOST
|
||||||
read -rp "➤ Usuario a configurar [$DEFAULT_USER]: " TMP_USER
|
[ -n "$NEW_HOST" ] && {
|
||||||
SERVER_USER="${TMP_USER:-$DEFAULT_USER}"
|
echo "$NEW_HOST" > /etc/hostname
|
||||||
|
sed -i "s/127.0.1.1.*/127.0.1.1\t$NEW_HOST/" /etc/hosts || true
|
||||||
# 2) ¿Instalar Pi-hole?
|
hostname "$NEW_HOST"
|
||||||
read -rp "➤ ¿Instalar Pi-hole? [Y/n]: " TMP_PIHOLE
|
|
||||||
INSTALL_PIHOLE="$( [[ ${TMP_PIHOLE,,} =~ ^n ]] && echo no || echo yes )"
|
|
||||||
|
|
||||||
# 3) ¿Instalar CasaOS?
|
|
||||||
read -rp "➤ ¿Instalar CasaOS? [Y/n]: " TMP_CASAOS
|
|
||||||
INSTALL_CASAOS="$( [[ ${TMP_CASAOS,,} =~ ^n ]] && echo no || echo yes )"
|
|
||||||
|
|
||||||
# 4) ¿Reiniciar automáticamente al terminar?
|
|
||||||
read -rp "➤ ¿Reiniciar automáticamente al terminar? [Y/n]: " TMP_REBOOT
|
|
||||||
AUTO_REBOOT="$( [[ ${TMP_REBOOT,,} =~ ^n ]] && echo no || echo yes )"
|
|
||||||
|
|
||||||
echo -e "\nResumen:"
|
|
||||||
echo " SERVER_USER = $SERVER_USER"
|
|
||||||
echo " INSTALL_PIHOLE= $INSTALL_PIHOLE"
|
|
||||||
echo " INSTALL_CASAOS= $INSTALL_CASAOS"
|
|
||||||
echo " AUTO_REBOOT = $AUTO_REBOOT"
|
|
||||||
echo "-----------------------------------"
|
|
||||||
sleep 2
|
|
||||||
#####################################################
|
|
||||||
|
|
||||||
###################################
|
|
||||||
# 1. Base APT + actualizaciones
|
|
||||||
###################################
|
|
||||||
install_base() {
|
|
||||||
LOG "Actualizando APT y herramientas básicas…"
|
|
||||||
export DEBIAN_FRONTEND=noninteractive
|
|
||||||
apt update && apt -y full-upgrade
|
|
||||||
apt install -y git curl gnupg lsb-release nano \
|
|
||||||
ca-certificates software-properties-common \
|
|
||||||
apt-transport-https build-essential ufw
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###################################
|
echo -e "\n📝 Copia tu perfil Oh-My-Posh (.omp.json) a ~/ antes de continuar"
|
||||||
# 2. Zsh + Oh-My-Zsh + autosuggestions
|
read -rp " (pulsa Enter para seguir)… "
|
||||||
###################################
|
|
||||||
install_shell() {
|
|
||||||
LOG "Instalando Zsh y Oh-My-Zsh…"
|
|
||||||
apt install -y zsh
|
|
||||||
sudo -u "$SERVER_USER" mkdir -p /tmp
|
|
||||||
sudo -u "$SERVER_USER" curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh \
|
|
||||||
-o /tmp/install-ohmyzsh.sh
|
|
||||||
sudo -u "$SERVER_USER" bash /tmp/install-ohmyzsh.sh --unattended
|
|
||||||
chsh -s "$(command -v zsh)" "$SERVER_USER"
|
|
||||||
|
|
||||||
LOG "Instalando plugin zsh-autosuggestions…"
|
################################################################################
|
||||||
|
# 1. Preguntas interactivas generales #
|
||||||
|
################################################################################
|
||||||
|
next "Preguntas iniciales"
|
||||||
|
DEFAULT_USER="${SUDO_USER:-$USER}"
|
||||||
|
read -rp "➤ Usuario Linux a configurar [$DEFAULT_USER]: " TMP
|
||||||
|
SERVER_USER="${TMP:-$DEFAULT_USER}"
|
||||||
|
|
||||||
|
read -rp "➤ Instalar Pi-hole? [Y/n]: " pih
|
||||||
|
INSTALL_PIHOLE="$( [[ ${pih,,} =~ ^n ]] && echo no || echo yes )"
|
||||||
|
|
||||||
|
read -rp "➤ Instalar CasaOS? [Y/n]: " cas
|
||||||
|
INSTALL_CASAOS="$( [[ ${cas,,} =~ ^n ]] && echo no || echo yes )"
|
||||||
|
|
||||||
|
read -rp "➤ Reinicio automático al final? [Y/n]: " reb
|
||||||
|
AUTO_REBOOT="$( [[ ${reb,,} =~ ^n ]] && echo no || echo yes )"
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# 2. Paquetes base + Zsh + utilidades #
|
||||||
|
################################################################################
|
||||||
|
next "Paquetes base"
|
||||||
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
|
apt update && apt -y full-upgrade
|
||||||
|
apt install -y git curl gnupg lsb-release nano build-essential \
|
||||||
|
ca-certificates software-properties-common \
|
||||||
|
apt-transport-https zsh fzf btop ufw unzip whiptail
|
||||||
|
|
||||||
|
next "Oh-My-Zsh + plugins"
|
||||||
|
sudo -u "$SERVER_USER" sh -c \
|
||||||
|
'curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash -s -- --unattended'
|
||||||
sudo -u "$SERVER_USER" git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions \
|
sudo -u "$SERVER_USER" git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions \
|
||||||
"/home/$SERVER_USER/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
|
"/home/$SERVER_USER/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
|
||||||
sudo -u "$SERVER_USER" sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions)/' \
|
sudo -u "$SERVER_USER" sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions)/' \
|
||||||
"/home/$SERVER_USER/.zshrc"
|
"/home/$SERVER_USER/.zshrc"
|
||||||
}
|
chsh -s "$(command -v zsh)" "$SERVER_USER"
|
||||||
|
|
||||||
###################################
|
################################################################################
|
||||||
# 3. Utilidades extra (fzf, btop)
|
# 3. Oh-My-Posh + Meslo Nerd Font #
|
||||||
###################################
|
################################################################################
|
||||||
install_utils() {
|
next "Oh-My-Posh + Meslo Nerd Font"
|
||||||
LOG "Instalando fzf y btop…"
|
curl -fsSL https://ohmyposh.dev/install.sh | bash -s -- -d /usr/local/bin
|
||||||
apt install -y fzf btop
|
|
||||||
}
|
|
||||||
|
|
||||||
###################################
|
TMP_FONT_DIR=$(mktemp -d)
|
||||||
# 4. Certbot (Let’s Encrypt)
|
curl -fsSL https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/Meslo.zip -o "$TMP_FONT_DIR/meslo.zip"
|
||||||
###################################
|
unzip -q "$TMP_FONT_DIR/meslo.zip" -d "$TMP_FONT_DIR"
|
||||||
install_certbot() {
|
mkdir -p /usr/local/share/fonts
|
||||||
LOG "Instalando Certbot (snap)…"
|
cp "$TMP_FONT_DIR"/*.ttf /usr/local/share/fonts/
|
||||||
|
fc-cache -f
|
||||||
|
|
||||||
|
sudo -u "$SERVER_USER" mkdir -p "/home/$SERVER_USER/.poshthemes"
|
||||||
|
if [[ -f "/home/$SERVER_USER/catppuccin_mocha.omp.json" ]]; then
|
||||||
|
cp "/home/$SERVER_USER/catppuccin_mocha.omp.json" "/home/$SERVER_USER/.poshthemes/"
|
||||||
|
else
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/catppuccin/oh-my-posh/main/themes/catppuccin_mocha.omp.json \
|
||||||
|
-o "/home/$SERVER_USER/.poshthemes/catppuccin_mocha.omp.json"
|
||||||
|
fi
|
||||||
|
chmod 644 "/home/$SERVER_USER/.poshthemes/catppuccin_mocha.omp.json"
|
||||||
|
OMP_LINE='eval "$(oh-my-posh init zsh --config ~/.poshthemes/catppuccin_mocha.omp.json)"'
|
||||||
|
grep -qxF "$OMP_LINE" "/home/$SERVER_USER/.zshrc" || echo "$OMP_LINE" >> "/home/$SERVER_USER/.zshrc"
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# 4. Certbot #
|
||||||
|
################################################################################
|
||||||
|
next "Certbot"
|
||||||
snap install core --classic >/dev/null || true
|
snap install core --classic >/dev/null || true
|
||||||
snap refresh core
|
snap refresh core
|
||||||
snap install --classic certbot
|
snap install --classic certbot
|
||||||
ln -sf /snap/bin/certbot /usr/bin/certbot
|
ln -sf /snap/bin/certbot /usr/bin/certbot
|
||||||
}
|
|
||||||
|
|
||||||
###################################
|
################################################################################
|
||||||
# 5. Docker Engine + compose-plugin
|
# 5. Docker + Portainer + ZeroTier + Tailscale #
|
||||||
###################################
|
################################################################################
|
||||||
install_docker() {
|
next "Docker, Portainer, ZeroTier, Tailscale"
|
||||||
LOG "Instalando Docker Engine…"
|
|
||||||
apt remove -y docker docker.io containerd runc || true
|
|
||||||
install -m0755 -d /etc/apt/keyrings
|
install -m0755 -d /etc/apt/keyrings
|
||||||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||||
gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
|
||||||
source /etc/os-release
|
source /etc/os-release
|
||||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
|
||||||
https://download.docker.com/linux/ubuntu ${VERSION_CODENAME} stable" \
|
https://download.docker.com/linux/ubuntu $VERSION_CODENAME stable" > /etc/apt/sources.list.d/docker.list
|
||||||
> /etc/apt/sources.list.d/docker.list
|
|
||||||
apt update
|
apt update
|
||||||
apt install -y docker-ce docker-ce-cli containerd.io \
|
apt install -y docker-ce docker-ce-cli containerd.io \
|
||||||
docker-buildx-plugin docker-compose-plugin
|
docker-buildx-plugin docker-compose-plugin
|
||||||
usermod -aG docker "$SERVER_USER"
|
usermod -aG docker "$SERVER_USER"
|
||||||
}
|
|
||||||
|
|
||||||
###################################
|
|
||||||
# 6. ZeroTier One
|
|
||||||
###################################
|
|
||||||
install_zerotier() {
|
|
||||||
LOG "Instalando ZeroTier…"
|
|
||||||
curl -s https://install.zerotier.com | bash
|
|
||||||
}
|
|
||||||
|
|
||||||
###################################
|
|
||||||
# 7. Portainer (contenedor Docker)
|
|
||||||
###################################
|
|
||||||
install_portainer() {
|
|
||||||
LOG "Desplegando Portainer CE…"
|
|
||||||
docker volume create portainer_data
|
docker volume create portainer_data
|
||||||
docker run -d --name portainer \
|
docker run -d --name portainer \
|
||||||
-p 8000:8000 -p 9443:9443 \
|
-p 8000:8000 -p 9443:9443 \
|
||||||
@@ -132,70 +135,76 @@ install_portainer() {
|
|||||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||||
-v portainer_data:/data \
|
-v portainer_data:/data \
|
||||||
portainer/portainer-ce:latest
|
portainer/portainer-ce:latest
|
||||||
}
|
|
||||||
|
|
||||||
###################################
|
curl -s https://install.zerotier.com | bash
|
||||||
# 8. CasaOS (opcional)
|
curl -fsSL https://tailscale.com/install.sh | sh
|
||||||
###################################
|
tailscale up --ssh --accept-dns=false || true
|
||||||
install_casaos() {
|
|
||||||
[[ "$INSTALL_CASAOS" == "no" ]] && return
|
################################################################################
|
||||||
LOG "Instalando CasaOS…"
|
# 6. CasaOS (opcional) #
|
||||||
|
################################################################################
|
||||||
|
if [[ "$INSTALL_CASAOS" == "yes" ]]; then
|
||||||
|
next "CasaOS"
|
||||||
curl -fsSL https://get.casaos.io | bash
|
curl -fsSL https://get.casaos.io | bash
|
||||||
}
|
fi
|
||||||
|
|
||||||
###################################
|
################################################################################
|
||||||
# 9. Pi-hole (opcional, nativo)
|
# 7. Pi-hole (opcional) #
|
||||||
###################################
|
################################################################################
|
||||||
install_pihole() {
|
if [[ "$INSTALL_PIHOLE" == "yes" ]]; then
|
||||||
[[ "$INSTALL_PIHOLE" == "no" ]] && return
|
next "Pi-hole"
|
||||||
LOG "Instalando Pi-hole… (modo unattended)"
|
|
||||||
export PIHOLE_SKIP_OS_CHECK=true
|
export PIHOLE_SKIP_OS_CHECK=true
|
||||||
curl -sSL https://install.pi-hole.net | bash -s -- --unattended
|
curl -sSL https://install.pi-hole.net | bash -s -- --unattended
|
||||||
}
|
fi
|
||||||
|
|
||||||
###################################
|
################################################################################
|
||||||
# 10. Plex Media Server (nativo)
|
# 8. Plex Media Server #
|
||||||
###################################
|
################################################################################
|
||||||
install_plex() {
|
next "Plex"
|
||||||
LOG "Instalando Plex Media Server…"
|
curl -fsSL https://downloads.plex.tv/plex-keys/PlexSign.key | gpg --dearmor -o /etc/apt/trusted.gpg.d/plex.gpg
|
||||||
curl -fsSL https://downloads.plex.tv/plex-keys/PlexSign.key | \
|
|
||||||
gpg --dearmor -o /etc/apt/trusted.gpg.d/plex.gpg
|
|
||||||
echo "deb [signed-by=/etc/apt/trusted.gpg.d/plex.gpg] https://downloads.plex.tv/repo/deb/ public main" \
|
echo "deb [signed-by=/etc/apt/trusted.gpg.d/plex.gpg] https://downloads.plex.tv/repo/deb/ public main" \
|
||||||
> /etc/apt/sources.list.d/plexmediaserver.list
|
> /etc/apt/sources.list.d/plexmediaserver.list
|
||||||
apt update
|
apt update && apt install -y plexmediaserver
|
||||||
apt install -y plexmediaserver
|
|
||||||
}
|
|
||||||
|
|
||||||
############################
|
################################################################################
|
||||||
# Ejecución en cascada
|
# 9. Samba #
|
||||||
############################
|
################################################################################
|
||||||
main() {
|
next "Samba"
|
||||||
install_base
|
apt install -y samba
|
||||||
install_shell
|
read -rp "➤ Carpeta a compartir (ruta completa): " SAMBA_DIR
|
||||||
install_utils
|
mkdir -p "$SAMBA_DIR"
|
||||||
install_certbot
|
read -rp "➤ Nombre de usuario Samba: " SAMBA_USER
|
||||||
install_docker
|
read -srp "➤ Contraseña Samba: " SAMBA_PASS; echo
|
||||||
install_zerotier
|
adduser --gecos "" --disabled-password "$SAMBA_USER"
|
||||||
install_portainer
|
echo "$SAMBA_USER:$SAMBA_PASS" | chpasswd
|
||||||
install_casaos
|
(echo "$SAMBA_PASS"; echo "$SAMBA_PASS") | smbpasswd -s -a "$SAMBA_USER"
|
||||||
install_pihole
|
cat >> /etc/samba/smb.conf <<EOF
|
||||||
install_plex
|
|
||||||
|
|
||||||
LOG "🎉 Instalación completa."
|
[$SAMBA_USER-share]
|
||||||
LOG "Accesos:\n • Portainer → https://<IP>:9443\n • CasaOS → http://<IP>\n • Plex → http://<IP>:32400/web\n • Pi-hole → http://<IP>/admin"
|
path = $SAMBA_DIR
|
||||||
|
browseable = yes
|
||||||
|
read only = no
|
||||||
|
guest ok = no
|
||||||
|
valid users = $SAMBA_USER
|
||||||
|
EOF
|
||||||
|
systemctl restart smbd nmbd
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# 10. Resumen y reinicio #
|
||||||
|
################################################################################
|
||||||
|
next "Resumen final"
|
||||||
|
echo -e "\n🔑 Accesos principales:"
|
||||||
|
echo " • Portainer → https://$NEW_HOST:9443"
|
||||||
|
echo " • Plex → http://$NEW_HOST:32400/web"
|
||||||
|
[[ "$INSTALL_PIHOLE" == "yes" ]] && echo " • Pi-hole → http://$NEW_HOST/admin"
|
||||||
|
[[ "$INSTALL_CASAOS" == "yes" ]] && echo " • CasaOS → http://$NEW_HOST"
|
||||||
|
echo " • Samba path → $SAMBA_DIR (usuario: $SAMBA_USER)"
|
||||||
|
echo -e "\n⚠️ Selecciona la fuente «MesloLGS NF» en tu terminal local para ver Oh-My-Posh correctamente."
|
||||||
|
|
||||||
if [[ "$AUTO_REBOOT" == "yes" ]]; then
|
if [[ "$AUTO_REBOOT" == "yes" ]]; then
|
||||||
LOG "Reiniciando en 10 s… (Ctrl-C para abortar)"
|
echo -e "\nReiniciando en 10 s… (Ctrl-C para abortar)"
|
||||||
sleep 10 && reboot
|
sleep 10 && reboot
|
||||||
else
|
else
|
||||||
read -rp $'\n¿Deseas reiniciar ahora? [y/N]: ' REPLY
|
read -rp $'\n¿Reiniciar ahora? [y/N]: ' ans
|
||||||
if [[ ${REPLY,,} == "y" ]]; then
|
[[ ${ans,,} == y ]] && reboot || echo "Reinicio omitido. ¡Instalación completa!"
|
||||||
LOG "Reiniciando…"
|
|
||||||
reboot
|
|
||||||
else
|
|
||||||
LOG "No se reinició. Hazlo manualmente cuando quieras."
|
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user