Setting up a new VPS usually starts with the essentials: updates, a sudo user, SSH keys, a firewall and Fail2ban. If you haven't done those yet, start with our guide to the first things to do on a new Linux VPS and come back.
This checklist picks up where that leaves off. It's for servers running real websites or applications, where you want to reduce the attack surface, catch problems early and recover quickly if something goes wrong. Commands use Ubuntu and Debian conventions, so adjust package names for other distributions.
Before you start
- Take a snapshot or backup of the server first. Some steps, especially SSH and firewall changes, can lock you out if done wrong.
- Keep a second SSH session open while changing SSH or firewall settings, so you can revert if the new config fails.
- Know how to reach your provider's console (often a web-based VNC or serial console) in case you lose SSH access.
Access control
1. Tighten SSH beyond keys
You've already set up key-based login and disabled password authentication, as covered in our SSH keys guide. Go a step further in /etc/ssh/sshd_config (or a file in /etc/ssh/sshd_config.d/):
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3
LoginGraceTime 30
AllowUsers deploy
X11Forwarding no
AllowUsers limits SSH to named accounts. Test with sudo sshd -t before restarting the service, and keep your existing session open until you've confirmed a new login works.
Optionally, restrict SSH to known IP addresses at the firewall level if you connect from fixed locations, or put SSH behind a VPN.
2. Review user accounts and sudo access
getent passwd | awk -F: '$3 >= 1000 {print $1}'
getent group sudo
Remove accounts that are no longer needed and take sudo away from anyone who doesn't need it. Each person should have their own account, never a shared login.
3. Protect your SSH keys
Use passphrases on private keys, remove old keys from ~/.ssh/authorized_keys, and consider hardware security keys for critical servers. Rotate keys when someone leaves.
Reduce the attack surface
4. Audit listening ports
See exactly what's listening for connections:
sudo ss -tulpn
Every line is a potential entry point. For a typical web server you'd expect SSH, HTTP (80) and HTTPS (443). Databases like MySQL or PostgreSQL and caches like Redis usually should listen only on 127.0.0.1 (localhost), not on all interfaces.
5. Review your firewall rules
You turned on a firewall during setup. Now check the rules match what ss shows:
sudo ufw status verbose
Use a default deny policy for incoming traffic and allow only what's needed. Remove temporary rules you added for testing. If you use Docker, be aware that published container ports can bypass UFW rules, as explained in our Docker on a VPS guide.
6. Remove unneeded packages and services
systemctl list-units --type=service --state=running
Disable and remove anything you don't use, such as old mail servers, FTP daemons or sample apps. Less software means fewer vulnerabilities to patch. If you need file transfer, prefer SFTP over FTP. See FTP vs SFTP vs SSH.
7. Keep a minimal web server footprint
- Hide version numbers (for Nginx,
server_tokens off;) - Disable directory listing
- Remove default pages and unused virtual hosts
- Redirect HTTP to HTTPS with a valid certificate, as shown in our Certbot guide
- Consider a web application firewall such as ModSecurity
Patch management
8. Confirm automatic security updates work
Setting up unattended-upgrades is common, but check that it actually runs:
sudo unattended-upgrade --dry-run --debug
cat /var/log/unattended-upgrades/unattended-upgrades.log
Also check whether a reboot is pending (/var/run/reboot-required on Ubuntu). Kernel updates often need a reboot to take effect, so schedule regular maintenance windows.
9. Update application dependencies too
System packages aren't everything. Keep PHP, Node.js, Python packages, container images and your CMS updated. Subscribe to security announcements for the software you rely on.
Kernel and system settings
10. Apply sensible sysctl network settings
Create a file like /etc/sysctl.d/99-hardening.conf with commonly recommended settings, for example:
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
Apply with sudo sysctl --system. Only change ip_forward and similar settings if you understand the impact, since Docker, VPNs and routing setups depend on them.
11. Use mandatory access control
Ubuntu ships with AppArmor and RHEL-based systems with SELinux. Leave them enabled rather than disabling them to fix an error. Check status with sudo aa-status or getenforce. When something is blocked, adjust the profile or policy instead of switching protection off.
12. Secure shared memory and temporary directories
Some administrators mount /tmp and /dev/shm with noexec,nosuid,nodev options to make it harder to run dropped malware from those locations. Test carefully, since some installers and applications expect to execute files from /tmp.
Visibility and detection
13. Centralize and review logs
Know where your logs live: journalctl, /var/log/auth.log, web server access and error logs. Set up log rotation so disks don't fill up, and consider shipping logs to another system so an attacker can't simply erase them. Fail2ban, covered in our Fail2ban setup guide, can act on what it sees in these logs.
Pair logs with resource monitoring, as described in how to monitor server resources on a Linux VPS. An unexplained CPU spike or outbound traffic surge is often the first sign of a compromise.
14. Run an audit and integrity checks
- Lynis is a free, open-source auditing tool that scans your system and suggests hardening improvements:
sudo apt install lynis && sudo lynis audit system. Work through its suggestions selectively. - AIDE builds a database of file checksums and reports changes to important system files.
- auditd can record security-relevant events, such as changes to
/etc/passwdor sudo use. - rkhunter or chkrootkit check for known rootkit signs. Expect some false positives.
Also review scheduled tasks with crontab -l for each user and /etc/cron.*, since attackers often add cron jobs for persistence. Our cron jobs guide explains how they work.
Recovery
15. Back up, off-server, and test restores
Hardening reduces risk but doesn't eliminate it. Keep automated backups of application files, databases and configuration in a separate location, following the 3-2-1 backup rule. Provider snapshots are convenient but often live with the same provider, so add an independent copy.
Most importantly, test restoring a backup to a fresh server. A backup you've never restored is a hope, not a plan.
Hardening checklist summary
- ☐ Snapshot taken and console access confirmed
- ☐ SSH: no root, no passwords, AllowUsers, low MaxAuthTries
- ☐ Unused accounts and sudo rights removed
- ☐ Listening ports audited, databases on localhost
- ☐ Firewall default deny, rules match services
- ☐ Unused services and packages removed
- ☐ Web server versions hidden, HTTPS enforced
- ☐ Automatic updates verified, reboots scheduled
- ☐ Application dependencies updated
- ☐ sysctl network hardening applied
- ☐ AppArmor or SELinux enabled
- ☐ Logs rotated and reviewed, monitoring in place
- ☐ Lynis audit run, integrity checks configured
- ☐ Cron jobs reviewed
- ☐ Off-server backups tested
Wrapping up
Server hardening isn't a one-time task. It's a set of habits: minimize what's exposed, limit who can get in, keep everything patched, watch for unusual activity and be ready to recover. Work through this list once, then revisit it every few months or after major changes. And if managing all of this yourself sounds like more than you want to take on, a managed server may be the better fit.
Related reading: ModSecurity Explained: How This Web Application Firewall Protects Your Site.