You just spun up a fresh VPS. Root password in hand, default SSH on port 22, firewall wide open—attackers will find it in minutes. I've seen brute-force attempts start within an hour of a server going live, sometimes faster.
This guide walks through the exact commands to lock down a new Ubuntu or Debian VPS before you deploy anything. SSH key authentication, a basic firewall, fail2ban to block repeat offenders, and automatic security patches. Twelve minutes if you type fast, fifteen if you're careful. No fluff, just the steps that matter.
Create a non-root user with sudo
Never run daily tasks as root. Create a regular user, give it sudo rights, then disable root login over SSH.
Log in as root:
ssh root@your_server_ip
Create the user and add it to the sudo group:
adduser deploy
usermod -aG sudo deploy
Pick a strong password when prompted. You'll replace password auth with keys in the next step, but set one anyway—it's required for sudo.
Set up SSH key authentication
Password authentication is too easy to brute-force. SSH keys stop that cold.
On your local machine (not the server), generate a key pair if you don't have one:
ssh-keygen -t ed25519 -C "[email protected]"
Press Enter to accept the default location. Set a passphrase or leave it empty—your call. Ed25519 keys are shorter and faster than RSA.
Copy the public key to your server:
ssh-copy-id deploy@your_server_ip
Enter the password you just set for deploy. The command appends your public key to ~/.ssh/authorized_keys on the server.
Test the key:
ssh deploy@your_server_ip
You should log in without a password prompt (or just the passphrase for your local key if you set one). If that works, you're ready to disable password auth.
Harden the SSH daemon
Open the SSH config:
sudo nano /etc/ssh/sshd_config
Find or add these lines:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PermitRootLogin no blocks root login entirely. PasswordAuthentication no forces key-based auth. Save and exit (Ctrl+O, Enter, Ctrl+X in nano).
Restart SSH:
sudo systemctl restart sshd
Before you close your current session, open a second terminal and test login as deploy with your key. If it works, you're safe. If not, you still have the first session open to fix it.
Configure the firewall with UFW
Ubuntu and Debian ship with UFW (Uncomplicated Firewall), a front-end for iptables. Default policy: deny all incoming, allow all outgoing.
Allow SSH first—lock yourself out and you'll need console access to recover:
sudo ufw allow OpenSSH
If you changed SSH to a non-standard port (say, 2222), allow that port number instead:
sudo ufw allow 2222/tcp
Allow HTTP and HTTPS if you're running a web server:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Enable the firewall:
sudo ufw enable
Confirm "yes" when prompted. Check status:
sudo ufw status verbose
You should see rules for SSH (or your custom port), 80, and 443. Everything else is blocked by default.
Install and configure fail2ban
Fail2ban watches log files for repeated failed login attempts and bans the offending IP for a set period. Even with keys, it's useful—prevents log spam and catches other services you might add later.
Install:
sudo apt update
sudo apt install fail2ban -y
Fail2ban reads /etc/fail2ban/jail.conf, but you should never edit that file directly—it gets overwritten on updates. Create a local override:
sudo nano /etc/fail2ban/jail.local
Paste this:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
[sshd]
enabled = true
port = ssh
logpath = /var/log/auth.log
bantime is one hour (3600 seconds). findtime is the window (600 seconds = 10 minutes). maxretry is five failures before a ban. Adjust to taste.
If you changed SSH to a custom port, replace port = ssh with port = 2222 (or your actual port).
Restart fail2ban:
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban
Check status:
sudo fail2ban-client status sshd
You'll see current bans (probably zero on a fresh server) and total bans over time.
Enable automatic security updates
Manual patching is fine if you check in daily. Most people don't. Unattended upgrades install security fixes automatically—major version upgrades still need your attention, but critical patches apply themselves.
Install the package:
sudo apt install unattended-upgrades -y
Enable it:
sudo dpkg-reconfigure -plow unattended-upgrades
Select "Yes" when prompted. The system will now pull security updates from the Ubuntu or Debian security repository and apply them overnight.
If you want more control, edit /etc/apt/apt.conf.d/50unattended-upgrades. The defaults are sensible: security updates only, no automatic reboots (though you can enable that if you want).
Verify everything
Run through a quick checklist:
- SSH login with your key works, password auth fails
- Root login over SSH is blocked
- UFW is active and allows only the ports you need
- Fail2ban is running and monitoring SSH
- Unattended upgrades is enabled
From your local machine, try logging in as root:
ssh root@your_server_ip
You should see "Permission denied (publickey)." Good.
Check UFW status on the server:
sudo ufw status
Verify fail2ban is watching:
sudo systemctl status fail2ban
If all three pass, you're done with the basics.
What about other services?
This guide covers SSH and the firewall. When you install a web server, database, or mail daemon, you'll need to open additional ports and possibly configure fail2ban jails for those services.
For Nginx or Apache, port 80 and 443 are already open. For MySQL or PostgreSQL, bind to localhost (127.0.0.1) unless you specifically need remote access—opening database ports to the internet is asking for trouble.
Fail2ban ships with jail configs for Apache, Nginx, Postfix, and others in /etc/fail2ban/jail.conf. Copy the relevant sections to jail.local and enable them. Don't expose services you're not actively using.
Optional: change the SSH port
Moving SSH off port 22 cuts down on automated scans. It's security through obscurity—not a replacement for keys and fail2ban, but it does reduce log noise.
Edit /etc/ssh/sshd_config:
sudo nano /etc/ssh/sshd_config
Find the line #Port 22, uncomment it, and change the number:
Port 2222
Pick something above 1024 and below 65535. Avoid common alternate ports like 2222 or 8022 if you want fewer bots knocking.
Update UFW before you restart SSH:
sudo ufw allow 2222/tcp
sudo ufw delete allow OpenSSH
sudo systemctl restart sshd
Test login with the new port:
ssh -p 2222 deploy@your_server_ip
If it works, update your fail2ban jail.local to match the new port.
Keep it running
You've built a solid foundation: key-based SSH, a deny-by-default firewall, automated bans for repeat offenders, and security patches that install themselves. The server won't stay secure forever—new services, config drift, and forgotten ports will chip away at it—but you've bought yourself time.
Check logs weekly (/var/log/auth.log for SSH, /var/log/fail2ban.log for bans). Watch for unusual login attempts or services you didn't install. Run sudo ufw status and sudo fail2ban-client status now and then to confirm everything's still active.
Twelve minutes well spent.
