Linux Open Port: Managing Firewall Ports Step-by-Step Guide
Managing open ports is one of the most critical tasks in Linux system administration. Whether you are hosting a website, running a database server, or configuring remote access, firewall ports determine which traffic is allowed into your system.
Leaving unnecessary ports open can expose your server to cyber threats, while blocking required ports can cause applications to fail. Thatβs why understanding how to properly manage firewall ports in Linux is essential for both beginners and experienced administrators.
In this guide, you will learn:
- What open ports are and why they matter
- How to check open ports in Linux
- How to open and close ports safely
- Step-by-step firewall configuration using UFW and firewalld
- Security best practices
Letβs get started.
What Is an Open Port in Linux?
A port is a communication endpoint that allows your server to send and receive data. Each service running on your system listens on a specific port.
For example:
| Service | Default Port |
|---|---|
| SSH | 22 |
| HTTP | 80 |
| HTTPS | 443 |
| MySQL | 3306 |
| PostgreSQL | 5432 |
When a port is open, external devices can connect to that service. When it is closed, connections are blocked by the firewall.
π Think of ports like doors in a building. You only want the necessary doors unlocked.
Why Managing Firewall Ports Is Important
Proper firewall management provides multiple benefits:
β Stronger Security
Closing unused ports reduces your attack surface and prevents unauthorized access.
β Better Network Control
You decide exactly which services are reachable from outside your network.
β Improved System Stability
Restricting traffic helps avoid overload from unwanted connections.
β Compliance Requirements
Many organizations require strict firewall policies for data protection.
How to Check Open Ports in Linux
Before opening or closing ports, you should identify which ones are already active.
Method 1: Using ss (Recommended)
The ss command is faster and more modern than older tools.
sudo ss -tuln
Explanation:
-tβ TCP connections-uβ UDP connections-lβ Listening ports-nβ Show numeric values
Youβll see output similar to:
tcp LISTEN 0 128 0.0.0.0:22
tcp LISTEN 0 128 0.0.0.0:80
This means SSH and HTTP are accepting connections.
Method 2: Using netstat
If your system still supports it:
sudo netstat -tuln
If the command is missing, install it:
sudo apt install net-tools
Method 3: Using lsof
To check which application is using a port:
sudo lsof -i :22
This is extremely useful for troubleshooting port conflicts.
Understanding Linux Firewall Options
Most Linux distributions rely on one of these firewall tools:
UFW (Uncomplicated Firewall)
Best for:
- Ubuntu
- Debian
- Beginners
- Quick server deployments
UFW simplifies complex iptables rules into easy commands.
firewalld
Best for:
- RHEL
- CentOS
- Rocky Linux
- Enterprise environments
It supports dynamic rule updates without restarting the firewall.
Step-by-Step: Open Ports Using UFW
Step 1: Check UFW Status
sudo ufw status
If inactive, enable it:
sudo ufw enable
β οΈ Important: Always allow SSH before enabling the firewall to avoid locking yourself out.
sudo ufw allow ssh
Step 2: Open a Specific Port
Example: Open port 8080
sudo ufw allow 8080
Allow TCP only:
sudo ufw allow 8080/tcp
Allow UDP:
sudo ufw allow 8080/udp
Step 3: Open Ports for a Service
Instead of remembering numbers:
sudo ufw allow http
sudo ufw allow https
Step 4: Verify the Rules
sudo ufw status numbered
Example output:
[1] 22/tcp ALLOW
[2] 80/tcp ALLOW
Step 5: Remove an Open Port
To close port 8080:
sudo ufw delete allow 8080
Or delete by rule number:
sudo ufw delete 2
Step-by-Step: Open Ports Using firewalld
Step 1: Check Firewall Status
sudo systemctl status firewalld
Start it if necessary:
sudo systemctl start firewalld
sudo systemctl enable firewalld
Step 2: List Current Ports
sudo firewall-cmd --list-ports
Step 3: Open a Port Temporarily
Example:
sudo firewall-cmd --add-port=8080/tcp
β οΈ This rule disappears after reboot.
Step 4: Open a Port Permanently
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
Step 5: Open a Service Instead
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
Step 6: Close a Port
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --reload
How to Test If a Port Is Open
After configuring the firewall, verify connectivity.
Using nc (Netcat)
From another machine:
nc -zv your_server_ip 8080
Success message:
Connection to your_server_ip 8080 port [tcp/*] succeeded!
Using telnet
telnet your_server_ip 8080
Common Firewall Mistakes to Avoid
β Opening Too Many Ports
Only allow what is necessary.
β Forgetting to Restrict IP Addresses
For sensitive services like databases:
UFW example:
sudo ufw allow from 192.168.1.10 to any port 3306
β Not Enabling the Firewall
Installing it isnβt enough β activate it.
β Ignoring Logs
Firewall logs help detect intrusion attempts.
Linux Firewall Security Best Practices
Follow these recommendations to keep your server secure:
β Use the Principle of Least Privilege
Open only the ports you absolutely need.
β Change Default Ports
For example, moving SSH from 22 reduces automated attacks.
β Enable Logging
Monitor suspicious traffic regularly.
β Keep Your System Updated
Security patches protect against known vulnerabilities.
sudo apt update && sudo apt upgrade
β Combine Firewall with Fail2Ban
Fail2Ban blocks repeated login attempts automatically.
UFW vs firewalld: Which Should You Choose?
| Feature | UFW | firewalld |
|---|---|---|
| Ease of Use | βββββ | βββ |
| Flexibility | βββ | βββββ |
| Beginner Friendly | β Yes | Moderate |
| Enterprise Ready | Limited | β Yes |
π Recommendation:
- Choose UFW for small servers or cloud deployments.
- Choose firewalld for enterprise infrastructure.
Conclusion
Managing open ports in Linux is not just about connectivity β itβs about protecting your server from unnecessary exposure.
By learning how to:
- Check active ports
- Open required services
- Close unused ports
- Apply firewall best practices
βyou dramatically improve your systemβs security posture.
Whether you use UFW for simplicity or firewalld for advanced control, consistent firewall management is a skill every Linux administrator should master.
Start reviewing your open ports today β your future self (and your server) will thank you.
Want to strengthen your Linux administration skills?
π Bookmark this guide for future reference.
π Share it with your team to promote better server security.
π Explore more Linux tutorials to level up your infrastructure management expertise.




