A fresh VPS is reachable from the whole internet the moment it boots. Within minutes, automated scanners will find its IP address and start trying common usernames and passwords over SSH. None of that is a reason to panic, but it is a reason to spend the first 15 minutes locking the server down before you install anything else. This guide walks through the essentials on Ubuntu and Debian, in the order you should do them.
Every command below assumes you are logged in as root over SSH, which is how most new servers start. Where a step could lock you out, there is a note on how to avoid it.
1. Update every package
The image your VPS was built from may be weeks or months old. Start by pulling in all security fixes released since then:
apt update
apt full-upgrade -y
apt autoremove -yIf a new kernel was installed, reboot so it takes effect:
[ -f /var/run/reboot-required ] && rebootWhile you are here, set the hostname and timezone so logs make sense later:
hostnamectl set-hostname web1
timedatectl set-timezone Europe/Berlin2. Create a normal user with sudo
Working as root all the time means a single typo or a compromised session has full control of the machine. Create a regular user and give it sudo rights instead:
adduser alice
usermod -aG sudo aliceadduser asks for a password. Pick a strong one: you will still need it for sudo, even after password logins over SSH are switched off. On a minimal Debian install, run apt install sudo first if the command is missing.
3. Set up SSH key login
SSH keys are far stronger than any password a human can remember, and they make brute-force attacks pointless. If you don't have a key yet, create one on your own computer, not on the server:
ssh-keygen -t ed25519 -C "alice@laptop"Then copy the public key to the new user on the server:
ssh-copy-id [email protected]On Windows without ssh-copy-id, paste the contents of ~/.ssh/id_ed25519.pub into the server manually, still as root:
mkdir -p /home/alice/.ssh
nano /home/alice/.ssh/authorized_keys
chown -R alice:alice /home/alice/.ssh
chmod 700 /home/alice/.ssh
chmod 600 /home/alice/.ssh/authorized_keysNow open a new terminal and check that you can log in as the new user and use sudo:
ssh [email protected]
sudo whoami # should print: rootDo not continue until this works. The next step turns off the way you are currently logged in.
4. Disable root login and password authentication
Rather than editing the main /etc/ssh/sshd_config, add a drop-in file. It survives package upgrades and keeps your changes in one place:
sudo nano /etc/ssh/sshd_config.d/00-hardening.confPermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers aliceThe 00- prefix matters. For most settings, OpenSSH uses the first value it reads, and files in sshd_config.d are read in alphabetical order. Many cloud images ship a 50-cloud-init.conf that sets PasswordAuthentication yes, which would otherwise win. Check for it with ls /etc/ssh/sshd_config.d/.
Test the configuration before applying it, then reload:
sudo sshd -t && sudo systemctl reload sshKeep your current session open and try a fresh login in another window. Confirm that ssh [email protected] is now refused. If something went wrong, you can still fix it from the session you kept open.
5. Turn on a firewall with ufw
A firewall makes sure only the services you chose are reachable, even if you later install something that listens on all interfaces by mistake. ufw is the simplest option on Ubuntu and Debian:
sudo apt install ufw -y
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw enableAllow SSH before you enable the firewall, or you will cut off your own connection. If you run a website, open the web ports too:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status verboseufw applies the same rules to IPv4 and IPv6 by default, which matters when your VPS has both a public IPv4 address and an IPv6 subnet. To limit SSH to your own address, use sudo ufw allow from 198.51.100.7 to any port 22 proto tcp and remove the general rule with sudo ufw delete allow OpenSSH. Only do this if your home or office IP is static.
One caveat: Docker writes its own iptables rules, and ports published with -p bypass ufw. If you use Docker, bind containers to 127.0.0.1 and put a reverse proxy in front, or read up on the DOCKER-USER chain.
6. Install fail2ban
With password login off, brute-force attempts can't succeed, but they still fill your logs. fail2ban watches for repeated failures and bans the source IP for a while:
sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
[sshd]
enabled = trueThe backend = systemd line is needed on Debian 12 and later, which no longer write /var/log/auth.log by default. It works on current Ubuntu too. Start it and check the jail:
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshdIf you ever ban yourself, unban with sudo fail2ban-client set sshd unbanip 198.51.100.7 from another connection.
7. Enable automatic security updates
Most compromises use vulnerabilities that already have a fix. unattended-upgrades installs security updates every day without you having to remember:
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgradesAnswer Yes. This creates /etc/apt/apt.conf.d/20auto-upgrades. By default only security updates are installed, which is the safe choice. Kernel updates still need a reboot. If you are happy for the server to restart itself at a quiet hour, edit /etc/apt/apt.conf.d/50unattended-upgrades:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";Check that it works with a dry run:
sudo unattended-upgrade --dry-run --debug8. Should you change the SSH port?
Moving SSH from port 22 to something like 2222 is popular advice. It is worth understanding what it does and doesn't do.
| Pros | Cons |
|---|---|
| Far fewer automated login attempts, so cleaner logs | Not real security: a port scan finds it in seconds |
| Less noise for fail2ban to process | You must pass -p or set the port in ~/.ssh/config everywhere |
| Hides you from the laziest bots | Some networks block unusual outbound ports |
| Easy to lock yourself out if the firewall isn't updated first |
With key-only login and fail2ban in place, port 22 is already safe. Changing the port is optional comfort, not protection. If you do it, open the new port in ufw first:
sudo ufw allow 2222/tcpOn Debian and older Ubuntu releases, add Port 2222 to your drop-in file and reload. On Ubuntu 22.10 and later, SSH is started by ssh.socket, which ignores the Port line. There, run sudo systemctl edit ssh.socket and add:
[Socket]
ListenStream=
ListenStream=2222Then run sudo systemctl daemon-reload && sudo systemctl restart ssh.socket. Test a login on the new port before you remove the old rule with sudo ufw delete allow OpenSSH. Also set port = 2222 under [sshd] in jail.local, so fail2ban bans on the right port.
9. Check what is listening
Finally, see which services accept network connections. Anything you don't recognise deserves a closer look:
sudo ss -tulpnRead the Local Address column. 127.0.0.1 or [::1] means the service is only reachable from the server itself, which is what you want for databases such as MySQL, PostgreSQL and Redis. 0.0.0.0 or [::] means it listens on every interface. Your firewall still blocks those ports unless you opened them, but it is better to bind internal services to localhost in their own configuration as well, so a firewall mistake doesn't expose them.
Remove what you don't need:
sudo systemctl disable --now SERVICE_NAMEYour 15-minute checklist
- Packages updated and rebooted into the latest kernel.
- A sudo user created and tested.
- SSH key login working for that user.
- Root login and password authentication disabled.
- ufw enabled with only the ports you need.
- fail2ban running with the
sshdjail. - unattended-upgrades installing security fixes daily.
- Listening ports reviewed with
ss -tulpn.
What comes next
This baseline stops the vast majority of automated attacks, but security is ongoing. Keep the software you install up to date, use HTTPS for anything with a login, and keep backups that you have actually tested restoring. Zettako's VPS hosting includes daily backups and network-level DDoS protection, which cover two things a firewall on the server can't. Everything inside the server is yours to control, which is exactly why these first 15 minutes matter.