Open a Port
Before you change rules, make sure SSH stays allowed on the port it uses, keep your current session open, and know where the server console is in your provider's control panel. Then open a port with its number and protocol:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw statusWithout /tcp or /udp, ufw opens the port for both protocols; name the protocol, since most services use TCP only. The rule applies at once and survives reboots. If ufw is not active yet, set it up first as described in how to set up a firewall with UFW: enabling it without an SSH rule locks you out.
Several Ports and Ranges
sudo ufw allow proto tcp from any to any port 80,443
sudo ufw allow 6000:6010/tcpA list or a range always needs the protocol. The colon form opens every port from 6000 to 6010.
Allow a Port from One Address
sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp
sudo ufw allow from 203.0.113.0/24 to any port 5432 proto tcpThis is the right way to open a database or an admin port: only the named address or network gets in, and everyone else is still dropped. Rules without an address cover IPv4 and IPv6; a rule with an IPv4 address covers IPv4 only, so add an IPv6 address as its own rule if one is needed. The database must also listen on a public address; see MySQL remote access. To limit a rule to one network interface, write it as sudo ufw allow in on eth1 to any port 3306 proto tcp.
Rate-Limit a Port
sudo ufw limit 22/tcpA limit rule allows the port but denies an address that opens six or more connections within 30 seconds. It suits SSH; use your SSH port number if you changed it, and check with sudo ufw status that the port now shows LIMIT. If an allow rule for the same port, such as OpenSSH, is listed above it, that rule matches first; delete the allow rule only after the limit rule is in place. Do not use it on web ports: browsers and proxies open many connections legitimately. For blocking based on failed logins, see how to install Fail2ban.
Deny or Reject a Port or an Address
sudo ufw deny 25/tcp
sudo ufw insert 1 deny from 203.0.113.10ufw checks rules from the top and stops at the first match. A deny rule for one address must come before the allow rules, which is why the second command uses insert 1. deny drops packets silently; reject sends an error back, so the client fails at once instead of waiting.
Check That the Port Is Reachable
On the server, confirm that a program listens on the port, and on which address:
sudo ss -tlnp | grep ':80 'From another machine you control, test the connection:
nc -zv your_server_ip 80- Succeeded: the port is open and a service answers.
- Connection refused: the packet arrived, but nothing listens on that port, or a reject rule answered.
- Timed out: a firewall dropped the packet: ufw, or a firewall at your provider.
Both tools are explained in the nc command and how to check open ports in Linux.
Close the Port Again
sudo ufw delete allow 80/tcp
sudo ufw status numbered
sudo ufw delete 4Delete a rule by repeating it after delete, or by its number. Numbers change after each deletion, so list again before the next one.
When the Result Is Not What You Expect
- ufw is inactive:
sudo ufw statussays so; rules are stored but not applied. - The service listens on 127.0.0.1: it accepts local connections only, whatever the firewall allows. Change its listen address.
- A provider firewall: open the port in your provider's control panel as well.
- Docker: a port published with
-pis opened by Docker's own rules and is reachable even when ufw denies it. Publish it on127.0.0.1if only the server should reach it; see Docker port mapping.
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.
More Guides
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.

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