Ubuntu Server Setup Script Generator
Choose what you want on a new Ubuntu server and copy one script that sets it up. Nothing you type leaves your browser.
setup.sh
#!/usr/bin/env bash
# First-time VPS setup for Ubuntu 22.04 and 24.04
# Made with https://giganodes.host/tools/vps/setup-script
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "Run this as root: sudo bash setup.sh"; exit 1; }
. /etc/os-release
[ "$ID" = "ubuntu" ] || { echo "This script is for Ubuntu."; exit 1; }
export DEBIAN_FRONTEND=noninteractive
apt_do() { apt-get -y -o DPkg::Lock::Timeout=300 -o Dpkg::Options::=--force-confold "$@"; }
echo "==> Updating packages"
apt_do update
apt_do upgrade
echo "==> Setting the clock to India time"
timedatectl set-timezone Asia/Kolkata
echo "==> Adding 2GB swap"
if [ -z "$(swapon --show --noheadings)" ] && [ ! -e /swapfile ]; then
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab
fi
echo "==> Turning on the firewall"
apt_do install ufw
mkdir -p /run/sshd
ssh_ports="$(sshd -T | awk '$1 == "port" {print $2}')"
for port in ${ssh_ports:-22}; do ufw allow "$port/tcp"; done
ufw --force enable
echo "==> Blocking repeated failed logins"
apt_do install fail2ban
systemctl enable --now fail2ban
echo "==> Turning on automatic security updates"
apt_do install unattended-upgrades
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
echo
echo "Done."
[ ! -f /var/run/reboot-required ] || echo "Restart the server to finish the updates: reboot"
Common questions
How do I run the script?
Log in to your server as root, type nano setup.sh, paste the script and save it. Then run bash setup.sh.
Which systems does it work on?
Ubuntu 22.04 and 24.04. Use it on a new server. If yours already runs websites or apps, read the script first, because the firewall closes every port you don't list.
Can it lock me out of my server?
The script opens your SSH port before it turns on the firewall. If you turn off password login, keep your current window open until you've logged in with your key in a new one.
Does the firewall cover Docker?
Not fully. Ports you publish with Docker skip UFW. To keep one private, publish it as 127.0.0.1:3000:3000.
Need a server to run it on?
AMD EPYC VPS in India from ₹600/mo, with full root access and UPI.
See VPS plans