How to Open a Specific Port on Ubuntu with UFW
-
Jason Yang - 03 Dec, 2025
- Updated 04 Jan, 2026
- Views —
To open a specific port (in this case, 9040) on Ubuntu, we can use UFW (Uncomplicated Firewall). It is a user-friendly command-line interface for managing firewall rules on Linux.
Here is a step-by-step guide to opening port 9040 (TCP).
1. Check UFW Status & Safety First
Before enabling the firewall, ensure that SSH is allowed to prevent locking yourself out of the server.
Check the current status:
sudo ufw status
If the status is inactive, add the SSH rule first, then enable it:
# 1. Allow SSH connection to prevent lockout
sudo ufw allow ssh
# 2. Enable UFW
sudo ufw enable
(Press y if it warns about disrupting existing SSH connections.)
2. Open Port 9040
Now that the firewall is active, use the following command to open the specific port:
sudo ufw allow 9040/tcp
Output:
Rule added
Rule added (v6)
This command allows inbound connections on port 9040 using the TCP protocol. If you need UDP as well, you can run sudo ufw allow 9040/udp.
Don’t open it to the whole internet if you can avoid it
sudo ufw allow 9040/tcp opens the port to Anywhere — every host on the internet can now probe it. If only one machine or network needs in, use a source-scoped rule so nobody else can even see the port:
sudo ufw allow from 203.0.113.10 to any port 9040 proto tcp
This is an alternative to the broad rule, not an addition. ufw allow only ever adds rules, so if you already ran sudo ufw allow 9040/tcp above, that Anywhere rule is still live and the scoped one changes nothing. Delete the broad rule first (it created both a v4 and a v6 entry):
sudo ufw delete allow 9040/tcp
sudo ufw allow from 203.0.113.10 to any port 9040 proto tcp
sudo ufw status numbered
That’s the difference between a service the whole internet can scan and one only your office IP or WireGuard VPN can reach.
3. Verify the Rules
To confirm the changes, check the status again. You should see port 9040 listed in the output.
sudo ufw status
Example Output:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
9040/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
9040/tcp (v6) ALLOW Anywhere (v6)
How to Close the Port
If you no longer need this port open, you can remove the rule with:
sudo ufw delete allow 9040/tcp
The delete allow form has to match the original rule exactly, which is fiddly once you’ve added scoped rules. It’s usually easier to list rules with an index and delete by number:
sudo ufw status numbered
sudo ufw delete 3
Important Notes regarding Cloud Services
If you are using cloud providers like AWS (Security Groups), Azure, or Google Cloud (VPC Firewall), opening the port in Ubuntu is not enough. You must also open the corresponding port in the cloud provider’s network security settings.