Linux Open Port: Managing Firewall Ports Step-by-Step Guide

Managing port on Linux

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:

ServiceDefault Port
SSH22
HTTP80
HTTPS443
MySQL3306
PostgreSQL5432

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?

FeatureUFWfirewalld
Ease of Use⭐⭐⭐⭐⭐⭐⭐⭐
Flexibility⭐⭐⭐⭐⭐⭐⭐⭐
Beginner Friendlyβœ… YesModerate
Enterprise ReadyLimitedβœ… 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.

(Visited 51 times, 1 visits today)

You may also like