Secure a new Linux VPS
Updated 27 Nov 2026 ubuntu security firewall
The first steps to take on a new Ubuntu or Debian server.
These commands are for Ubuntu and Debian. Run them after your first login.
1. Install updates
sudo apt update && sudo apt upgrade -y
Reboot if the kernel was updated:
sudo reboot
2. Enable automatic security updates
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
3. Require SSH keys
First connect with an SSH key and check that it works: this turns password sign-in off. Keep your current session open. Create /etc/ssh/sshd_config.d/00-hardening.conf:
PasswordAuthentication no
PermitRootLogin prohibit-password
Check the configuration, then reload:
sudo sshd -t && sudo systemctl reload ssh
Open a second terminal and confirm you can still sign in before closing the first.
4. Turn on a firewall
Allow SSH first so you do not lock yourself out:
sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
Open only the ports your applications need, for example sudo ufw allow 20002/tcp.
5. Block repeated failed logins
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
6. Keep private services private
Check what is listening:
sudo ss -tulpn
Databases and caches should listen on 127.0.0.1 unless you have a specific reason.
If you lock yourself out
Use the browser console from your server page. Console login needs a password, which you can set when deploying or reinstalling.