Open ports with UFW: single ports, ranges and trusted IPs
Open TCP and UDP ports in UFW, allow port ranges, limit access to one IP or subnet, label rules with comments and confirm everything with ufw status numbered.
- Beginner
- Linux VPS
- 5 min read
UFW (Uncomplicated Firewall) is the standard firewall front end on Ubuntu and Debian. This guide shows the exact ufw allow syntax for single ports, port ranges and trusted IP addresses, so you open only what your server needs and nothing more. Each rule you add is one more door into the server, so keep the list short and labelled.
Before you start
- A RunRacks Linux VPS running Ubuntu 22.04, Ubuntu 24.04 or Debian 12, with UFW installed. If it is not set up yet, start with Set up the UFW firewall on Ubuntu and Debian.
- A user with
sudorights, connected over SSH. - Your SSH port. It is 22 unless you changed it.
Always allow SSH before you run sudo ufw enable. UFW denies all incoming traffic by default, so enabling it without an SSH rule closes your session and locks you out. If that happens, log in through the browser console in your Dashboard control panel and run sudo ufw allow 22/tcp.
Allow SSH first, then check the current state:
sudo ufw allow 22/tcp
sudo ufw status verboseIf you moved SSH to another port, use that number instead of 22.
Open a single port
The basic form is ufw allow <port>/<protocol>:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 51820/udpAlways name the protocol. If you write sudo ufw allow 80 with no protocol, UFW opens the port for both TCP and UDP. That works, but it opens more than a web server needs.
UFW also knows some services by name from /etc/services, so sudo ufw allow https is the same as allowing 443. Packages can ship application profiles too. List them with:
sudo ufw app listOn a server with Nginx installed you will usually see Nginx Full, Nginx HTTP and Nginx HTTPS. Allow one with quotes because the name has a space:
sudo ufw allow 'Nginx Full'Open a port range
Ranges use a colon between the first and last port, and a protocol is required:
sudo ufw allow 6000:6010/tcp
sudo ufw allow 60000:61000/udpThe second line is the range that mosh uses, for example. Without /tcp or /udp, UFW rejects a range with an error.
To open several separate ports in one rule, list them with commas. This also needs a protocol:
sudo ufw allow proto tcp from any to any port 80,443,8080Allow a port only from a trusted IP or subnet
This is the most useful rule for admin ports such as SSH, database ports or a control panel. Only the address you name can connect.
Allow everything from one IP:
sudo ufw allow from 203.0.113.10Allow one port from one IP:
sudo ufw allow from 203.0.113.10 to any port 22 proto tcpAllow a database port from a whole office subnet:
sudo ufw allow from 198.51.100.0/24 to any port 3306 proto tcpThe /24 is CIDR notation for 256 addresses. If that is new to you, read CIDR notation and subnets explained.
To replace a public SSH rule with a trusted-IP rule, add the new rule first, test a fresh SSH login from that IP, then delete the old public rule. Never remove the public rule first.
Home internet connections often change IP address. Before you limit SSH to your home IP, make sure you can still reach the server through the Dashboard browser console if the address changes.
Label rules with comments
Add comment to the end of any rule so you remember later why it exists:
sudo ufw allow 443/tcp comment 'HTTPS for Nginx'
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp comment 'SSH from office'Comments show up in ufw status output. They cost nothing and make clean-ups much safer.
Check it works
List the rules with their numbers:
sudo ufw status numberedExample output:
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 443/tcp ALLOW IN Anywhere # HTTPS for Nginx
[ 3] 3306/tcp ALLOW IN 198.51.100.0/24
[ 4] 22/tcp (v6) ALLOW IN Anywhere (v6)
[ 5] 443/tcp (v6) ALLOW IN Anywhere (v6) # HTTPS for NginxRules marked (v6) are the IPv6 copies that UFW creates for you. Then test from another computer, not from the server itself:
nc -vz 203.0.113.10 443A succeeded or open result means the port is reachable. A firewall rule only lets traffic through. A service still has to be listening on that port, which you can check with sudo ss -tulpn.
Troubleshooting
- "ERROR: Must specify 'tcp' or 'udp' with multiple ports": ranges and comma lists need a protocol. Add
/tcp,/udporproto tcp. - Rule added but the port is still closed: the service may only listen on
127.0.0.1, or UFW may be inactive. Runsudo ufw statusandsudo ss -tulpn. See Port open in the firewall but still unreachable. - "Skipping adding existing rule": the rule is already there. Nothing needs to change.
- A trusted-IP rule does not match: you may be connecting over IPv6, or through a VPN with a different exit IP. Check your current address and add the right one.
- Locked out after enabling UFW: use the browser console in the Dashboard, run
sudo ufw allow 22/tcp, then reconnect.
Next steps
- Clean up rules you no longer need: Delete, insert and reorder UFW rules safely.
- Slow down password guessing on SSH: Rate-limit SSH and other ports with ufw limit.
- Block abusive addresses: Block an IP address or subnet with UFW.
- Need a server to practise on? See Linux VPS plans.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Security
Check the Windows Firewall rule for RDP port 12230
Find and verify the inbound TCP 12230 rule that lets Remote Desktop reach your RunRacks server, recreate it if it was deleted and scope it without locking yourself out.
- Intermediate
- Windows VPS · Admin RDP
- 5 min read
Security
Linux Security: Using aide for File Integrity Monitoring on VPS
AIDE builds a checksum database of your system files and reports anything that changed. This guide installs it, creates a baseline, tunes the rules and schedules daily checks on a Linux VPS.
- Advanced
- Linux VPS
- 6 min read
Security
iptables basics: list, allow, drop and save rules
Learn how iptables chains and rule order work, then build a small ruleset that allows SSH and web traffic, drops everything else and can be edited by line number.
- Intermediate
- Linux VPS
- 6 min read