Server Setup
You just got a fresh VPS. It's running Ubuntu, has a root password, and that's it. Let's make it production-ready.
Step 1: First Login
Step 2: Update Everything
Step 3: Create a Regular User
Never work as root. Create a user with sudo access:
Step 4: Set Up SSH Keys
On your local machine:
Or manually on the server:
Step 5: Secure SSH
Change these settings:
# Disable root login
PermitRootLogin no
# Disable password authentication
PasswordAuthentication no
# Optionally change port
# Port 2222
Don't Lock Yourself Out
Before disabling password auth, make sure your SSH key works! Open a new terminal and test logging in with the key.
Step 6: Set Up Firewall
Step 7: Install Essential Tools
Step 8: Configure fail2ban
Protect against brute force attacks:
Step 9: Set Timezone
Step 10: Automatic Security Updates
Server Setup Checklist
□ System updated
□ Non-root user created
□ User added to sudo group
□ SSH keys configured
□ Root login disabled
□ Password auth disabled
□ Firewall enabled (SSH allowed)
□ fail2ban installed
□ Timezone set
□ Automatic updates enabled
Install a Web Server
Nginx
Let's Encrypt SSL
Complete Setup Script
#!/bin/bash
# server-setup.sh - Run as root on fresh Ubuntu
set -euo pipefail
USERNAME="deploy"
# Update system
apt update && apt upgrade -y
# Create user
adduser --gecos "" "$USERNAME"
usermod -aG sudo "$USERNAME"
# SSH hardening
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
# Install essentials
apt install -y git curl wget vim htop unzip fail2ban ufw
# Firewall
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable
# Enable services
systemctl enable --now fail2ban
# Timezone
timedatectl set-timezone UTC
# Automatic updates
apt install -y unattended-upgrades
echo 'Unattended-Upgrade::Automatic-Reboot "false";' > /etc/apt/apt.conf.d/50unattended-upgrades-local
echo "Setup complete!"
echo "Remember to:"
echo "1. Add SSH key to /home/$USERNAME/.ssh/authorized_keys"
echo "2. Test SSH login before disconnecting"
echo "3. Run: systemctl restart sshd"
Why should you add your SSH key before disabling password authentication?
Key Takeaways
- Never work as root - create a sudo user
- SSH keys before disabling password auth
- UFW is simple but effective firewall
- fail2ban protects against brute force
- Automatic security updates are essential
- Test connectivity at each step
Next: introduction to Docker.