Last active
November 22, 2025 12:12
-
-
Save delamux/ffa1037bcc0db6e75ec3f1352eaa308f to your computer and use it in GitHub Desktop.
Setup VPS
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
| #!/bin/bash | |
| # VPS Initial Setup Script | |
| # Run this on your LOCAL machine, it will configure the remote VPS | |
| # ⚠️ This script have been generated with the Help of AI, please verify first on a local machine. | |
| set -e # Exit on any error | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| echo -e "${GREEN}=== VPS Initial Setup Script ===${NC}\n" | |
| # Prompt for information | |
| read -p "Enter VPS IP address: " VPS_IP | |
| read -p "Enter root password: " -s ROOT_PASSWORD | |
| echo "" | |
| read -p "Enter new sudo username (e.g., baker): " DEPLOY_USER | |
| read -p "Enter your local SSH public key path (default: ~/.ssh/id_ed25519.pub): " SSH_KEY_PATH | |
| SSH_KEY_PATH=${SSH_KEY_PATH:-~/.ssh/id_ed25519.pub} | |
| # Validate SSH key exists | |
| if [ ! -f "$SSH_KEY_PATH" ]; then | |
| echo -e "${RED}Error: SSH public key not found at $SSH_KEY_PATH${NC}" | |
| echo "Generate one with: ssh-keygen -t ed25519 -C 'your_email@example.com'" | |
| exit 1 | |
| fi | |
| SSH_PUB_KEY=$(cat "$SSH_KEY_PATH") | |
| echo -e "\n${YELLOW}Configuration:${NC}" | |
| echo "VPS IP: $VPS_IP" | |
| echo "Deploy User: $DEPLOY_USER" | |
| echo "SSH Key: $SSH_KEY_PATH" | |
| read -p "Continue? (y/n): " -n 1 -r | |
| echo "" | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| exit 1 | |
| fi | |
| echo -e "\n${GREEN}Starting VPS configuration...${NC}\n" | |
| # Create a temporary script to run on the remote server | |
| cat > /tmp/vps_setup.sh <<'SCRIPT_EOF' | |
| #!/bin/bash | |
| set -e | |
| DEPLOY_USER=$1 | |
| SSH_PUB_KEY="$2" | |
| echo "=== Updating system ===" | |
| apt update && apt upgrade -y | |
| echo "=== Installing essential tools ===" | |
| apt install -y \ | |
| git \ | |
| curl \ | |
| wget \ | |
| htop \ | |
| tmux \ | |
| unattended-upgrades \ | |
| vim \ | |
| ca-certificates | |
| echo "=== Creating deploy user: $DEPLOY_USER ===" | |
| if id "$DEPLOY_USER" &>/dev/null; then | |
| echo "User $DEPLOY_USER already exists" | |
| else | |
| adduser --disabled-password --gecos "" $DEPLOY_USER | |
| echo "User $DEPLOY_USER created" | |
| fi | |
| echo "=== Adding user to sudo group ===" | |
| usermod -aG sudo $DEPLOY_USER | |
| echo "=== Setting up SSH key authentication ===" | |
| mkdir -p /home/$DEPLOY_USER/.ssh | |
| echo "$SSH_PUB_KEY" > /home/$DEPLOY_USER/.ssh/authorized_keys | |
| chmod 700 /home/$DEPLOY_USER/.ssh | |
| chmod 600 /home/$DEPLOY_USER/.ssh/authorized_keys | |
| chown -R $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.ssh | |
| echo "=== Configuring SSH (disable password auth) ===" | |
| sed -i 's/#*PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config | |
| sed -i 's/#*PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config | |
| sed -i 's/#*PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config | |
| echo "=== Configuring Fail2Ban ===" | |
| # cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local | |
| # systemctl enable fail2ban | |
| # systemctl start fail2ban | |
| echo "=== Configuring automatic security updates ===" | |
| echo 'Unattended-Upgrade::Automatic-Reboot "false";' >> /etc/apt/apt.conf.d/50unattended-upgrades | |
| dpkg-reconfigure -plow unattended-upgrades | |
| echo "=== Installing Docker ===" | |
| # Add Docker's official GPG key | |
| install -m 0755 -d /etc/apt/keyrings | |
| curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc | |
| chmod a+r /etc/apt/keyrings/docker.asc | |
| # Add the repository to Apt sources | |
| echo \ | |
| "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ | |
| $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ | |
| tee /etc/apt/sources.list.d/docker.list > /dev/null | |
| apt update | |
| apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin | |
| echo "=== Adding user to docker group ===" | |
| usermod -aG docker $DEPLOY_USER | |
| echo "=== Restarting SSH service ===" | |
| systemctl restart sshd | |
| echo "=== Setup completed successfully! ===" | |
| echo "You can now connect with: ssh $DEPLOY_USER@$(hostname -I | awk '{print $1}')" | |
| SCRIPT_EOF | |
| # Copy and execute the script on the remote server | |
| echo -e "${YELLOW}Connecting to VPS and running setup...${NC}" | |
| ssh -p "$ROOT_PASSWORD" scp /tmp/vps_setup.sh root@$VPS_IP:/tmp/ | |
| ssh -p "$ROOT_PASSWORD" ssh root@$VPS_IP "bash /tmp/vps_setup.sh '$DEPLOY_USER' '$SSH_PUB_KEY'" | |
| # Clean up | |
| rm /tmp/vps_setup.sh | |
| echo -e "\n${GREEN}=== VPS Setup Complete! ===${NC}\n" | |
| echo "Next steps:" | |
| echo "1. Test SSH connection: ssh $DEPLOY_USER@$VPS_IP" | |
| echo "2. Add to your SSH config (~/.ssh/config):" | |
| echo "" | |
| echo "Host myvps" | |
| echo " HostName $VPS_IP" | |
| echo " User $DEPLOY_USER" | |
| echo " IdentityFile $SSH_KEY_PATH" | |
| echo " IdentitiesOnly yes" | |
| echo "" | |
| echo "3. Then connect with: ssh myvps" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment