From Zero to Server Hero: A Fun Guide to Securing Your Ubuntu VPS
So, you've just spun up a brand new Virtual Private Server. It’s a pristine digital canvas, a world of possibilities! But to the rest of the internet, it’s a juicy, unguarded steak sitting in a piranha tank. Before we do anything else, let's build our fortress.
This guide will walk you through the essential steps to transform your vulnerable new server into a hardened bastion of security. We'll even cover the goofy errors that pop up and how to heroically vanquish them.
Step 1: Dethrone the All-Powerful Root User
Right now, you're logging in as root. Think of root as having the keys to the entire kingdom. You can build anything, but you can also accidentally burn the whole thing down with one clumsy typo. We don't want that kind of stress.
Let's create a trusty sidekick—a user for ourselves—and give it sudo powers, which is like asking for the kingdom's keys only when you absolutely need them.
Become your sidekick:
# Create a new user
adduser yourname
# Give admin (sudo) rights
usermod -aG sudo yourname
# Switch to the new user
su - yourname
Grant the superpowers:
usermod -aG sudo pk
Create your sidekick (replace pk with your chosen hero name):
adduser pk
Give your new user a super-strong password. The other info is optional; feel free to just press Enter through it.
Awesome! Now the root user can retire to a comfy castle while we do our work safely.
Step 2: The Unbreakable Lock (SSH Keys)
Passwords are for peasants. They can be guessed, stolen, or brute-forced by internet gremlins. We're going to install a magic lock on our server that only opens with a special key that only you possess.
- The Fix (The Cool, Manual Way):
a. First, grab your public key. On your local machine, display the key and copy the entire output:powershell cat C:\Users\patik\.ssh\id_ed25519.pub
b. Back on your VPS, it's time to build the lockbox. Run these commands one by one:bash mkdir -p ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
c. Now, open theauthorized_keysfile withnano(a simple text editor) and paste your public key inside:bash nano ~/.ssh/authorized_keys
d. Save and exit by pressingCtrl+X, thenY, thenEnter. - Test the magic! From a new local terminal, SSH into your server. It should let you in without a password. Victory!
Install the lock on the server. This is where things can get tricky.
A Wild Error Appears!
If you're on Windows, the handyssh-copy-idcommand doesn't exist! It's a classic "feature." But fear not, doing it manually makes you look way cooler.
Forge the key on your LOCAL computer. Open a new terminal on your home machine and run:
ssh-keygen -t ed25519
type $env:USERPROFILE\.ssh\id_ed25519.pub
This creates the most modern and secure type of key.
Step 3: Fortify the Walls (Harden SSH)
Now that our magic key works, let's tell the server to stop accepting passwords entirely and to slam the door on anyone trying to log in as root.
- Change these two rules:
PasswordAuthentication noPermitRootLogin no
(Make sure to remove the#at the beginning of the lines if they have one!)
The Fix: The service is just called ssh.
sudo systemctl restart ssh
Restart the SSH service.
A Wild Error Appears!
You runsudo systemctl restart sshdand the server yells:Unit sshd.service not found.It turns out Ubuntu is a bit picky about names.
Edit the SSH config file:
sudo nano /etc/ssh/sshd_config
Pro-Tip: ALWAYS test your login from a new terminal window before closing your current one. Getting locked out of your own server is a truly epic fail.
Step 4: The Bouncer (UFW Firewall)
Our server has an open-door policy for every port. Let's hire a bouncer (UFW, the Uncomplicated Firewall) to check IDs.
Put the bouncer on duty:
sudo ufw enable
The Fix: Just tell him the specific services by name.
sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https
Tell the bouncer who's on the VIP list. We need to allow SSH (so we can get in) and web traffic.
A Wild Error Appears!
You try to be fancy and usesudo ufw allow 'WWW Full', but the bouncer just stares blankly:ERROR: Could not find a profile matching 'WWW Full'. He's not a mind reader.
Now, only connections for SSH, HTTP, and HTTPS are allowed in. Everyone else gets the boot.
Step 5: The Robot Blocker (Fail2ban)
Your server is constantly being pelted by bots trying to guess your password. Fail2ban is a security guard that watches for this and instantly bans any IP that tries and fails too many times.
sudo apt install fail2ban -y
That's it. It starts working right out of the box. Easiest security win of the day!
Step 6: The Secret Handshake (2FA)
This is the final, most spy-movie step. To get in, you'll need your magic key and a secret, constantly changing code from your phone. It's the ultimate security combo.
- Generate your secret codes. Run
google-authenticatorand answeryto all the questions. CRITICAL: Scan the QR code with your phone's authenticator app and SAVE THE EMERGENCY BACKUP CODES somewhere super safe.- The Debugging Quest: The error message tells you to check the logs with
sudo journalctl -xeu ssh.service. The logs reveal a cryptic message:AuthenticationMethods cannot be satisfied. Translation: We told SSH to use a secret handshake, but we forgot to actually enable the system that handles secret handshakes.
- The Debugging Quest: The error message tells you to check the logs with
Restart and Test.
sudo systemctl restart ssh
This time, it works! When you log in from a new terminal, it will ask for your "Verification code." Type in the 6-digit number from your app, and you're in. High five!
The Fix: We need to make sure all the settings are perfectly aligned in /etc/ssh/sshd_config.
sudo nano /etc/ssh/sshd_config
Ensure these four lines are present and correct:
UsePAM yes
KbdInteractiveAuthentication yes
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
The key was KbdInteractiveAuthentication yes. That's the master switch we forgot to flip.
Configure SSH to require the handshake. This is a true rite of passage, and where things get dicey.
A Wild Error Appears!
You configure everything, restart SSH, and...Job for ssh.service failed. The service won't even start! You've broken the front door!
Install the gear:
sudo apt install libpam-google-authenticator -y
✅ Congrats, you’re now a Server Hero!
Your VPS is armed with:
- A safer user setup
- SSH keys
- A firewall
- Fail2ban
- Two-factor authentication
- Automatic security updates
Hackers will have to find someone else to bother.
Conclusion: You Are the Server Hero!
You did it. Your server is no longer a sitting duck. It's a fortress. You have a trusty sidekick, a magic lock, a bouncer, a robot blocker, and a secret handshake. The internet gremlins will have to find an easier target.
Now go forth and build something amazing!
Member discussion: