info@soykanbilisim.com.tr +90 535 011 07 27 Pzt–Cum 09:00–18:00 · Cmt 09:00–13:00

Setting Up a Firewall with UFW on a Linux Server: Step by Step

The right order for setting up UFW on your Ubuntu / Debian server without locking yourself out: default rules, SSH rate limiting, IP restrictions, IPv6 and logging.

Setting Up a Firewall with UFW on a Linux Server: Step by Step

One of the first things to do on a new VPS is to turn on the firewall. On Ubuntu and Debian the easiest way is UFW (Uncomplicated Firewall). UFW lets you manage the iptables/nftables rules in the Linux kernel with simple commands.

In this guide we go step by step in the right order, avoiding the most common mistake: enabling the firewall before allowing SSH and locking yourself out of the server.

Before you start

  • Make sure you have VNC / console access in your provider's panel. If something goes wrong, you can recover from there.
  • Know which port SSH runs on (22 by default).
  • If you use a panel such as aaPanel, cPanel or Plesk, use the panel's own firewall screen. The panel manages UFW itself, and rules added from the command line may conflict with it.

1. Install and set the default rules

sudo apt update && sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing

These two rules mean "deny everything coming in, allow connections going out from the server".

2. Allow SSH BEFORE enabling the firewall

sudo ufw limit 22/tcp

Using limit instead of allow temporarily blocks an IP address that makes many connection attempts in a short time (in UFW the threshold is around 6 within 30 seconds). It slows down password-guessing attacks considerably. If SSH runs on a different port, use that port instead of 22.

3. Website ports

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

4. Enable the firewall and check

sudo ufw enable
sudo ufw status verbose

Without closing your current SSH session, test that you can connect again from a new terminal.

5. Open admin ports only to your own IP

Instead of opening ports for panels, databases or monitoring tools to the whole internet, open them only to your own static IP address:

sudo ufw allow from 203.0.113.10 to any port 8443 proto tcp

If you don't have a static IP, using a VPN (WireGuard, Tailscale) for admin access is the safest option.

Never open database ports (MySQL 3306, PostgreSQL 5432, Redis 6379, MongoDB 27017) to the whole internet. If the application runs on the same server, these ports don't need to be reachable from outside at all.

6. Deleting and editing rules

sudo ufw status numbered
sudo ufw delete 3

Rules are evaluated from top to bottom; the first matching rule wins.

7. Don't forget IPv6

If your server has an IPv6 address, attackers can come from there too. Make sure this line is in /etc/default/ufw:

IPV6=yes

With this setting on, UFW automatically applies your rules to IPv6 as well.

8. Turn on logging

sudo ufw logging low

Blocked connections are written to /var/log/ufw.log. You can see who is trying which port and how often.

Is UFW enough on its own?

UFW's rate limiting is basic. On production servers we recommend using fail2ban alongside it: fail2ban reads the logs and blocks addresses that repeatedly fail to log in for longer periods. On top of that, disabling password login for SSH and allowing only SSH keys makes brute-force attacks completely ineffective.

And remember: if you use Docker on the server, the ports you publish can bypass UFW rules. We cover this important topic in a separate article.

Summary checklist

  • Default: incoming traffic denied, outgoing traffic allowed
  • SSH allowed and protected with limit
  • Only the ports you really need are open
  • Admin and database ports are closed to the internet
  • IPv6 rules enabled
  • Logs are kept and checked regularly

If you'd like us to set up and harden your server, take a look at our server management service or write to us.

Back to all posts Share on WhatsApp

Let's plan your project together

Tell us what you need; we'll send you the right solution and a clear quote the same day.

Chat with us