How can I open some ports on Ubuntu?
Categories:
How to Open Ports on Ubuntu: A Comprehensive Guide

Learn how to configure your Ubuntu firewall (UFW) to open specific ports, allowing network traffic for applications and services.
Opening ports on your Ubuntu system is a fundamental task for network administrators and developers. Whether you're hosting a web server, running a game server, or simply need an application to communicate over the network, correctly configuring your firewall is crucial. This guide will walk you through the process of opening ports using Ubuntu's Uncomplicated Firewall (UFW), ensuring your system remains secure while allowing necessary traffic.
Understanding UFW: Ubuntu's Uncomplicated Firewall
UFW is the default firewall configuration tool for Ubuntu. It provides a user-friendly way to manage iptables
rules, making it easier to control network access to your system. By default, UFW is disabled and configured to deny all incoming connections while allowing all outgoing connections. This secure-by-default approach means you must explicitly open any ports you want to allow incoming traffic on.
flowchart TD A[Incoming Connection] --> B{UFW Active?} B -->|No| C[Connection Allowed (No Firewall)] B -->|Yes| D{Rule for Port?} D -->|Allow| E[Connection Allowed] D -->|Deny (Default)| F[Connection Blocked] E --> G[Application/Service] F --> H[Connection Dropped]
UFW decision flow for incoming network connections
Checking UFW Status and Enabling It
Before you can open ports, you need to ensure UFW is active. If it's not, you'll need to enable it. Enabling UFW will immediately apply its default rules, which typically means denying all incoming connections. It's a good practice to allow SSH access before enabling UFW if you're connected remotely, to avoid locking yourself out.
sudo ufw status verbose
Check the current status of UFW
1. Allow SSH (if remote)
If you're accessing your Ubuntu machine remotely via SSH, allow port 22 (or your custom SSH port) before enabling UFW to prevent being locked out. You can specify the service name or the port number.
2. Enable UFW
Once SSH is allowed (if applicable), enable UFW. You will be prompted to confirm this action, as it may disrupt existing connections.
3. Verify UFW Status
After enabling, check the status again to confirm UFW is active and the rules are applied.
sudo ufw allow ssh
# OR
sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status verbose
Commands to allow SSH, enable UFW, and verify status
Opening Specific Ports
UFW allows you to open ports in several ways: by port number, by service name, or by specifying a range of ports. You can also specify the protocol (TCP or UDP) for more granular control. If no protocol is specified, UFW applies the rule to both TCP and UDP.
Open a Single Port (TCP)
sudo ufw allow 80/tcp
Open a Single Port (UDP)
sudo ufw allow 53/udp
Open a Port for Both TCP/UDP
sudo ufw allow 1234
Open by Service Name
sudo ufw allow 'Apache Full'
Or for specific services like HTTP/HTTPS:
sudo ufw allow http sudo ufw allow https
Open a Port Range
sudo ufw allow 6000:6007/tcp
Allow from Specific IP Address
sudo ufw allow from 192.168.1.100 to any port 22
After adding any rule, it's good practice to check the UFW status again to ensure your new rules are active.
sudo ufw status verbose
Verify UFW status after adding new rules
Deleting UFW Rules
If you need to close a port or remove a rule, you can do so by specifying the rule itself or by its number in the ufw status numbered
output. It's generally safer to delete by rule number to avoid accidentally deleting the wrong rule if you have similar rules.
1. List Rules with Numbers
First, list all current UFW rules with their corresponding numbers. This will help you identify the exact rule you want to delete.
2. Delete Rule by Number
Once you have the rule number, use the delete
command followed by the number. You will be prompted for confirmation.
3. Delete Rule by Content
Alternatively, you can delete a rule by specifying its exact content, for example, sudo ufw delete allow 80/tcp
.
sudo ufw status numbered
# Example: Delete rule number 3
sudo ufw delete 3
# Example: Delete rule by content
sudo ufw delete allow 80/tcp
Commands to list and delete UFW rules