You spin up a fresh VPS, log in as root, and the server is yours. But it's also wide open to anyone scanning the internet for weak targets. Server hardening means closing those gaps before an attacker finds them.
This checklist walks through ten concrete steps to lock down a Linux server. You don't need prior security experience—just terminal access and fifteen minutes.
What hardening actually means
Hardening is reducing your attack surface. Every open port, default password, or unused service is a potential way in. The goal is simple: disable what you don't need, secure what you do, and make brute-force attacks expensive.
Think of it like locking your house. You wouldn't leave every window open and the front door unlocked. Same principle here.
Step 1: Update the system packages
Start here every time. Outdated packages carry known vulnerabilities that automated scripts exploit within hours of a CVE announcement.
On Debian or Ubuntu:
sudo apt update && sudo apt upgrade -y
On CentOS, Rocky, or AlmaLinux:
sudo yum update -y
This pulls the latest security patches from your distribution's repositories. Run it before you configure anything else.
Step 2: Create a non-root user with sudo access
Never run day-to-day tasks as root. A typo or compromised session can wipe your system. Create a separate user and grant it sudo privileges instead.
adduser deploy
usermod -aG sudo deploy
Replace deploy with whatever username you prefer. The second command adds the user to the sudo group, which allows it to run administrative commands with sudo.
Test the new account before you lock out root:
su - deploy
sudo whoami
If that prints root, the sudo configuration works. Log out and log back in as the new user for the remaining steps.
Step 3: Disable root login over SSH
Once you have a working sudo user, disable direct root login. Attackers always try root first because the username is guaranteed to exist.
Open the SSH daemon configuration:
sudo nano /etc/ssh/sshd_config
Find the line that says PermitRootLogin and change it to:
PermitRootLogin no
If the line is commented out with a #, remove the #. Save and restart SSH:
sudo systemctl restart sshd
Now root cannot log in remotely. If you need root access, log in as your sudo user and run sudo -i.
Step 4: Set up SSH key authentication
Passwords are guessable. SSH keys are not. A key pair uses public-key cryptography: the private key stays on your machine, the public key goes on the server.
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 file location. Set a passphrase if you want an extra layer of protection.
Copy the public key to the server:
ssh-copy-id deploy@your_server_ip
That appends your public key to ~/.ssh/authorized_keys on the server. Test the login:
ssh deploy@your_server_ip
If you connect without typing a password, the key works.
Step 5: Disable password authentication
Once key authentication works, turn off password login entirely. This blocks brute-force attacks cold.
Edit /etc/ssh/sshd_config again:
sudo nano /etc/ssh/sshd_config
Set these three directives:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Restart SSH:
sudo systemctl restart sshd
Before you log out, open a second terminal and confirm you can still connect. If you lock yourself out, you'll need console access through your hosting provider's dashboard.
Step 6: Change the default SSH port
SSH listens on port 22 by default. Automated scanners hit that port constantly. Moving SSH to a non-standard port cuts down on noise and login attempts.
Pick a port above 1024 and below 65535 that isn't already in use—something like 2200 or 44122. Edit sshd_config:
sudo nano /etc/ssh/sshd_config
Find the Port line and change it:
Port 2200
Restart SSH:
sudo systemctl restart sshd
From now on, connect with:
ssh -p 2200 deploy@your_server_ip
This won't stop a determined attacker, but it removes your server from the path of mass scans.
Step 7: Configure a firewall with UFW
A firewall blocks traffic to ports you aren't using. UFW (Uncomplicated Firewall) is a frontend for iptables that makes this straightforward.
Install UFW if it's not already present:
sudo apt install ufw
Deny all incoming traffic by default, then allow only the ports you need. If you changed SSH to port 2200:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2200/tcp
If you're running a web server, allow HTTP and HTTPS:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Enable the firewall:
sudo ufw enable
Check the status:
sudo ufw status
You should see your allowed ports listed. If you accidentally lock yourself out, most hosting providers offer console access to disable UFW.
Step 8: Install and configure Fail2Ban
Fail2Ban monitors log files for repeated failed login attempts and bans the offending IP for a set period. It's automated rate limiting.
Install it:
sudo apt install fail2ban
Copy the default config to a local override:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Edit jail.local and ensure the SSH jail is enabled:
sudo nano /etc/fail2ban/jail.local
Look for the [sshd] section. Set these values:
[sshd]
enabled = true
port = 2200
maxretry = 3
bantime = 3600
This bans an IP for one hour after three failed attempts. Restart Fail2Ban:
sudo systemctl restart fail2ban
Check the status:
sudo fail2ban-client status sshd
You'll see currently banned IPs and the total ban count.
Step 9: Enable automatic security updates
Manual updates are easy to forget. Automatic updates keep critical patches applied without you lifting a finger.
On Ubuntu or Debian, install the unattended-upgrades package:
sudo apt install unattended-upgrades
Enable it:
sudo dpkg-reconfigure --priority=low unattended-upgrades
This configures the system to install security updates automatically. You can review the configuration in /etc/apt/apt.conf.d/50unattended-upgrades if you want to tweak what gets updated.
On CentOS or Rocky, use yum-cron:
sudo yum install yum-cron
sudo systemctl enable yum-cron
sudo systemctl start yum-cron
Edit /etc/yum/yum-cron.conf and set apply_updates = yes to automatically install updates.
So what if you need to roll back a bad update?
Automatic updates are low-risk for security patches, but kernel updates occasionally break custom configurations. If you're running production workloads, test updates in staging first or exclude kernel updates from the automatic process.
For most small VPS setups, the risk of an unpatched vulnerability is higher than the risk of a bad patch.
Step 10: Disable unused services and ports
Every running service is another thing to secure. If you're not using it, turn it off.
List active services:
sudo systemctl list-units --type=service --state=running
Look for anything you don't recognize or need. Common examples: Bluetooth daemons, printer services, or legacy mail agents.
Disable a service:
sudo systemctl disable --now service_name
Check listening ports:
sudo ss -tuln
If you see a port open that you didn't configure, track down which service owns it and either disable the service or block the port with UFW.
What to check first when something breaks
If you lock yourself out of SSH, use your hosting provider's web console to revert changes. Most offer VNC or serial console access.
If Fail2Ban bans your own IP, SSH in from another IP or use the console to run:
sudo fail2ban-client unban your_ip_address
If a service won't start after hardening, check the logs:
sudo journalctl -u service_name -n 50
The last fifty lines usually show the error. Permission issues and port conflicts are the usual culprits.
How often should I run updates?
Automatic security updates handle critical patches daily. Run a full manual upgrade weekly or monthly depending on how actively you manage the server.
Can I use these steps on a cPanel server?
Yes, but be careful with the firewall. cPanel uses CSF (ConfigServer Security & Firewall) by default, which conflicts with UFW. Stick with CSF if it's already installed. The SSH hardening steps work the same.
What if I need root login for a specific tool?
Re-enable it temporarily in sshd_config, run your task, then disable it again. Or configure the tool to use sudo instead—that's almost always the better option.
Does changing the SSH port actually help?
It reduces log noise and automated scans but won't stop a targeted attack. Think of it as a first filter, not a security control.
Should I use a different firewall instead of UFW?
UFW is fine for most use cases. If you need advanced filtering or NAT rules, use iptables or nftables directly. For cPanel, use CSF.
First step after reading this
Don't try to do all ten steps at once. Start with package updates, create a sudo user, and set up SSH keys. Test each change before moving to the next one.
Once SSH is locked down and the firewall is running, the rest of the checklist is maintenance. Schedule a monthly review to check for new services, review Fail2Ban logs, and confirm automatic updates are still running.
Hardening isn't a one-time task. It's a baseline you maintain.
