Set up a firewall on your Linux VPS with UFW

UFW on your Debian or Ubuntu VPS: SSH first, then your ports, a port for one IP address, ufw limit, deleting rules, IPv6, logging and Docker.

EliteStarHosting Team8 November 2026Updated 26 November 20264 min read
Ready to start? View the VPS Hosting plans

With UFW (Uncomplicated Firewall) you decide which ports of your Debian or Ubuntu VPS can be reached from outside. In Secure your new Linux VPS you turn UFW on with four commands.

A firewall matters as much on a cheap VPS as on an expensive server: bots scan every IP address.

Don't lock yourself out. Keep your current SSH window open and test every change in a second window.

The default rules

After installation, UFW is off, with these defaults: block incoming traffic, allow outgoing traffic. Set that yourself to be sure:

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing

SSH first, then turn it on

sudo ufw limit 22/tcp
sudo ufw enable

Confirm with y. With limit instead of allow, UFW blocks an IP address that tries to connect 6 or more times within 30 seconds. That slows down password-guessing bots. An existing allow 22/tcp rule becomes a limit rule.

SSH on another port

If SSH runs on another port, such as 2222, use that number. Moving SSH now? Keep to this order:

  1. Open the new port: sudo ufw limit 2222/tcp.
  2. Put Port 2222 in /etc/ssh/sshd_config.d/00-hardening.conf and check it with sudo sshd -t.
  3. Restart SSH. On Debian and Ubuntu 22.04: sudo systemctl restart ssh. On Ubuntu 24.04 and newer: sudo systemctl daemon-reload and then sudo systemctl restart ssh.socket.
  4. Test in a new window: ssh -p 2222 [email protected].
  5. If that works, remove the old rule with sudo ufw delete limit 22/tcp.

Ports for your services

Only open what you use, for example:

sudo ufw allow 80,443/tcp comment 'website'
sudo ufw allow 25565/tcp comment 'Minecraft'
sudo ufw allow 30120 comment 'FiveM'
sudo ufw allow 7777:7780/udp

Without /tcp or /udp, UFW opens both, as FiveM needs. You write a range of ports with a colon, and then tcp or udp is required. With comment your list stays readable. Setting up a game server yourself is covered in A game server on your VPS.

A port only for your IP address

With a fixed IP address at home, for example 198.51.100.25, you can open SSH for that address only:

sudo ufw allow proto tcp from 198.51.100.25 to any port 22
sudo ufw delete limit 22/tcp

The second command is needed, because the first rule that matches counts: as long as the general rule exists, anyone can get in. If your IP address changes, you can no longer get in yourself.

Viewing and deleting rules

sudo ufw status numbered
sudo ufw delete 3

After each delete the numbers shift, so check the list again before deleting another. Or repeat the rule itself with delete in front, such as sudo ufw delete allow 25565/tcp.

IPv6

UFW filters IPv6 by default (IPV6=yes in /etc/default/ufw). A rule without an IP address applies to both and appears twice in the list, the second time with (v6). If you delete by number, only that one disappears. With delete allow ... both do.

Logging

By default UFW logs at level low: blocked traffic, with a cap so your log doesn't fill up. Read it like this:

sudo journalctl -k | grep "UFW BLOCK"

If your VPS uses rsyslog, it is also in /var/log/ufw.log. sudo ufw logging medium gives more detail, but higher levels can fill your disk quickly.

Watch out with Docker

Ports you publish in Docker with -p bypass UFW. If only a reverse proxy on your VPS needs the port, publish it on 127.0.0.1, such as -p 127.0.0.1:8080:80. More in Install Docker on your VPS.

A Windows VPS?

There you use Windows Firewall, which is on by default on Windows Server 2022. Opening a port is in Connect with Remote Desktop, shielding Remote Desktop in Windows VPS: software and updates.

Locked out anyway? Contact support and include the IP address of your VPS. All options are in the ufw manual.