|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Variables |
| 4 | +NEW_USER="youruser" |
| 5 | +NEW_USER_PASSWORD="your-secret-password" |
| 6 | +SSH_PUBLIC_KEY="your-public-key-content" |
| 7 | + |
| 8 | +# Update system |
| 9 | +apt update && apt upgrade -y |
| 10 | + |
| 11 | +# Install required packages |
| 12 | +apt install -y sudo ufw fail2ban unattended-upgrades apt-listchanges |
| 13 | + |
| 14 | +# Configure unattended-upgrades |
| 15 | +cat > /etc/apt/apt.conf.d/20auto-upgrades << EOF |
| 16 | +APT::Periodic::Update-Package-Lists "1"; |
| 17 | +APT::Periodic::Unattended-Upgrade "1"; |
| 18 | +APT::Periodic::Download-Upgradeable-Packages "1"; |
| 19 | +APT::Periodic::AutocleanInterval "7"; |
| 20 | +EOF |
| 21 | + |
| 22 | +cat > /etc/apt/apt.conf.d/50unattended-upgrades << EOF |
| 23 | +Unattended-Upgrade::Origins-Pattern { |
| 24 | + "origin=Debian,codename=\${distro_codename},label=Debian-Security"; |
| 25 | + "origin=Debian,codename=\${distro_codename}-security,label=Debian-Security"; |
| 26 | +}; |
| 27 | +Unattended-Upgrade::AutoFixInterruptedDpkg "true"; |
| 28 | +Unattended-Upgrade::MinimalSteps "true"; |
| 29 | +Unattended-Upgrade::InstallOnShutdown "false"; |
| 30 | +Unattended-Upgrade::Mail "root"; |
| 31 | +Unattended-Upgrade::MailReport "on-change"; |
| 32 | +Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; |
| 33 | +Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; |
| 34 | +Unattended-Upgrade::Remove-Unused-Dependencies "false"; |
| 35 | +Unattended-Upgrade::Automatic-Reboot "true"; |
| 36 | +Unattended-Upgrade::Automatic-Reboot-Time "02:00"; |
| 37 | +EOF |
| 38 | + |
| 39 | +# Enable unattended-upgrades |
| 40 | +systemctl enable unattended-upgrades |
| 41 | +systemctl start unattended-upgrades |
| 42 | + |
| 43 | +# Create new user and add to sudo group |
| 44 | +useradd -m -s /bin/bash $NEW_USER |
| 45 | +echo "$NEW_USER:$NEW_USER_PASSWORD" | chpasswd |
| 46 | +usermod -aG sudo $NEW_USER |
| 47 | + |
| 48 | +# Setup SSH key for new user |
| 49 | +mkdir -p /home/$NEW_USER/.ssh |
| 50 | +echo "$SSH_PUBLIC_KEY" > /home/$NEW_USER/.ssh/authorized_keys |
| 51 | +chmod 700 /home/$NEW_USER/.ssh |
| 52 | +chmod 600 /home/$NEW_USER/.ssh/authorized_keys |
| 53 | +chown -R $NEW_USER:$NEW_USER /home/$NEW_USER/.ssh |
| 54 | + |
| 55 | +# Configure SSH |
| 56 | +sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config |
| 57 | +sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config |
| 58 | +sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config |
| 59 | + |
| 60 | +# Configure fail2ban |
| 61 | +cat > /etc/fail2ban/jail.local << EOF |
| 62 | +[sshd] |
| 63 | +enabled = true |
| 64 | +port = ssh |
| 65 | +filter = sshd |
| 66 | +logpath = /var/log/auth.log |
| 67 | +maxretry = 3 |
| 68 | +bantime = 3600 |
| 69 | +findtime = 600 |
| 70 | +EOF |
| 71 | + |
| 72 | +# Configure firewall |
| 73 | +ufw default deny incoming |
| 74 | +ufw default allow outgoing |
| 75 | +ufw allow ssh |
| 76 | +ufw allow http |
| 77 | +ufw allow https |
| 78 | +echo "y" | ufw enable |
| 79 | + |
| 80 | +# Install Docker |
| 81 | +apt install -y ca-certificates curl gnupg |
| 82 | +install -m 0755 -d /etc/apt/keyrings |
| 83 | +curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg |
| 84 | +chmod a+r /etc/apt/keyrings/docker.gpg |
| 85 | + |
| 86 | +echo \ |
| 87 | + "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \ |
| 88 | + "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ |
| 89 | + tee /etc/apt/sources.list.d/docker.list > /dev/null |
| 90 | + |
| 91 | +apt update |
| 92 | +apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin |
| 93 | + |
| 94 | +# Add user to docker group |
| 95 | +usermod -aG docker $NEW_USER |
| 96 | + |
| 97 | +# Restart services |
| 98 | +systemctl restart sshd |
| 99 | +systemctl restart fail2ban |
| 100 | + |
| 101 | +echo "Setup completed!" |
0 commit comments