diff --git a/init_vps.sh b/init_vps.sh new file mode 100644 index 0000000..d19c6dd --- /dev/null +++ b/init_vps.sh @@ -0,0 +1,168 @@ +#!/bin/bash + +# ===== Package Definitions ===== # +COMMON_PKGS="git curl wget tmux nano net-tools unzip zip gnupg tzdata" +MONITORING_PKGS="htop btop" +ALPINE_PKGS="git curl wget tmux nano net-tools unzip zip gnupg htop tzdata" +ALPINE_BTOP="btop" + +# Docker dependencies +DEBIAN_DOCKER_PKGS="ca-certificates software-properties-common apt-transport-https" +RHEL_DOCKER_PKGS="yum-utils" +FEDORA_DOCKER_PKGS="dnf-plugins-core" +ALPINE_DOCKER_PKGS="docker docker-cli docker-compose" + +# Docker packages +DOCKER_PKGS="docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin" + +# Automatic update packages to remove +DEBIAN_AUTO_UPDATE_PKGS="unattended-upgrades" +RHEL_AUTO_UPDATE_PKGS="dnf-automatic yum-cron" +ALPINE_AUTO_UPDATE_PKGS="" + +# ===== Timezone Configuration ===== # +set_utc_timezone() { + echo "šŸ•’ Setting UTC timezone..." + case $DISTRO in + debian|ubuntu) + ln -fs /usr/share/zoneinfo/UTC /etc/localtime + dpkg-reconfigure -f noninteractive tzdata + ;; + centos|rhel|rocky|almalinux|fedora) + timedatectl set-timezone UTC + ;; + alpine) + ln -fs /usr/share/zoneinfo/UTC /etc/localtime + ;; + arch) + ln -sf /usr/share/zoneinfo/UTC /etc/localtime + hwclock --systohc + ;; + esac + echo "Timezone set to UTC" +} + +# ===== Remove Automatic Updates ===== # +remove_auto_updates() { + echo "šŸ”§ Removing automatic update packages..." + case $DISTRO in + debian|ubuntu) + apt purge -y $DEBIAN_AUTO_UPDATE_PKGS + ;; + centos|rhel|rocky|almalinux|fedora) + yum remove -y $RHEL_AUTO_UPDATE_PKGS + ;; + esac + echo "Automatic update packages removed" +} + +# ===== Main Script ===== # +# Detect distribution +if [ -f /etc/os-release ]; then + . /etc/os-release + DISTRO=$ID + [ "$ID" = "alpine" ] && DISTRO="alpine" +elif [ -f /etc/redhat-release ]; then + DISTRO="rhel" +elif [ -f /etc/alpine-release ]; then + DISTRO="alpine" +else + echo "āŒ Unsupported Linux distribution!" + exit 1 +fi + +# Install packages +echo "šŸ” Updating system packages..." +case $DISTRO in + debian|ubuntu) + apt update -y && apt upgrade -y + apt install -y $COMMON_PKGS $MONITORING_PKGS $DEBIAN_DOCKER_PKGS + ;; + centos|rhel|rocky|almalinux) + yum update -y + yum install -y epel-release + yum install -y $COMMON_PKGS $MONITORING_PKGS $RHEL_DOCKER_PKGS + ;; + fedora) + dnf update -y + dnf install -y $COMMON_PKGS $MONITORING_PKGS $FEDORA_DOCKER_PKGS + ;; + arch) + pacman -Syu --noconfirm + pacman -S --noconfirm $COMMON_PKGS $MONITORING_PKGS + ;; + alpine) + apk update + apk add $ALPINE_PKGS + sed -i 's/http:\/\/dl-cdn.alpinelinux.org/https:\/\/dl-cdn.alpinelinux.org/g' /etc/apk/repositories + echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories + apk update + apk add $ALPINE_BTOP + ;; +esac + +# Remove automatic updates +remove_auto_updates + +# Set UTC timezone +set_utc_timezone + +# Install Docker +echo "🐳 Installing Docker..." +case $DISTRO in + debian|ubuntu) + install -m 0755 -d /etc/apt/keyrings + curl -fsSL https://download.docker.com/linux/$DISTRO/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + chmod a+r /etc/apt/keyrings/docker.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/$DISTRO $(. /etc/os-release && echo "$VERSION_CODENAME") stable" > /etc/apt/sources.list.d/docker.list + apt update -y + apt install -y $DOCKER_PKGS + ;; + centos|rhel|rocky|almalinux) + yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo + yum install -y $DOCKER_PKGS + ;; + fedora) + dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo + dnf install -y $DOCKER_PKGS + ;; + arch) + pacman -S --noconfirm docker + ;; + alpine) + apk add $ALPINE_DOCKER_PKGS + ;; +esac + +# Start Docker +echo "⚔ Starting Docker..." +case $DISTRO in + alpine) + service docker start + rc-update add docker boot + ;; + arch) + systemctl enable --now docker.service + ;; + *) + systemctl enable --now docker + ;; +esac + +# Configure user +CURRENT_USER=$(who am i | awk '{print $1}') +echo "šŸ‘„ Adding user $CURRENT_USER to Docker group..." +case $DISTRO in + alpine) + addgroup $CURRENT_USER docker + ;; + *) + usermod -aG docker $CURRENT_USER + ;; +esac + +echo -e "\nāœ… Setup Complete!" +echo "Installed: $COMMON_PKGS $MONITORING_PKGS Docker" +echo "Removed: Automatic update packages" +echo "Timezone: UTC" +echo -e "\nā— Re-login as $CURRENT_USER for changes to take effect."