Skip to content
Log In Sign Up info@muvcloud.com

Before You Enable ufw

A firewall that blocks SSH locks you out of your own server. Follow this order:

  • Allow SSH first: add the SSH rule before you set a default-deny policy or enable ufw, on the port SSH really listens on. sudo sshd -T | grep -i '^port' shows it.
  • Keep your session open: test a new login from a second terminal before you close the current one.
  • Know the way back: find the server console in your provider's control panel; it works without the network.

ufw is installed on Ubuntu but inactive. On Debian, install it with sudo apt install ufw. On AlmaLinux and Rocky Linux, use firewalld instead; never run two firewall tools on one server.

Set Up ufw

sudo ufw allow OpenSSH
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status verbose

OpenSSH is an application profile for port 22/tcp. If SSH uses another port, replace the first line with that port, for example sudo ufw allow 2222/tcp. The two default policies are already ufw's defaults; setting them makes the result certain. When you run it over SSH, ufw enable warns that the command may disrupt existing SSH connections and asks for confirmation. With the SSH rule in place, answer y. The firewall is active now and at every boot.

What the Defaults Mean

  • Deny incoming: new connections from outside are dropped unless a rule allows them. Dropped means no answer at all, so a client sees a timeout, not "connection refused".
  • Allow outgoing: the server can still reach update mirrors, DNS servers, and APIs.
  • Replies pass: answers to connections the server opened itself are allowed automatically.

Allow Your Services

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp

The last rule opens the database port to one address only. Ranges, rate limits, and deny rules are in how to open a port with UFW.

Application Profiles

Packages can place profiles in /etc/ufw/applications.d/ that name their ports:

sudo ufw app list
sudo ufw app info OpenSSH
sudo ufw allow "Nginx Full"

On Ubuntu and Debian, the OpenSSH server package adds the OpenSSH profile and the Nginx package adds profiles such as Nginx HTTP, Nginx HTTPS, and Nginx Full, which covers both ports. Profile names with spaces need quotes. If a profile is not listed, allow the port by number.

List and Delete Rules

sudo ufw status numbered
sudo ufw delete 3

ufw asks for confirmation and deletes rule 3. The numbers shift after every deletion, so list again before you delete the next one. You can also delete a rule by repeating it: sudo ufw delete allow 80/tcp. Before you delete an SSH rule, confirm that another rule allows the port SSH uses.

IPv6

ufw filters IPv6 too when IPV6=yes is set in /etc/default/ufw, the default. A rule without an address then appears twice in ufw status, once marked (v6); a rule for one IPv4 address covers IPv4 only, so add a second rule for an IPv6 address. Keep the setting on whenever the server has an IPv6 address. If you change it, run sudo ufw reload to apply it.

Logging, Reset, and Docker

  • Logging: sudo ufw logging on records blocked packets in the kernel log as lines with UFW BLOCK; read them with sudo journalctl -k | grep UFW.
  • Reset: sudo ufw reset disables ufw and returns it to its installation defaults, with no rules. Add the SSH rule again before you enable it.
  • One tool: while ufw is active it owns the rule set; do not add rules with iptables or nft by hand.
  • Docker: ports published with docker run -p are opened by Docker's own firewall rules and bypass ufw. Bind such ports to 127.0.0.1 when only the server itself should reach them; see Docker port mapping.
  • Provider firewall: if your provider runs a firewall in front of the server, a port must be open there as well.

To see what is actually listening before you open anything, use how to check open ports in Linux. This firewall is step 5 of the initial server setup checklist.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations